Mark
Mark

Reputation: 2760

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

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>

Upvotes: 2

Views: 5280

Answers (1)

James Johnson
James Johnson

Reputation: 46047

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();"
}

Upvotes: 1

Related Questions