none
How to call C# event button_click from javascript function?

    General discussion

  •  How to call C# event button_click from javascript function?

    In the following code how do i call btnsubmit_Click event from function doBeforeUnload() of javascript

     

    Thanks In Advance,

    <script language="javascript" type="text/javascript">
      var UNLOAD_MSG = "Do you want to save the changes?";
      var IGNORE_UNLOAD = true;
    
      function doBeforeUnload() {
        if (IGNORE_UNLOAD) return; // Let the page unload 
    
        if (window.event)
          window.event.returnValue = UNLOAD_MSG; // IE 
        else
          return UNLOAD_MSG; // FireFox 
      }
      if (window.body)
        window.body.onbeforeunload = doBeforeUnload; // IE 
      else
        window.onbeforeunload = doBeforeUnload; // FireFox 
    
      function SetIgnoreUnload(bool) {
        IGNORE_UNLOAD = bool;
      }
    </script>
    <asp:TextBox ID="TextBox1" runat="server" Width="169px" MaxLength="3" ToolTip="Kindly fill the Code"
      CssClass="UpperCase" CausesValidation="true" onchange="IGNORE_UNLOAD = false"
      onkeypress="IGNORE_UNLOAD = false"></asp:TextBox>
    <asp:Button ID="btnsubmit" runat="server" Text="Submit" CssClass="longbuttonStyles"
      Width="120px" Height="32px" CausesValidation="true" OnClick="btnsubmit_Click"
      OnClientClick="SetIgnoreUnload(true);" />


    Ashish


    • Edited by Ashish Tripathi Tuesday, July 12, 2011 7:21 AM Code preview in aspx
    Tuesday, July 12, 2011 7:19 AM

All replies

  • You should be able to trigger the click event on the btnSubmit button.

     

    document.getElementById("btnSubmit").click();
    

    This will cause a postback and firing of btnSubmit_Click.

     

    You should have a look at jQuery javascript library, it helps to avoid writing specific code for IE, FF and other browsers.\

    To trigger click you would write like this

     

    $("#btnSubmit").click();
    

     

    For ASP.NET related questions you should take a look at the ASP.NET forums.
    Tuesday, July 12, 2011 1:12 PM
    Moderator
  • its giving document.getElementById("btnSubmit") as null . so not able to fire the event.

     

    Wednesday, July 13, 2011 9:15 AM
  • It is the ID of the ASP.NET button control and I misread that you spelled it "btnsubmit", all lower case.

    Try document.getElementById("btnsubmit").click() instead.

     

    Wednesday, July 13, 2011 9:42 AM
    Moderator
  • Hi

    You may either try

    document.getElementById('<%=btnSubmit.ClientID %>').click();

    or

    $get('<%=btnSubmit.ClientID %>').click();

    Sever controls has acquire a client id in the html mark up according to the hierarchy of the controls which is not exactly look like the id you assigned. So at client side we need to use the ClientId. Or if you sure the control having a unique id then you can set the client ID mode to static. So the id you given won't change. This has to be done with care because if two controls at different hierarchies (one on the page and one may be from a user control and both has client id mode static) will cause problems.

     

    Good luck

    Wednesday, July 13, 2011 10:57 AM
  • this post is going to be a repeatation of Albin Abel,

    to sum up, put this property to your button then in your client side your button name will be same as you give otherwise asp.net could change your button ID.

    ClientIDMode="Static" 
    

    have a look at this article to learn the ClientIDMode.


    http://www.west-wind.com/weblog/posts/2009/Nov/07/ClientIDMode-in-ASPNET-40

     

     


    I'm so sure you will not press to "Mark As Answer". and "Mark as Helpful".
    Wednesday, July 13, 2011 11:17 AM