How do I fire a server side button click event from javascript?

Discussion in 'ASP .Net' started by Steve Kershaw, May 27, 2006.

  1. Hi,

    I need to fire a server side button click event from my client side
    javascript. The client side javascript code follows:

    <script language="javascript" type="text/javascript">
    function myFunction()
    {
    var strname;
    strname = prompt("What is your name", "");
    form1.<%= hiddenTextField.ClientID %>.value = strname;
    form1.<% =btnSubmit.ClientID %>.click();

    }
    </script>

    You can see that I'm trying to use the "click();" event for the <asp:
    "btnSubmit" button but it dosen't seem to work.
    Any ideas?!

    Thanks in advance for your help.

    Steve
     
    Steve Kershaw, May 27, 2006
    #1
    1. Advertisements

  2. Bruno Alexandre, May 27, 2006
    #2
    1. Advertisements

  3. I'm sorry, maybe I didn't explain myself properly. I don't need a popup
    that allows the user to enter some text (their name in this case). The
    javascript I displayed already does that:

    strname = prompt("What is your name", "");

    What I need is, after the user enters their name and clicks the OK
    button on the javascript (client side) prompt popup, I need the
    javascript function to call a ( server side, ASP.NET, C# ) OnClick
    event.... Like this:

    form1.<% =btnSubmit.ClientID %>.click();

    The server side event looks like:

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
    TextBox2.Text = hiddenTextField.Value;
    Response.Write("Your name is: " + hiddenTextField.Value);
    }

    However, it dosen't work!
    Can anyone help?!!

    Steve
     
    Steve Kershaw, May 27, 2006
    #3
  4. Steve Kershaw

    Vinu Guest

    Hai Steve,

    I hope u r button id is "btnSubmit"

    Step 1:
    In the server side page load event just add this code...

    Page.GetPostBackEventReference(ImageButton1)

    Step 2:-

    Try calling this line " __doPostBack('btnSubmit','OnClick');"

    call the above function "callPostBack" after u r prompt
    statement.

    eg:-

    <script language="javascript" type="text/javascript">
    function myFunction()
    {
    var strname;
    strname = prompt("What is your name", "");

    // if its a valid name then just call post back..
    __doPostBack('btnSubmit','OnClick');

    }
    </script>

    The java script will trigger a postback as if it was initiated by
    the Button.. u can handle it on u r server side



    hope this helps u

    Regards
    Vinu
     
    Vinu, May 27, 2006
    #4
  5. Steve Kershaw

    sjr.priyanka

    Joined:
    Oct 8, 2011
    Messages:
    4
    Likes Received:
    0
    sjr.priyanka, Oct 8, 2011
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.
Loading...