我正在使用找到的代码 here使我的 gridview 具有可点击的行。其代码是:
protected void gvdownloadaccounts_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false; //hide the ID
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Row.ToolTip = "Click to select row";
e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.gvdownloadaccounts, "Select$" + e.Row.RowIndex);
}
}
...效果很好!...不过我需要让“onclick”在后面的代码中运行 C# 方法。该方法从数据库获取数据并用该数据填充一些 Web 控件(如文本框等)。这似乎并不那么难,所以如果有人能在正确的方向上给我一脚,那就太棒了。
我正在考虑仅重定向到同一页面的想法,但使用查询字符串,这样我就可以在 page_load 上捕获我的代码。但正如人们所预料的那样:
e.Row.Attributes["onclick"] = Response.Redirect("www.google.com");
...不起作用。
最佳答案
标记
<asp:Button ID="btn" runat="server" style="display:none;" OnClick="Btn_Click" OnClientClick="UpdateControl();"
/>
//该按钮将被隐藏。执行单击并在后面的代码中调用其处理程序中的函数将很有用。现在,在处理程序中编写代码来调用您的 C# 方法。
Java 脚本
//执行隐藏按钮的点击。
<script language="javascript" type="text/javascript">
function PerformClick() {
document.getElementById('<%=btn.ClientID %>').click();
}
</script>
如何在每次单击行时调用 C# 方法?
e.Row.Attributes["onclick"] = "<script language='javascript' type='text/javascript'>function PerformClick() {document.getElementById('<%=btn.ClientID %>').click();</script>";
关于c# - 单击 GridView 中的一行时回发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10135838/