我假设通过声明“[...]点击2行[...]”你实际上意味着“点击第二排”;至少,这是你最后的片段所暗示的,因为它只显示第二行的值(在一个侧面注意:ID在那里是错的;它应该是7890
)。
下面的代码段示出了GridView
其允许单个行的选择,以及使用在代码隐藏的事件处理程序的每个TextBox
的文本设置为根据值所选行中:
Page.aspx:在代码隐藏文件Page.aspx.cs
<asp:GridView runat="server" ID="gridView" OnSelectedIndexChanged="gridview_SelectedIndexChanged" AutoGenerateSelectButton="true"></asp:GridView>
事件处理程序:
void gridview_SelectedIndexChanged(object sender, EventArgs e)
{
var grid = sender as GridView;
if (grid == null) return;
//Cell[0] will be the cell with the select button; we don't need that one
Textbox1.Text = grid.SelectedRow.Cell[1].Text /* 2 */;
Textbox2.Text = grid.SelectedRow.Cell[2].Text /* Ravi */;
Textbox3.Text = grid.SelectedRow.Cell[3].Text /* 7890 */;
}