Page view counter

how do i make a call to onclick event from javascript?

Last post 11-22-2008 8:59 AM by NC01. 4 replies.

Sort Posts:

  • how do i make a call to onclick event from javascript?

    11-20-2008, 8:29 AM
    • Loading...
    • aspd
    • Joined on 08-26-2007, 9:40 PM
    • Posts 506
    • Points 36

    Hi All,

    I have a button that has "searchMember();" as the onclickclient event.

    I have a searchMember js function which is the following:

    function searchMember(sender,args) {
        __doPostBack('btnSearch', '');
        }

     

    I want the javascript to do a postback to the click event of btnSearch.

    Any ideas?

  • Re: how do i make a call to onclick event from javascript?

    11-20-2008, 9:40 AM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 7:33 PM
    • Posts 13,275
    • Points 71,391
    • TrustedFriends-MVPs

    That should work (just tried it and it does work for me). Are you sure that "btnSearch" is the correct ID of the button? Try this instead and see what happens:

    <script type="text/javascript">
    <!--
    function searchMember(sender,args)
    {
        <%= ClientScript.GetPostBackEventReference(btnSearch, string.Empty) %>;
    }
    // -->
    </script>

    That won't work from an include JavaScript file however.

    NC...

  • Re: how do i make a call to onclick event from javascript?

    11-20-2008, 10:00 AM
    • Loading...
    • siedler
    • Joined on 10-02-2008, 4:34 PM
    • Germany
    • Posts 166
    • Points 1,040

    Hi,

    here is a working sample

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    <title></title>

    </head>

    <script language="JavaScript">

    function setRedirect() {

    __doPostBack('Button1', '');

    }

    </script>

    <body>

    <form id="form1" runat="server">

     

    <div>

    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="ServerButton" />

    <input id="Button2" type="button" value="ClientButton" onclick="__doPostBack('Button1', '');"/>

    </div>

    </form>

    </body>

    </html>

    -------------------------------------------------
    Jürgen
  • Re: how do i make a call to onclick event from javascript?

    11-21-2008, 3:04 PM
    Answer
    • Loading...
    • rossisdead
    • Joined on 11-21-2008, 7:43 PM
    • Posts 20
    • Points 149

     That function in itself should work just fine, however you want to keep these things in mind:

    1. Make sure that the object is definitely being registered with the page's ScriptManager as an AsyncPostBackControl.

    2. __doPostBack() works with an ASP control's Name attribute versus it's ID attribute. So if your button's name is certainly "btnSearch" and doesn't have any additional naming container names added to it, then your function should work as-is. However, if your button's rendered control ID is something like "ctl00_Content_btnSearch", you would have to make sure the __doPostBack function has the button name formatted as "ctl00$Content$btnSearch".

  • Re: how do i make a call to onclick event from javascript?

    11-22-2008, 8:59 AM
    Answer
    • Loading...
    • NC01
    • Joined on 08-26-2005, 7:33 PM
    • Posts 13,275
    • Points 71,391
    • TrustedFriends-MVPs

    rossisdead:

     That function in itself should work just fine, however you want to keep these things in mind:

    1. Make sure that the object is definitely being registered with the page's ScriptManager as an AsyncPostBackControl.

    2. __doPostBack() works with an ASP control's Name attribute versus it's ID attribute. So if your button's name is certainly "btnSearch" and doesn't have any additional naming container names added to it, then your function should work as-is. However, if your button's rendered control ID is something like "ctl00_Content_btnSearch", you would have to make sure the __doPostBack function has the button name formatted as "ctl00$Content$btnSearch".

    If you hard-code the button's name/ID into the markup you are just asking for problems down the line as any changes to the page can change those properties.

    NC...

Page 1 of 1 (5 items)