Search code examples
asp.netgridviewselectedlinkbuttononclientclick

(ASP.NET) In grid view, how can I make the selected row data show up on a confirmation dialog box?


I tried the following code on a linkbutton onClientClick. But it is calling an error.

return confirm('""Are you sure you want to report on the  & **row.Cells(3).Text** &  vs  & **row.Cells(4).Text** & game, at the  & **row.Cells(5).Text** &  stadium. For  & **row.Cells(2).Text** &  on the  & **row.Cells(1).Text &** " ."'); 

Below is the rest of the code.

<asp:gridview id="FixtureGridView" runat="server"
              autogeneratecolumns="False"
              datasourceid="matches"
              height="140px" 
              width="800px" 
              onselectedindexchanged="FixtureGridView_SelectedIndexChanged">
              <columns>
                  <asp:commandfield showselectbutton="True" />
                  <asp:boundfield datafield="date" headertext="date" sortexpression="date" readonly="True" />
                  <asp:boundfield datafield="kick-off time" headertext="kick-off time" sortexpression="kick-off time" />
                  <asp:boundfield datafield="home team" headertext="home team" sortexpression="home team" />
                  <asp:boundfield datafield="away team" headertext="away team" sortexpression="away team" />
                  <asp:boundfield datafield="stadium" headertext="stadium" sortexpression="stadium" />
                  <asp:TemplateField>
                      <ItemTemplate>
                          <asp:LinkButton ID="LinkButton1" Runat="server"
                                          OnClientClick="return confirm('Are you sure you want to report on this game');"
                                          CommandName="Select">
                          Report
                          </asp:LinkButton>
                      </ItemTemplate>
                  </asp:TemplateField>

Solution

  • you can use the row_databound event of the gridview like this, you have to cast the found control to the same control type

    if (e.Row.RowType == DataControlRowType.DataRow) {
        LinkButton link = (LinkButton)e.Row.FindControl("LinkButton1");
        link.Attributes.Add("onclick", "return confirm('Are you sure you want to report on this game');");
    }