Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling server side event from html button control

Tags:

html

asp.net

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

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

I don't want to use asp control button.

Please help..

like image 834
jitendra Avatar asked Aug 25 '11 10:08

jitendra


People also ask

How to call aspx page from html button?

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.

Which HTML control provide ServerClick event?

The ServerClick event is raised when the HtmlButton control is clicked. This server event causes a round trip to occur from the client to the server and back.

How do you call a button click event in code behind?

To simulate the button click in code, you simply call the event handler: Button1_Click(object sender, EventArgs e). protected void Page_Load(object sender, EventArgs e) { //This simulates the button click from within your code. Button1_Click(Button1, EventArgs.


3 Answers

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.

like image 119
SPArcheon Avatar answered Oct 26 '22 16:10

SPArcheon


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!

like image 26
Ray Porrata Avatar answered Oct 26 '22 16:10

Ray Porrata


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
    }
}
like image 40
James Johnson Avatar answered Oct 26 '22 17:10

James Johnson