Link to home
Start Free TrialLog in
Avatar of Rajkumar Gs
Rajkumar GsFlag for India

asked on

Call Code-Behind Button Click Event or Function In JQuery

In ASPX Page, I have a dummy button
<asp:Button id="Dummybtn" runat="server" style="display:none;" OnClick="Dummybtn_Click"/>

Open in new window

This is its code-behind click event code
 Protected Sub Dummybtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Dummybtn.Click
        releaseTheDoc()
    End Sub

Open in new window

I am using jquery external files
   
......
 <script language="javascript" src="js/jquery.js"></script>
    <script language="javascript" src="js/navigateaway.min.js"></script>
</head>

Open in new window

In navigateaway.min.js, file - a event will get trigger when user tries to navigate away from current page. I want to execute an sql update query on this event. Code behind VB function releaseTheDoc() will do this.

What I am trying to achieve is call this function in JQuery OR
Call the Dummybtn_Click, which will do this job


Do you know, how to achieve this ?

Hope this make sense to you.
Raj
SOLUTION
Avatar of Rajkumar Gs
Rajkumar Gs
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rajkumar Gs

ASKER

How can I call a code-behind function in JQuery ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
on your asp button change to this:


<asp:Button id="Dummybtn" UseSubmitBehavior ="false" runat="server" style="display:none;" OnClick="Dummybtn_Click"/>

 
then in the html generated you can see that the onclick on your input button will have a __doPostBack js function,

then just copy the onclick function that the input button generates and copy it and call it within your javascript function

Thanks Proculopsis for your input.

I know that -
But I tried this code-snippet, which NOT worked.
 document.getElementbyid('Dummybtn').click();

Open in new window


Your code is working - Thanks!
document.getElementById( "Dummybtn" ).click();

Open in new window


Good. Do you know - How can I call a code-behind function in JQuery ?
@eguilherme: I got the button click worked in JQuery. Though I tried your suggestion.
Button
<asp:Button id="Dummybtn" runat="server" UseSubmitBehavior="false" style="display:none;" />

Open in new window


Checked the HTML generated
 PO-Details.txt

But I couldn't find that you suggested.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
RajkumarGSD:

in the html the button click of the dummybtn is:


javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;Dummybtn&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))

so if you call

WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;Dummybtn&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))
 
in javascript you will simulate the button click
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JosephEricDavis
JosephEricDavis

Otherwise, when you do your jquery ajax call to a specific url in your site that is hitting an aspx page, the pageLoad for that page will be the method that runs.  If you want to make it quick and dirty you can add code in your pageLoad method that will check the query string for a specific attribute and if it is present then you can call that specific method you are wanting to call on the server.

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["updateID"] != null)
    {
        YourMethod(Request.QueryString["updateID"]);
    }
}

In this case you would need to compose the url of your request to look something like this...
mypage.aspx?updateID=5

Of course updateID could be named whatever you wanted.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Guys for your suggestions.

I'll go through your comments and will be back soon

Raj
@eguilherme:

When I tried this code
 WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;Dummybtn&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))

Open in new window

instead of
$('#Dummybtn').trigger('click');

Open in new window

not triggering button click
Guys. I really appreciate your inputs.

Since I am in a tight timeline, I am going with button click which is working for me.
Once I have time, I will try your alternative suggestions. I know that is the better solution.
This is regarding calling a C# function in Javascript
C# Code
    public string strName = string.Empty;

    public void TestFunction()
    {
        strName = "Raj";
    }

Open in new window

Javascript Function
     <script type="text/javascript" language="javascript">         
     function init()          
     {               
     alert('hello ');               
     <%TestFunction();%>;               
     alert('<%=strName%>');          
     }          
     </script>

Open in new window


But this is not working with external file which is having jquery codes.
Thanks to you all
Raj
hi..
i have a aspx page it contains a usercontrol...when i click button in the user control a popup opens using jquery dialog ...popup is in the div tag in usercontrol....popup contains a button.......

When i click button in popup no code behind action taking place....?