C# 如何在C中单击GridView行后填充文本框
我有一个GridView:C# 如何在C中单击GridView行后填充文本框,c#,asp.net,gridview,rowdatabound,C#,Asp.net,Gridview,Rowdatabound,我有一个GridView: <asp:GridView ID="gridSearchResults" AutoGenerateColumns="false" DataKeyNames="uid" runat="server" AllowSorting="true" AutoGenerateSelectButton="true" CssClass="table table-striped table-bordered" OnRowDataBound="gridSearchRe
<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>
这很有效。但是,我想删除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.GetPostBackClientHyperlinkgrdSearchResults的内容,选择$+e.Row.RowIndex;但我不知道如何将两者混合
提前感谢您的帮助 答案可以在这里找到 对于我的解决方案,我需要修改一些东西。在gridSearchResults_RowDataBound中,我需要添加:
gridViewRowEventArgs.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gridSearchResults, "Select$" + gridViewRowEventArgs.Row.RowIndex);
在gridSearchResults\u UserSelected中,我需要更新单元格索引 是您的工作示例,您在gridSearchResults.SelectedRow.Cells集合中拥有所需的信息。在修改后的事件中,您是否尝试使用gridViewRowEventArgs.Row.Cells代替?您的问题不清楚。如果要删除选择按钮,可以将其从aspx代码中删除。OnSelectedIndexChanged仍然有效。不,它不起作用。
gridViewRowEventArgs.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gridSearchResults, "Select$" + gridViewRowEventArgs.Row.RowIndex);