- 爱易网页
-
ASP.NET教程
- GridView点击事件的有关问题
日期:2014-05-18 浏览次数:20464 次
GridView点击事件的问题
各位:
我的GridView 加入 编辑 删除等按钮
protected void gvNetnum_RowUpdating
这个事件 点击后我要获得 点击后这行 某一列的值 怎么做啊
以前2003我用
string ID = e.Item.Cells[1].Text.ToString();
2005
//GridViewRow row = gvNetnum.SelectedRow;
//string aa = row.Cells[1].Text.ToString();
都取不出来值啊? 谢谢大家帮帮我啊 新手啊
------解决方案--------------------
protected void GridView_UpdateCommand(object sender, GridViewUpdateEventArgs e)
{
//参考
gridView.Rows[e.RowIndex].Cells
gridView.Rows[e.RowIndex].FindControl()
}
------解决方案--------------------
#region//更新展开编辑列事件
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string nameStr = " ";
string addressStr = " ";
string cityStr = " ";
string stateStr = " ";
string zipStr = " ";
string updateStr = "update authors set au_lname=@name,address=@address,city=@city,state=@state,zip=@zip where au_id=@id ";
this.conString();
com = new SqlCommand(updateStr,con);
com.Parameters.Add( "@name ",System.Data.SqlDbType.VarChar,40);
com.Parameters.Add( "@address ",System.Data.SqlDbType.VarChar,40);
com.Parameters.Add( "@city ",System.Data.SqlDbType.VarChar,20);
com.Parameters.Add( "@state ",System.Data.SqlDbType.Char,2);
com.Parameters.Add( "@zip ",System.Data.SqlDbType.Char,5);
com.Parameters.Add( "@id ",System.Data.SqlDbType.Int,4);
GridViewRow gvRow = this.GridView1.Rows[e.RowIndex];//建立GridView行对象
TextBox nametxt = (TextBox)gvRow.Controls[1].Controls[0];
TextBox addresstxt = (TextBox)gvRow.Controls[2].Controls[0];
TextBox citytxt = (TextBox)gvRow.Controls[3].Controls[0];
TextBox statetxt = (TextBox)gvRow.Controls[4].Controls[0];
TextBox ziptxt = (TextBox)gvRow.Controls[5].Controls[0];
nameStr = nametxt.Text;
addressStr = addresstxt.Text;
cityStr = citytxt.Text;
stateStr = statetxt.Text;
zipStr = ziptxt.Text;
string ValStr = this.ValUpdate(nameStr,addressStr,cityStr,stateStr,zipStr);
if (ValStr == "ok ")
{
com.Parameters[ "@id "].Value = this.GridView1.DataKeys[(int)e.RowIndex].Value.ToString();
com.Parameters[ "@name "].Value = nameStr;
com.Parameters[ "@address "].Value = addressStr;
com.Parameters[ "@city "].Value = cityStr;
com.Parameters[ "@state "].Value = stateStr;
com.Parameters[ "@zip "].Value = zipStr;
try
{
con.Open();
com.ExecuteNonQuery();
this.Label3.Text = "更新成功! ";
con.Close();
}
catch (Exception ex)
{
this.Label3.Text = "无法更新! " + ex.Message;
}
finally
{
this.GridView1.EditIndex = -1;
this.Bind();
}
}
else
{
this.Label3.Text = ValStr;