calling server side event from html button control

后端 未结 6 1584
自闭症患者
自闭症患者 2020-12-13 14:32

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



        
相关标签:
6条回答
  • 2020-12-13 14:56

    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);
    }
    
    0 讨论(0)
  • 2020-12-13 15:00

    Please follow this tutorial: 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.

    -- More info on hijacking the postback event:

    • http://msdn.microsoft.com/en-us/library/aa720099(vs.71).aspx
    • http://wiki.asp.net/page.aspx/1082/dopostback-function/
    • http://geekswithblogs.net/mnf/archive/2005/11/04/59081.aspx
    0 讨论(0)
  • 2020-12-13 15:04

    You may use event handler serverclick as below

    //cmdAction is the id of HTML button as below

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

    //cs code

    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");
            }
        }
    
    0 讨论(0)
  • 2020-12-13 15:09

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

    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.

    <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.

    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.

    Hoping this helps. Enjoy!

    0 讨论(0)
  • 2020-12-13 15:12

    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.OnServerClick property.

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

    This should work and call foo_OnClick in 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.

    0 讨论(0)
  • 2020-12-13 15:12

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

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

    And in your 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
        }
    }
    
    0 讨论(0)
提交回复
热议问题