0

我的网格视图有问题。我已经搜索了很多解决方案,但找不到任何答案。我想我已经找到了问题,当我按下更新按钮时,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;
        }
    }
4

3 回答 3

0

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

发生这种情况的原因是您在 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());
}
于 2012-12-30T14:47:57.737 回答
0

我通常以这种方式进行数据绑定....试试这个函数并在你的页面加载和其他你需要的函数中调用它。

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();

}
于 2012-12-30T14:58:03.510 回答
0

或者您可以简单地将 !Page.IsPostBack 替换为 !IsPostBack

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGridView(_productRepo.GetAll());
    }
}
于 2012-12-31T16:38:50.307 回答