Asp.Net 在具有自动生成列的 GridView 中的代码隐藏编辑值中找到



我正在尝试创建一个网格视图的用户控件,该控件将能够显示,编辑和删除记录,并绑定到任何数据表。 在我的用户控件中,我有:

<asp:GridView ID="EditableGrid" runat="server" Width="500px" Height="500px" AllowSorting="True"
AutoGenerateColumns = "true"
AutoGenerateDeleteButton   ="true" AutoGenerateEditButton="true"
OnRowEditing="EditableGrid_RowEditing" 
OnRowCancelingEdit="EditableGrid_RowCancelingEdit"
OnRowUpdating="EditableGrid_RowUpdating"
OnRowDeleting="EditableGrid_RowDeleting"
></asp:GridView> 

在我的代码中,我有:

public void InitGrid(string theconnstr, string thetablename)
{
connstr = theconnstr;
tablename = thetablename;
//  Session["edgconnstr"] = connstr;
//  Session["edgtablename"] = tablename;
con = new SqlConnection(connstr);
con.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandText = "SELECT * FROM " + tablename;
using (SqlDataReader rd = cmd.ExecuteReader())
{
if (!rd.HasRows) return;
fields = new List<EdField>();
for (int i =0; i < rd.FieldCount; i++)
{
fields.Add(new EdField(rd.GetName(i), rd.GetDataTypeName(i)));
}
}
}
con.Close();
}
public void Bind()
{
//   connstr = (String)Session["edgconnstr"];
//  tablename = (String)Session["edgtablename"];
con = new SqlConnection(connstr);
con.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandText = "SELECT * FROM " + tablename;
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
da.Fill(dt);
EditableGrid.DataSource = dt;
EditableGrid.DataBind();
EditableGrid.Visible = true;
}
}
}
con.Close();
}

protected void EditableGrid_RowEditing(object sender, GridViewEditEventArgs e)
{
EditableGrid.EditIndex = e.NewEditIndex;
Bind();
}
protected void EditableGrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
EditableGrid.EditIndex = -1;
Bind();
}

protected void EditableGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
}

protected void EditableGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

}

现在,我尝试在EditableGrid_RowUpdating事件处理程序中找到已编辑行的新值,但没有成功。我只是无法访问文本框,因此无法运行查询以将旧值更新为新值。我想要实现的目标可能吗?

使用 e.NewValues 集合。

protected void EditableGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int tx = Convert.ToInt32(e.NewValues[0]);
}

最新更新