protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
TextBox na = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0];
TextBox i = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
TextBox sid = (TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0];
// string a = name.Text;
string name = na.Text;
int id = Convert.ToInt32(i.Text);
String stdid = sid.Text;
String query = "update [table] set name=@name,stdid=@stdid where id=@id ";
SqlConnection sqc = new SqlConnection(cons);
SqlCommand qu = new SqlCommand(query, sqc);
qu.Parameters.AddWithValue("@name", name);
qu.Parameters.AddWithValue("@stdid", stdid);
qu.Parameters.AddWithValue("@id", id);
string f = qu.CommandText;
sqc.Open();
qu.ExecuteNonQuery();
sqc.Close();
}
catch (Exception ee) { }
refresh_dgv();
}
并且有我的客户端鳕鱼:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
OnRowDeleting="GridView1_RowDeleting"OnRowEditing="GridView1_RowEditing" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True">
<Columns>
<asp:BoundField DataField="id" HeaderText ="id"/>
<asp:BoundField DataField="name" HeaderText="name" />
<asp:BoundField DataField="stdid" HeaderText="stdid" />
</Columns>
</asp:GridView>
答案 0 :(得分:0)
此问题可以通过以下多种方式解决。
<强> 1 强> 我们必须检查页面是否被回发。如果没有,那么我们将绑定网格。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
<强> 2 强> 使用 DataKeys 例如
<asp:gridview id="GridView1"
...
DataKeyNames="ProjectID"
/>
string empsname = Convert.ToString(grdvw1.DataKeys[e.RowIndex].Values[1]);
第3 强>
Enableviewstate 属性 false gridview 您可以获得新值