Gridview绑定意外清除

本文关键字:清除 意外 绑定 Gridview | 更新日期: 2023-09-27 17:49:53

我的gridview有问题。我已经寻找了很多解决方案,但没有找到任何答案。我想我已经找到了问题所在,即当我按下更新按钮时,gridview不再被绑定-这会导致空值。我以为在罗威汀就够了。我还可以在哪里绑定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;
        }
    }

Gridview绑定意外清除

如果我理解正确的话,您是说您的数据网格中的表单控件在回发后消失或重置为初始状态。

发生这种情况的原因是因为您在Page_Load方法中绑定了网格,这在页面生命周期中来得太晚,无法恢复控件值。直到LoadViewstate和LoadPostbackData事件被触发后,你的网格才会加载,因此,每次回发时,你的网格控件都会加载它们的原始状态。

我猜你熟悉asp.net生命周期,但如果不熟悉,这里有一篇文章:http://msdn.microsoft.com/en-us/library/ms972976.aspx。这个问题我已经处理过很多次了,我花了一段时间才完全理解这里发生了什么。

该问题的一个解决方案是在重写的OnInit方法中加载网格,该方法在恢复控制数据之前发生。像这样的代码应该可以工作:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    BindGridView(_productRepo.GetAll());
}

我通常这样做数据绑定....尝试这个函数,并在页面加载和其他需要的函数中调用它。

protected void bind()
{
    con.Open();
    SqlCommand cmd = new SqlCommand("Your Query", con);
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = cmd;
    da.Fill(ds);
    gvCourse.DataSource = ds;
    gvCourse.DataBind();
    con.Close();
}

或者您可以简单地替换!页。IsPostBack

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGridView(_productRepo.GetAll());
    }
}