我的网格视图有问题。我已经搜索了很多解决方案,但找不到任何答案。我想我已经找到了问题,当我按下更新按钮时,gridview 不再绑定 - 这会导致 null 值。我认为在 RowEditing 上绑定就足够了。我还能在哪里绑定我的gridview?
请参阅下面的标记:
<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">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" CausesValidation="false" />
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity" SortExpression="Quantity">
<EditItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Text='<%# Bind("Quantity") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblQuantity" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Family" SortExpression="Family.Name">
<EditItemTemplate>
<asp:DropDownList ID="ddlFamily" runat="server" OnInit="ddlFamily_Init">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblFamily" runat="server" Text='<%# Eval("Family.Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
和代码隐藏:
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<TextBox>(this.Page, "txtName");
TextBox txtQuantity = FindChildControl<TextBox>(this.Page, "txtQuantity");
DropDownList ddlFamily = FindChildControl<DropDownList>(this.Page, "ddlFamily");
// More code to populate a new product and bind the gridview again etc.
}
我也有一个 RowDataBound 方法。这会导致问题吗?
protected void ProductGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlFamily");
ddl.DataSource = _familyRepo.GetAll().Select(f => f.Name);
ddl.DataBind();
Product product = _productRepo.FindSingle(p => p.Id == (int)ProductGridView.DataKeys[e.Row.RowIndex].Value);
ddl.SelectedIndex = (int)product.FamilyID - 1;
}
}