【问题标题】:ADO.net Entity Gridview is null when RowUpdating event fired触发 RowUpdating 事件时,ADO.net Entity Gridview 为空
【发布时间】:2012-08-15 08:05:08
【问题描述】:

所以我在 ASP.Net (C#) 网页中使用 ADO.net 实体数据模型。我正在使用 gridviews 向我的数据库动态添加和检索数据。我正在使用母版页,而我的 gridview 位于 contentplaceholder 中。我对这段代码的唯一问题是,当我的 RowUpdating 事件触发时,gridview 为空。我可以通过 BindGV 函数调用,然后其余代码完美地更新数据库,使用我刚刚绑定到的数据库中的原始数据,因为数据库尚未更新。无论如何,如果我将 bindGV() 更改为 Gridview1.databind(),则 gridview 为空。我认为当数据连接关闭时,gridview 引用的数据源在事件结束时变为空,是否有办法防止这种情况发生?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    onrowcancelingedit="GridView1_RowCancelingEdit" 
    onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" 
    onrowdatabound="GridView1_RowDataBound" 
    >
<Columns>

    <asp:TemplateField Visible="false">
        <ItemTemplate>
            <asp:Label ID="lblId" runat="server" Text='<%# Eval("id") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:Label ID="lblId" runat="server" Text='<%# Eval("id") %>'></asp:Label>
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Machine">
        <ItemTemplate>
            <asp:Label ID="lblMachine" runat="server" Text='<%# Eval("MachineName") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtMachine" runat="server" Text='<%# Eval("MachineName") %>'></asp:TextBox>
        </EditItemTemplate>
        <ControlStyle Width="60px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Dept">
        <ItemTemplate>
            <asp:Label ID="lblDept" runat="server" Text='<%# Eval("WorkCenterFK") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:DropDownList ID="ddldept" runat="server" >
            </asp:DropDownList>
        </EditItemTemplate>
        <ControlStyle Width="120px" />
    </asp:TemplateField>
    <asp:CommandField HeaderText="Actions" ShowEditButton="true" ShowDeleteButton="True"
        ControlStyle-Width="50px" CausesValidation="false">
    </asp:CommandField>

</Columns>

protected void BindGV()
{
    QualityEntities database = new QualityEntities();
    GridView1.DataSource = (from m in database.Machines
                            from d in database.Workcenters
                            where m.WorkcenterFK == d.id
                            select
                                new { id = m.id, MachineName = m.MachineName, WorkCenterFK = d.WorkCenterName }); ;
    GridView1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack) BindGV();
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    BindGV();
}



protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
    BindGV();
}


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    QualityEntities database = new QualityEntities();
    BindGV();
    Int32 id = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("lblId")).Text);
    DropDownList ddl = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlDept"));
    TextBox txt = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtMachine"));
    Machine mch = (from m in database.Machines where m.id == id select m).Single();
    mch.MachineName = txt.Text;
    mch.WorkcenterFK = Convert.ToInt32(ddl.SelectedItem.Value);
    database.SaveChanges();
    GridView1.EditIndex = -1;
    BindGV();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    QualityEntities database = new QualityEntities();
    DropDownList temp = (DropDownList)(e.Row.FindControl("ddlDept"));

    if (temp != null)
    {
        temp.DataSource = (from w in database.Workcenters select w);
        temp.DataTextField = "WorkCenterName";
        temp.DataValueField = "id";
        temp.DataBind();
        Int32 id = Convert.ToInt32(((Label)e.Row.FindControl("lblId")).Text);
    }
}

【问题讨论】:

    标签: c# asp.net entity-framework


    【解决方案1】:

    不确定为什么GridView 为空,但我不使用成员变量,而是使用始终是事件源的事件的sender(在本例中为GridView

    // ...
    GridView grid = (GridView)sender;
    Int32 id = Convert.ToInt32(((Label)grid.Rows[e.RowIndex].FindControl("lblId")).Text);
    // ...
    

    【讨论】:

    • 感谢您的提示。当我回去清理我的代码时,我正计划与发件人一起玩。这个页面只是我快速拼凑的东西,以验证我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    相关资源
    最近更新 更多