这是UI中的代码
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn" runat="server" CommandName="Edit" Text="AFE" />
</ItemTemplate>
</asp:TemplateField>
--</Columns>
当我单击“编辑”按钮时,我想获取同一页面中文本框中所有字段的详细信息。我尝试使用:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
GridViewRow Row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
string text = Row.Cells[2].Text;
}
但我在字符串文本中得到“”..
这是我的 GridView 的截图
最佳答案
- 您可以使用存储在
命令参数
中的行索引来查找点击编辑
按钮的行。 - 现在您可以在单元格索引位置为 2 的那一行中找到
TextBox
。 然后获取
TextBox
的Text
。protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if(e.CommandName == "Edit") { int index = Convert.ToInt32(e.CommandArgument); GridViewRow clickedRow = CustomersGridView.Rows[index]; TextBox myTextBox = (TextBox)clickedRow.Cells[2].FindControl("TextBoxName"); string text = myTextBox.Text; } }
在ASPX页面中,你需要像这样处理行命令事件:
<asp:gridview id="GridView1"
datasourceid="DataSource"
autogeneratecolumns="false"
onrowcommand="GridView1_RowCommand"
runat="server">
</asp:gridview>
关于c# - 在行命令中获取 Gridview 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22394919/