如何在 C# 中单击 GridView 行后填充文本框
How to Populate Textbox after GridView row clicked in C#
我有一个 GridView:
<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>
使用 `AutoGenerateSelectButton="true" 每行都会出现一个 "select" 按钮。我可以点击它并开火:
`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;
}
有效。但是,我想删除 "select" 按钮并能够按行中的任意位置来填充文本框。
这是 gridSearchResults_RowDataBound 的代码,它应该处理行点击:
protected void gridSearchResults_RowDataBound(object objSender, GridViewRowEventArgs gridViewRowEventArgs) {
if (gridViewRowEventArgs.Row.RowType == DataControlRowType.DataRow) {
// Setup click handler and cursor
//gridViewRowEventArgs.Row.Attributes["onclick"] = DONT KNOW WHAT TO ADD HERE
gridViewRowEventArgs.Row.Attributes["style"] = "cursor:pointer";
// Implement row mouseover and mouseout
gridViewRowEventArgs.Row.Attributes.Add("onmouseover", "this.originalStyle=this.style.backgroundColor; this.style.backgroundColor='#B3E5FC';");
gridViewRowEventArgs.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalStyle;");
}
}
我见过类似 e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(grdSearchResults, "Select$" + e.Row.RowIndex);
的东西,但我不确定如何将两者混合。
在此先感谢您的帮助!
答案可以在这里找到; 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 索引。
我有一个 GridView:
<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>
使用 `AutoGenerateSelectButton="true" 每行都会出现一个 "select" 按钮。我可以点击它并开火:
`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;
}
有效。但是,我想删除 "select" 按钮并能够按行中的任意位置来填充文本框。
这是 gridSearchResults_RowDataBound 的代码,它应该处理行点击:
protected void gridSearchResults_RowDataBound(object objSender, GridViewRowEventArgs gridViewRowEventArgs) {
if (gridViewRowEventArgs.Row.RowType == DataControlRowType.DataRow) {
// Setup click handler and cursor
//gridViewRowEventArgs.Row.Attributes["onclick"] = DONT KNOW WHAT TO ADD HERE
gridViewRowEventArgs.Row.Attributes["style"] = "cursor:pointer";
// Implement row mouseover and mouseout
gridViewRowEventArgs.Row.Attributes.Add("onmouseover", "this.originalStyle=this.style.backgroundColor; this.style.backgroundColor='#B3E5FC';");
gridViewRowEventArgs.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalStyle;");
}
}
我见过类似 e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(grdSearchResults, "Select$" + e.Row.RowIndex);
的东西,但我不确定如何将两者混合。
在此先感谢您的帮助!
答案可以在这里找到; 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 索引。