Note that there are some explanatory texts on larger screens.

plurals
  1. POGet Selected Row On Button Click
    text
    copied!<p>I have a GridView which does the following:</p> <pre><code>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; } </code></pre> <p>I have a button called btnTest and I would like to be able to cast the current row to my custom object. I have tried the following without any luck.</p> <pre><code>Customer currentRow = (Customer)gvNotifications.SelectedRow.DataItem; </code></pre> <p>'DataItem' is always null. I'm sure this is a easy fix but after Googling the problem I have yet to find anything that would work.</p> <p>Here is my GridView in the aspx:</p> <pre><code>&lt;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"&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText="Customer Id" SortExpression="CustomerId" HeaderStyle-ForeColor="WhiteSmoke"&gt; &lt;ItemTemplate&gt; &lt;%#Eval("CustomerId")%&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText="Contact Name" SortExpression="ContactName" HeaderStyle-ForeColor="WhiteSmoke"&gt; &lt;ItemTemplate&gt; &lt;%#Eval("ContactName")%&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;asp:Button ID="btnTest" runat="server" Text="Use" onclick="btnTest_Click" /&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload