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..
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.
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.
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.
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.
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!
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With