Note that there are some explanatory texts on larger screens.

plurals
  1. POGridview binding clears unexpectedly
    text
    copied!<p>I have a problem with my gridview. I have searched alot for the solution but can't find any answers. I think I have located the problem to be that the gridview is no longer bound when I press the update button - which results in null values. I thought it was enough to bind at RowEditing. Where else can I bind my gridview?</p> <p>See the markup below:</p> <pre><code> &lt;asp:GridView ID="ProductGridView" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Id" OnRowEditing="ProductGridView_RowEditing" OnRowCancelingEdit="ProductGridView_RowCancelingEdit" OnRowUpdating="ProductGridView_RowUpdating" OnRowDeleting="ProductGridView_RowDeleting" OnDataBound="ProductGridView_DataBound" OnRowDataBound="ProductGridView_RowDataBound"&gt; &lt;Columns&gt; &lt;asp:CommandField ShowDeleteButton="True" ShowEditButton="True" CausesValidation="false" /&gt; &lt;asp:TemplateField HeaderText="Name" SortExpression="Name"&gt; &lt;EditItemTemplate&gt; &lt;asp:TextBox ID="txtName" runat="server" Text='&lt;%# Bind("Name") %&gt;'&gt;&lt;/asp:TextBox&gt; &lt;/EditItemTemplate&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID="lblName" runat="server" Text='&lt;%# Eval("Name") %&gt;'&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText="Quantity" SortExpression="Quantity"&gt; &lt;EditItemTemplate&gt; &lt;asp:TextBox ID="txtQuantity" runat="server" Text='&lt;%# Bind("Quantity") %&gt;'&gt;&lt;/asp:TextBox&gt; &lt;/EditItemTemplate&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID="lblQuantity" runat="server" Text='&lt;%# Eval("Quantity") %&gt;'&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText="Family" SortExpression="Family.Name"&gt; &lt;EditItemTemplate&gt; &lt;asp:DropDownList ID="ddlFamily" runat="server" OnInit="ddlFamily_Init"&gt; &lt;/asp:DropDownList&gt; &lt;/EditItemTemplate&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID="lblFamily" runat="server" Text='&lt;%# Eval("Family.Name") %&gt;'&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; </code></pre> <p>And the code-behind:</p> <pre><code> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindGridView(_productRepo.GetAll()); } } private void BindGridView(object source) { ProductGridView.DataSource = source; ProductGridView.DataBind(); } protected void ProductGridView_RowEditing(object sender, GridViewEditEventArgs e) { ProductGridView.EditIndex = e.NewEditIndex; BindGridView(_productRepo.GetAll()); // GetAll returns an IEnumerable. rowCount = ProductGridView.Rows.Count; // Count is 6 here, which is correct. } protected void ProductGridView_RowUpdating(object sender, GridViewUpdateEventArgs e) { rowCount = ProductGridView.Rows.Count; // Count is 0 here. //BindGridView(_productRepo.GetAll()); // Tried to rebind which works but getting the old values obviously. //rowCount = ProductGridView.Rows.Count; // Count is 6 here. // Get the controls - all is null. Works ok when I use BindGridView above. TextBox txtName = FindChildControl&lt;TextBox&gt;(this.Page, "txtName"); TextBox txtQuantity = FindChildControl&lt;TextBox&gt;(this.Page, "txtQuantity"); DropDownList ddlFamily = FindChildControl&lt;DropDownList&gt;(this.Page, "ddlFamily"); // More code to populate a new product and bind the gridview again etc. } </code></pre> <p>I also have a RowDataBound method. Can this contribute to the problem?</p> <pre><code>protected void ProductGridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow &amp;&amp; (e.Row.RowState &amp; DataControlRowState.Edit) == DataControlRowState.Edit) { DropDownList ddl = (DropDownList)e.Row.FindControl("ddlFamily"); ddl.DataSource = _familyRepo.GetAll().Select(f =&gt; f.Name); ddl.DataBind(); Product product = _productRepo.FindSingle(p =&gt; p.Id == (int)ProductGridView.DataKeys[e.Row.RowIndex].Value); ddl.SelectedIndex = (int)product.FamilyID - 1; } } </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