我正在尝试将 ADO 实体查询绑定到 gridview,但 gridview 触发的每个事件都会导致页面回发。每次页面回发时,数据源都是空的,我必须再次绑定查询。
除 rowupdating 事件外,所有代码都可用于显示和编辑数据。
我的问题在于,当 rowupdating 事件触发时,页面会回发,我必须再次绑定数据,丢失我想要更新数据库的选定值。如果我不再次绑定数据,则数据源为 null,并且我的 .findcontrol 返回 ArgumentOutOfRange 异常。
我通过以下方式测试 rowupdating 事件中的其余代码:
1) 在 rowupdating 事件上设置断点
2)点击更新超链接
3) 使用 SQL Server Management Studio 更改表中的数据
4) 继续代码执行
5) 重新执行 SQL SMS 中的查询
在此之后,表中的数据更新为旧值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCA_QualityModel;
public partial class Admin : System.Web.UI.Page
{
static HCA_QualityEntities database = new HCA_QualityEntities();
static object grvMachinesQuery = (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 });
protected void Page_Load(object sender, EventArgs e)
{
//bind the gridview on intial page load
if (!Page.IsPostBack) bindGridViewData();
}
protected void bindGridViewData()
{
//Data binds for gridviews and ddl's
grvMachines.DataSource = grvMachinesQuery;
grvWorkCenters.DataSource = (from w in database.Workcenters select w);
grvUsers.DataSource = (from u in database.Users select u);
ddlAddDept.DataSource = grvWorkCenters.DataSource;
ddlAddDept.DataValueField = "id";
ddlAddDept.DataTextField = "WorkCenterName";
DataBind();
}
//Adds new data to the db, then rebinds the griviews to the db
#region Submit Buttons
protected void btnSubmitMachine_Click(object sender, EventArgs e)
{
//add new machine to the database
Machine temp = new Machine();
temp.MachineName = txtAddMachine.Text;
temp.WorkcenterFK = Int32.Parse(ddlAddDept.SelectedValue);
database.Machines.AddObject(temp);
database.SaveChanges();
bindGridViewData();
}
protected void btnSubmitDept_Click(object sender, EventArgs e)
{
//add new workcenter to database
Workcenter temp = new Workcenter();
temp.WorkCenterName = txtAddDept.Text;
database.Workcenters.AddObject(temp);
database.SaveChanges();
bindGridViewData();
}
protected void btnUserSubmit_Click(object sender, EventArgs e)
{
//add new user to the database
HCA_QualityModel.User temp = new User();
temp.Username = txtAddUser.Text;
temp.Password = txtAddPassword.Text;
database.Users.AddObject(temp);
database.SaveChanges();
bindGridViewData();
}
#endregion
//Handles Updating, editing, and deleting Gridview Controls
#region Gridview machines
protected void grvMachines_RowDataBound(object sender, GridViewRowEventArgs e)
{
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();
}
}
protected void grvMachines_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Int32.Parse(((Label)grvMachines.Rows[e.RowIndex].FindControl("lblId") as Label).Text);
database.Machines.DeleteObject(((Machine)(from m in database.Machines where m.id == id select m).Single()));
database.SaveChanges();
bindGridViewData();
}
protected void grvMachines_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grvMachines.EditIndex = -1;
bindGridViewData();
}
protected void grvMachines_RowEditing(object sender, GridViewEditEventArgs e)
{
grvMachines.EditIndex = e.NewEditIndex;
bindGridViewData();
}
protected void grvMachines_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Gridviews have no data on postback from events, rebinding looses the selected values from edit mode.
bindGridViewData();
DropDownList dept = (DropDownList)grvMachines.Rows[grvMachines.EditIndex].FindControl("ddlDept");
TextBox name = (TextBox)grvMachines.Rows[grvMachines.EditIndex].FindControl("txtMachine");
Int32 id = (Convert.ToInt32(((Label)grvMachines.Rows[grvMachines.EditIndex].FindControl("lblId")).Text));
//Working Code to update the database, recieving incorrect data from controls due to rebinding
HCA_QualityEntities database = new HCA_QualityEntities();
Machine temp = (from m in database.Machines where m.id == id select m).First();
temp.MachineName = (name.Text);
temp.WorkcenterFK = Int32.Parse(dept.SelectedValue);
database.SaveChanges();
grvMachines.EditIndex = -1;
bindGridViewData();
}
#endregion
}
ASP代码
<%@ Page Title="" Language="C#" MasterPageFile="~/site.master" AutoEventWireup="true"
CodeFile="Admin.aspx.cs" Inherits="Admin" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<table style="width:800px; vertical-align:top;">
<tr style="width:800px;">
<td style="width:230px; height:90px;">
Add New Machine:
<br />
Machine Dept
<br />
<asp:TextBox ID="txtAddMachine" runat="server" Width="70px"></asp:TextBox>
<asp:DropDownList ID="ddlAddDept" runat="server" Width="110px">
</asp:DropDownList>
<br />
<asp:Button ID="btnSubmitMachine" runat="server" Text="Submit"
onclick="btnSubmitMachine_Click" />
</td>
<td style="width:190px; height:90px;">
Add New Dept:
<br />
Dept Name
<br />
<asp:TextBox ID="txtAddDept" runat="server" Width="100px"></asp:TextBox>
<br />
<asp:Button ID="btnSubmitDept" runat="server" Text="Submit"
onclick="btnSubmitDept_Click" />
</td>
<td style="width:250px; height:90px;">
Add New User:
<br />
Username Password
<br />
<asp:TextBox ID="txtAddUser" runat="server" Width="90px"></asp:TextBox>
<asp:TextBox ID="txtAddPassword" runat="server" Width="90px"></asp:TextBox>
<br />
<asp:Button ID="btnUserSubmit" runat="server" Text="Submit"
onclick="btnUserSubmit_Click" />
</td>
</tr>
<tr style="width:800px; vertical-align:top;">
<td style="width:230px;">
<asp:GridView ID="grvMachines" runat="server" AutoGenerateColumns="False"
Width="230px" onrowdeleting="grvMachines_RowDeleting"
onrowediting="grvMachines_RowEditing"
onrowcancelingedit="grvMachines_RowCancelingEdit"
onrowupdating="grvMachines_RowUpdating"
onrowdatabound="grvMachines_RowDataBound"
EnableViewState="False">
<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="50px" />
</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>
</asp:GridView>
</td>
<td style="width:170px;">
<asp:GridView ID="grvWorkCenters" runat="server" AutoGenerateColumns="False"
Width="170px">
<Columns>
<asp:TemplateField HeaderText="id" 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="WorkCenter">
<ItemTemplate>
<asp:Label ID="lblWorkcenter" runat="server" Text='<%# Eval("WorkCenterName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtWorkcenter" runat="server" Text='<%# Eval("WorkCenterName") %>'></asp:TextBox>
</EditItemTemplate>
<ControlStyle Width="75px" />
</asp:TemplateField>
<asp:CommandField HeaderText="Actions" ShowEditButton="true" ShowDeleteButton="true"
ControlStyle-Width="50px" CausesValidation="False" />
</Columns>
</asp:GridView>
</td>
<td style="width:290px;">
<asp:GridView ID="grvUsers" runat="server" AutoGenerateColumns="False"
Width="290px">
<Columns>
<asp:TemplateField HeaderText="id" Visible="false">
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtId" runat="server" Text='<%# Eval("id") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User">
<ItemTemplate>
<asp:Label ID="lblUser" runat="server" Text='<%# Eval("Username") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtUser" runat="server" Text='<%# Eval("Username") %>'></asp:TextBox>
</EditItemTemplate>
<ControlStyle Width="120px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:Label ID="lblPassword" runat="server" Text='<%# Eval("Password") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPassword" runat="server" Text='<%# Eval("Password") %>'></asp:TextBox>
</EditItemTemplate>
<ControlStyle Width="120px" />
</asp:TemplateField>
<asp:CommandField HeaderText="Actions" ShowEditButton="true" ShowDeleteButton="true"
ControlStyle-Width="50px" CausesValidation="false" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</asp:Content>