c# - 单击 GridView 中的一行时回发

标签 c# asp.net gridview postback web-controls

我正在使用找到的代码 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/

相关文章:

c# - Linq OrderBy() 与 List.Sort() 的速度

c# - 在 LoginName 控件中显示全名而不是用户名

c# - 传递参数,是否发生拆箱

c# - ASP.NET MVC 中的 session 变量

c# - 有什么方法可以通过文本输入即时过滤 GridView 吗?

c# - 从设计器中隐藏 WPF 控件的属性 (Visual Studio 2010)

c# - 将本地化字符串转换为十进制

c# - System.Data.SqlClient.SqlConnection 错误 : Invalid column name 'B' .

Android:动态添加单元格到GridView?

asp.net - 向 ASP.NET Gridview 添加动态列