How can I redirect from a grid view when a row is clicked

半城伤御伤魂 提交于 2019-12-12 21:19:07

问题


In my grid View I have a template field button when clicked it redirects the page and sends the msgid along with the url, Is it possible to do that by clicking anywhere in the row, i.e if i click on the msgid or title field then it should redirect to respond metric page with the row msgid. Just like a select button,

MsgID      Title       Actions

1          First        Image(Which redirects)
2          second       Image(Which redirects)

in this table if the user clicks on any spot in row 1 then the page should redirect, it has to perform the same action of Image. How can I do that

  <asp:GridView ID="Grid_Messagetable" runat="server" BorderWidth="5" GridLines="None"
                    CssClass="gridTable" SelectedIndex="0"
                     DataKeyNames="MsgID" ShowHeaderWhenEmpty="true"
                    OnRowDataBound="MyGrid_RowDataBound" AutoGenerateColumns="False" AllowSorting="true"
                    OnSorting="gridView_Sorting" >                                     <Columns>

                        <asp:BoundField DataField="MsgID" HeaderText="MsgID" SortExpression="MsgID" />
                        <asp:BoundField DataField="Title" ItemStyle-Width="35%" HeaderText="Subject" SortExpression="Title" />                                                          <asp:TemplateField HeaderText="Actions" ItemStyle-Width="15%">
       <ItemTemplate>
           <asp:ImageButton ID="imgbtn_ViewDashBoard"  ImageUrl="Styles/Images/icon_dashboard.png"
                                    Enabled="True" Width="" runat="server" PostBackUrl='<%# Eval("MsgID", "ResponseMetric.aspx?MsgID={0}") %>'
                                    Text='Send' ToolTip="View DashBoard"></asp:ImageButton>
                            </ItemTemplate>
                        </asp:TemplateField>

                    </Columns>
                </asp:GridView>

回答1:


You can use the RowDataBound event to attach a click event to the row.

Markup:

redirect = function(){
    window.location.hef = "home.aspx";
}

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" ...>

Code-behind:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Attributes["onclick"] = "redirect();"
}


来源:https://stackoverflow.com/questions/8068782/how-can-i-redirect-from-a-grid-view-when-a-row-is-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!