所以我在ASP.Net(C#)网页中使用ADO.net实体数据模型。我使用gridviews动态地向我的数据库添加和检索数据。我正在使用母版页,我的gridview位于contentplaceholder中。我唯一的问题是,当我的RowUpdating事件触发时,gridview为null。我可以通过BindGV函数调用,然后其余的代码完全正确地更新数据库,我刚刚绑定到数据库的原始数据,因为数据库还没有更新。在所有事件中,如果我将bindGV()更改为Gridview1.databind(),则gridview为null。我认为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);
}
}
答案 0 :(得分:1)
不确定为什么GridView
为null,但不使用成员变量,而是使用事件的sender
,它始终是事件的来源(在本例中为{{1} }})
GridView