如何在C#中单击GridView行后填充文本框

时间:2017-11-29 16:44:02

标签: c# asp.net gridview rowdatabound

我有一个GridView:

 DataSet ds = excelReader.AsDataSet();
        DataTable data = ds.Tables[0];
        List<DataRow> rows = data.Rows.Cast<DataRow>().ToList();

使用“AutoGenerateSelectButton =”true“每行都会出现”选择“按钮。我可以点击这个并开火:

List<String>

哪个有效。但是,我想删除“选择”按钮,并能够按行中的任何位置来填充TextBoxes。

这是gridSearchResults_RowDataBound的代码,它应该处理行点击:

<asp:GridView ID="gridSearchResults" AutoGenerateColumns="false" DataKeyNames="uid" runat="server"
    AllowSorting="true" AutoGenerateSelectButton="true" CssClass="table table-striped table-bordered"
    OnRowDataBound="gridSearchResults_RowDataBound"
    OnSelectedIndexChanged="gridSearchResults_UserSelected">
    <Columns>
        <asp:BoundField DataField="uid" HeaderText="UID" SortExpression="uid" ItemStyle-Width="30%"/>
        <asp:BoundField DataField="givenName" HeaderText="First Name" SortExpression="givenName" ItemStyle-Width="35%" />
        <asp:BoundField DataField="sn" HeaderText="Last Name" SortExpression="sn" ItemStyle-Width="35%" />
    </Columns>
</asp:GridView>

我见过类似`protected void gridSearchResults_UserSelected(object sender, EventArgs e) { // Get the user's ID from the selected row idNumber.Text = gridSearchResults.SelectedRow.Cells[1].Text; firstName.Text = gridSearchResults.SelectedRow.Cells[2].Text; lastName.Text = gridSearchResults.SelectedRow.Cells[3].Text; } 的内容,但我不确定如何将两者混合在一起。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

答案可以在这里找到; https://www.aspsnippets.com/Articles/Selecting-GridView-Row-by-clicking-anywhere-on-the-Row.aspx

对于我的解决方案,我需要修改一些事情。在gridSearchResults_RowDataBound我需要添加:

gridViewRowEventArgs.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gridSearchResults, "Select$" + gridViewRowEventArgs.Row.RowIndex);

gridSearchResults_UserSelected我需要更新Cell索引。