How to call button click event from java script functionLet's say we want to call button click event on body load event.
So the first step to design a html form with two hidden field to store the object sender and event arguments value as follows:
<bodyy onload="javascript:__doPostBack('Button1','')">
<formm id="Form1" runat="server">
<div>
<input type ="hidden" name ="__EVENTTARGET" value =""/>
<input type ="hidden" name ="__EVENTARGUMENT" value =""/>
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />
</div>
</form>
</body>
Then write the java script function in the head section:
<script type="text/javascript">
function __doPostBack(eventTarget, eventArgument) {
document.Form1.__EVENTTARGET.value = eventTarget;
document.Form1.__EVENTARGUMENT.value = eventArgument;
document.Form1.submit();
}
</script>
Last step to put the following code in .cs file:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form["__EVENTTARGET"] == "Button1")
{
//fire event
Button1_Click(this, new EventArgs());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//write your code here...
}