C# 将 OnClick 事件添加到 ASP.NET 控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10893409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:40:45  来源:igfitidea点击:

Adding OnClick event to ASP.NET control

c#asp.netonclickcontrols

提问by Oak

i would like to create OnClickevent for my panel. So far now the most of the google results look more or less like this: adding onclick event to aspnet label. Is there any way, to call codebehind function from javascript or panel attributes? Because I would like to Redirect user to a new page and before that save some information in ViewSTate or Sessionstate. Any suggestions?

我想OnClick为我的panel. 到目前为止,大多数 google 结果看起来或多或少是这样的:将 onclick 事件添加到 aspnet label。有什么办法可以从 javascript 或面板属性调用代码隐藏函数吗?因为我想将用户重定向到新页面,然后在 ViewSTate 或 Sessionstate 中保存一些信息。有什么建议?

采纳答案by Imran Balouch

In your java script method raise a __dopostbackcall to a Server side method.

在您的 java 脚本方法中,__dopostback调用Server side method.

<script type="text/javascript">
     function YourFunction()
     {
         __doPostBack('btnTemp', '')
     }
</script>

Where btnTempis a server side button, so write a onClick event of this button on server side, where you can do the processing and then redirect to other page.

其中btnTemp是服务器端按钮,因此在服务器端编写此按钮的 onClick 事件,您可以在其中进行处理,然后重定向到其他页面。

You can have a good understanding of dopostback at DoPostBack Understanding

你可以在DoPostBack 理解中对 dopostback 有很好的理解

回答by Liath

There is a PostBackUrl property on a ASP.NET Button, you could render the button as normal then postback to a different page - this is where your OnClick method would need to be declared.

ASP.NET 按钮上有一个 PostBackUrl 属性,您可以正常呈现该按钮,然后回发到不同的页面 - 这是需要声明 OnClick 方法的地方。

I would strongly recommend against posting back to the same page then doing a Response.Redirect(), consider the traffic. The browser requests the page, posts back then is sent a HttpRedirect and then navigates to the new page. With the method I have outlined above this is not required and the browser has to make one request less (meaning the message doesn't have to be sent or the page rebuilt on the server) and is a significant performance benefit.

我强烈建议不要发回同一页面然后执行 Response.Redirect(),考虑流量。浏览器请求页面,回帖,然后发送一个 HttpRedirect,然后导航到新页面。使用我上面概述的方法,这不是必需的,浏览器必须减少一个请求(意味着不必发送消息或在服务器上重建页面),这是一个显着的性能优势。

回答by Imran Balouch

My aspx page is like:

我的 aspx 页面是这样的:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <script type="text/javascript">
        function CallMe() { __doPostBack('btnTemp', '') }
    </script>
</head>
<body>
    <form id="form1" runat="server">
         <asp:Button ID="btnTemp" runat="server" Text="Test" onclick="btnTemp_Click" />
         <div> <asp:Label ID="Label1" runat="server" Text="Label1"></asp:Label>
         <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></div>
    </form>
</body>

And my Server Side code is as:

我的服务器端代码如下:

protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Attributes.Add("onClick", "CallMe();");
    }
protected void btnTemp_Click(object sender, EventArgs e)
    {

    }

Thats the code that I have written, I haven;t included the using statement, Page directive etc in above code.

这就是我编写的代码,我没有在上面的代码中包含 using 语句、Page 指令等。