从 html 按钮控件调用服务器端事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7189037/
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-29 10:15:39  来源:igfitidea点击:

calling server side event from html button control

asp.nethtml

提问by jitendra

I am creating an application using ASP.Net, in which I have a HTML button on an aspx page.

我正在使用 ASP.Net 创建一个应用程序,其中在 aspx 页面上有一个 HTML 按钮。

<input type="submit" id="btnSubmit" runat="server" 
   style="background-image:url(App_Themes/default/Images/quiz_class/btn_submit.jpg);
   width:80px; height:29px;" onserverclick="btnSubmit_ServerClick" />

But when I click on the button then it not calling the btnSubmit_ServerClick event on the aspx.cs page.

但是当我点击按钮时,它不会调用 aspx.cs 页面上的 btnSubmit_ServerClick 事件。

I don't want to use asp control button.

我不想使用asp控制按钮。

Please help..

请帮忙..

回答by BlueSoul

If you are OK with converting the input button to a server side control by specifying runat="server", and you are using asp.net, an option could be using the HtmlButton.OnServerClickproperty.

如果您可以通过指定将输入按钮转换为服务器端控件runat="server",并且您正在使用asp.net,则可以选择使用HtmlButton.OnServerClick属性。

<input id="foo "runat="server" type="button" onserverclick="foo_OnClick" />

This should work and call foo_OnClickin your server side code. Also notice that based on Microsoft documentation linked above, you should also be able to use the HTML 4.0 tag.

这应该可以工作并调用foo_OnClick您的服务器端代码。另请注意,根据上面链接的 Microsoft 文档,您还应该能够使用 HTML 4.0 标记。

回答by Ray Porrata

On your aspx page define the HTML Button element with the usual suspects: runat, class, title, etc.

在您的 aspx 页面上,使用通常的可疑对象定义 HTML Button 元素:runat、class、title 等。

If this element is part of a data bound control (i.e.: grid view, etc.) you may want to use CommandName and possibly CommandArgument as attributes. Add your button's content and closing tag.

如果此元素是数据绑定控件的一部分(即:网格视图等),您可能希望使用 CommandName 和可能的 CommandArgument 作为属性。添加按钮的内容和结束标记。

<button id="cmdAction" 
    runat="server" onserverclick="cmdAction_Click()" 
    class="Button Styles" 
    title="Does something on the server" 
    <!-- for databound controls -->
    CommandName="cmdname"> 
    CommandArgument="args..."
    >
    <!-- content -->
    <span class="ui-icon ..."></span>
    <span class="push">Click Me</span>
</button>

On the code behind page the element would call the handler that would be defined as the element's ID_Click event function.

在代码隐藏页面上,元素将调用定义为元素的 ID_Click 事件函数的处理程序。

protected void cmdAction_Click(object sender, EventArgs e)
{
: do something.
}

There are other solutions as in using custom controls, etc. Also note that I am using this live on projects in VS2K8.

还有其他解决方案,如使用自定义控件等。另请注意,我在 VS2K8 中的项目中实时使用它。

Hoping this helps. Enjoy!

希望这会有所帮助。享受!

回答by James Johnson

The easiest way to accomplish this is to override the RaisePostBackEvent method.

完成此操作的最简单方法是覆盖 RaisePostBackEvent 方法。

<input type="button" ID="btnRaisePostBack" runat="server" onclick="raisePostBack();" ... />

And in your JavaScript:

在你的 JavaScript 中:

raisePostBack = function(){
    __doPostBack("<%=btnRaisePostBack.ClientID%>", "");
}

And in your code:

在你的代码中:

protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
    //call the RaisePostBack event 
    base.RaisePostBackEvent(source, eventArgument);

    if (source == btnRaisePostBack)
    {
         //do some logic
    }
}

回答by Bram Vandenbussche

Please follow this tutorial: http://decoding.wordpress.com/2008/11/14/aspnet-how-to-call-a-server-side-method-from-client-side-javascript/

请遵循本教程:http: //decoding.wordpress.com/2008/11/14/aspnet-how-to-call-a-server-side-method-from-client-side-javascript/

On a sidenote: ASP.NET generates a client side javascript function that you can call from your own functions to perform the postback you want.

旁注:ASP.NET 生成一个客户端 javascript 函数,您可以从您自己的函数调用该函数来执行您想要的回发。

-- More info on hiHymaning the postback event:

-- 有关劫持回发事件的更多信息:

回答by Alinezhada

just use this at the end of your button click event

只需在按钮单击事件结束时使用它

protected void btnAddButton_Click(object sender, EventArgs e)
{
   ... save data routin 
     Response.Redirect(Request.Url.AbsoluteUri);
}

回答by Ankur Agarwal

You may use event handler serverclick as below

您可以使用事件处理程序 serverclick 如下

//cmdAction is the id of HTML button as below

//cmdAction 是 HTML 按钮的 id 如下

<body>
    <form id="form1" runat="server">
        <button type="submit" id="cmdAction" text="Button1" runat="server">
            Button1
        </button>
    </form>
</body>

//cs code

//cs代码

public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
                cmdAction.ServerClick += new EventHandler(submit_click);  
        }

        protected void submit_click(object sender, EventArgs e)
        {
            Response.Write("HTML Server Button Control");
        }
    }