<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID,Name" >
<Columns>
<asp:TemplateField HeaderText="用户姓名" >
<ItemTemplate>
<asp:TextBox ID="UserName" Text="-" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作" >
<ItemTemplate>
<asp:LinkButton runat="server" ID="btnSave" OnCommand="btn_Save" Text="保存" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.aspx.cs文件:
//对保存按钮点击的处理
protected void btn_Save(object sender, CommandEventArgs e)
{
//这里如何获得用户修改文本框UserName的值?以便将修改的值保存进数据库。
}
8 个解决方案
#1
在前一个页面设置文本框的值然后添加到这页吧!不然好像很麻烦啊!
#2
<asp:LinkButton runat="server" CommandArgument='<%#((GridViewRow)Container).RowIndex %>' ID="btnSave" OnCommand="btn_Save" Text="保存" />
//对保存按钮点击的处理
protected void btn_Save(object sender, CommandEventArgs e)
{
int nIndex = 0;
if(!int.TryParse(e.CommandArgument.ToString(),out nIndex))
{
return;
}
if (nIndex < 0 || nIndex > this.GridView1.Rows.Count - 1) return;
TextBox t = (TextBox)this.GridView1.Rows[nIndex].FindControl("UserName");
if (t != null)
{
Response.Write(t.Text);
}
}
#3
protected void btn_Save(object sender, CommandEventArgs e)
{
Button btn = sender as Button;
GridViewRow row = btn.NamingContainer as GridViewRow;
TextBox textBox = row.FindControl("UserName") as TextBox;
string xx = textBox.Text;
}
{
Button btn = sender as Button;
GridViewRow row = btn.NamingContainer as GridViewRow;
TextBox textBox = row.FindControl("UserName") as TextBox;
string xx = textBox.Text;
}
#4
顶2楼。。不错。学习。
#5
zhujiazhi 的方法更简单,把Button 改成LinkButton就可以了
#6
2# 大牛。。。。。正解
#7
在LinkButton的click事件下:
protected void btnSave_Click(object sender, EventArgs e)
{
LinkButton myL = (LinkButton)sender;
int Index = ((GridViewRow)(myL.NamingContainer)).RowIndex;//获得行号
string a=((TextBox)GridView1.Rows[index].Cells[所在列号].FindControl("UserName")).Text; //
}
protected void btnSave_Click(object sender, EventArgs e)
{
LinkButton myL = (LinkButton)sender;
int Index = ((GridViewRow)(myL.NamingContainer)).RowIndex;//获得行号
string a=((TextBox)GridView1.Rows[index].Cells[所在列号].FindControl("UserName")).Text; //
}
#8
UP
#1
在前一个页面设置文本框的值然后添加到这页吧!不然好像很麻烦啊!
#2
<asp:LinkButton runat="server" CommandArgument='<%#((GridViewRow)Container).RowIndex %>' ID="btnSave" OnCommand="btn_Save" Text="保存" />
//对保存按钮点击的处理
protected void btn_Save(object sender, CommandEventArgs e)
{
int nIndex = 0;
if(!int.TryParse(e.CommandArgument.ToString(),out nIndex))
{
return;
}
if (nIndex < 0 || nIndex > this.GridView1.Rows.Count - 1) return;
TextBox t = (TextBox)this.GridView1.Rows[nIndex].FindControl("UserName");
if (t != null)
{
Response.Write(t.Text);
}
}
#3
protected void btn_Save(object sender, CommandEventArgs e)
{
Button btn = sender as Button;
GridViewRow row = btn.NamingContainer as GridViewRow;
TextBox textBox = row.FindControl("UserName") as TextBox;
string xx = textBox.Text;
}
{
Button btn = sender as Button;
GridViewRow row = btn.NamingContainer as GridViewRow;
TextBox textBox = row.FindControl("UserName") as TextBox;
string xx = textBox.Text;
}
#4
顶2楼。。不错。学习。
#5
zhujiazhi 的方法更简单,把Button 改成LinkButton就可以了
#6
2# 大牛。。。。。正解
#7
在LinkButton的click事件下:
protected void btnSave_Click(object sender, EventArgs e)
{
LinkButton myL = (LinkButton)sender;
int Index = ((GridViewRow)(myL.NamingContainer)).RowIndex;//获得行号
string a=((TextBox)GridView1.Rows[index].Cells[所在列号].FindControl("UserName")).Text; //
}
protected void btnSave_Click(object sender, EventArgs e)
{
LinkButton myL = (LinkButton)sender;
int Index = ((GridViewRow)(myL.NamingContainer)).RowIndex;//获得行号
string a=((TextBox)GridView1.Rows[index].Cells[所在列号].FindControl("UserName")).Text; //
}
#8
UP