c# - Get Selected Row On Button Click -


i have gridview following:

protected override void render(system.web.ui.htmltextwriter textwriter)     {         foreach (gridviewrow gvrow in gvnotifications.rows)         {             if (gvrow.rowtype == datacontrolrowtype.datarow)             {                 gvrow.attributes.add("onmouseover", "this.previous_color=this.style.backgroundcolor;this.style.backgroundcolor='#ffff99';this.style.cursor='hand';");                 gvrow.attributes.add("onmouseout", "this.style.backgroundcolor=this.previous_color;");                 gvrow.attributes["onclick"] = clientscript.getpostbackclienthyperlink(gvnotifications, "select$" + gvrow.rowindex,true);             }         }          base.render(textwriter);     }      protected void gvnotifications_selectedindexchanged(object sender, eventargs e)     {         gvnotifications.selectedrowstyle.backcolor = color.lightblue;     } 

i have button called btntest , able cast current row custom object. have tried following without luck.

customer currentrow = (customer)gvnotifications.selectedrow.dataitem; 

'dataitem' null. i'm sure easy fix after googling problem have yet find work.

here gridview in aspx:

<asp:gridview      id="gvnotifications"     runat="server"     autogeneratecolumns="false"     gridlines="none"     cssclass="mgrid"     alternatingrowstyle-backcolor="#f0f0f0"     allowpaging="true"     allowsorting="true"     pagerstyle-cssclass="pgr"     pagesize="25"     onpageindexchanging="gvnotifications_pageindexchanging"     onsorting="gvnotifications_sorted" headerstyle-cssclass="srtac"      onselectedindexchanged="gvnotifications_selectedindexchanged">     <columns>         <asp:templatefield headertext="customer id" sortexpression="customerid" headerstyle-forecolor="whitesmoke">             <itemtemplate>                 <%#eval("customerid")%>             </itemtemplate>         </asp:templatefield>         <asp:templatefield headertext="contact name" sortexpression="contactname" headerstyle-forecolor="whitesmoke">             <itemtemplate>                 <%#eval("contactname")%>             </itemtemplate>         </asp:templatefield>     </columns> </asp:gridview> <asp:button id="btntest" runat="server" text="use" onclick="btntest_click" /> 

try selected row

protected void yourgridview_selectedindexchanged(object sender, eventargs e) {      //grab selected row      gridviewrow row = yourgridviewid.selectedrow;  } 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -