1

所以我在 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);
    }
}
4

1 回答 1

1

不知道为什么GridView是空的,但我不会使用成员变量,而是使用sender始终是事件源的事件(在这种情况下GridView

// ...
GridView grid = (GridView)sender;
Int32 id = Convert.ToInt32(((Label)grid.Rows[e.RowIndex].FindControl("lblId")).Text);
// ...
于 2012-08-19T21:29:52.000 回答