Fire Code behind code AND Javascript code associated to a Button Click Event

Discussion in 'ASP .Net' started by =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004.

  1. I have a button that opens a new Window (some kind of search-window), which is fired using JavaScript (btnSearch.Attributes["onclick"]=".....";)
    Now I need to run some code behind code BEFORE this JavaScript runs. I tried to define another Button with an EventHandler associated, and from this (after doing my code) fire 'btnSearch.click()'. But I can't make it work.
    I tried
    private void Button1_Click(object sender, System.EventArgs e

    RestartAll()
    Response.Write("<script>window.document.forms[0].elements['btnSearch'].click();</script>")

    but unfortunately the browser says that this element is not valid
    Am I on the right way or are there better options to do what I try to accomplish
    Thanks for any help
     
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004
    #1
    1. Advertising

  2. Carlo
    In order to do what you want you can call a Javascript function to load a new window after you handled the post back event of your search click button

    Don't attach any javascript function to the search click event. Instead handle the event on the postback like you usually do. In the postback handler add a call to a javascript function that opens a window.

    Add Page.RegisterStartupScript("<script language=\"JavaScript\">OpenMyWindow();</script>"); in your btnClick handle
    method

    When your aspx page re-renders on the browser after postback it will execute your Javascript function

    HTH
    Suresh

    ----- Carlo Marchesoni wrote: ----

    I have a button that opens a new Window (some kind of search-window), which is fired using JavaScript (btnSearch.Attributes["onclick"]=".....";)
    Now I need to run some code behind code BEFORE this JavaScript runs. I tried to define another Button with an EventHandler associated, and from this (after doing my code) fire 'btnSearch.click()'. But I can't make it work.
    I tried
    private void Button1_Click(object sender, System.EventArgs e

    RestartAll()
    Response.Write("<script>window.document.forms[0].elements['btnSearch'].click();</script>")

    but unfortunately the browser says that this element is not valid
    Am I on the right way or are there better options to do what I try to accomplish
    Thanks for any help
     
    =?Utf-8?B?U3VyZXNo?=, Feb 10, 2004
    #2
    1. Advertising

  3. Carlo,

    I have a little bit of sample code on my website, www.aboutfortunate.com,
    that might be just what you need. It shows how to make the "Body" tag of the
    page into a server side control. Once this is done you can add a javascript
    to the Body tag so that when a page loads that script fires. This technique
    would do exactly what you need.

    To find the sample code click the "Code Library" link in top right corner of
    the site and then use the "Search" box there to search for something like:
    "javascript on page load"

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    Free code library at:
    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzche


    "Suresh" <> wrote in message
    news:...
    > Carlo,
    > In order to do what you want you can call a Javascript function to load a

    new window after you handled the post back event of your search click
    button.
    >
    > Don't attach any javascript function to the search click event. Instead

    handle the event on the postback like you usually do. In the postback
    handler add a call to a javascript function that opens a window.
    >
    > Add Page.RegisterStartupScript("<script

    language=\"JavaScript\">OpenMyWindow();</script>"); in your btnClick handler
    > method.
    >
    > When your aspx page re-renders on the browser after postback it will

    execute your Javascript function.
    >
    > HTH,
    > Suresh.
    >
    > ----- Carlo Marchesoni wrote: -----
    >
    > I have a button that opens a new Window (some kind of search-window),

    which is fired using JavaScript (btnSearch.Attributes["onclick"]=".....";).
    > Now I need to run some code behind code BEFORE this JavaScript runs.

    I tried to define another Button with an EventHandler associated, and from
    this (after doing my code) fire 'btnSearch.click()'. But I can't make it
    work.
    > I tried:
    > private void Button1_Click(object sender, System.EventArgs e)
    > {
    > RestartAll();
    >

    Response.Write("<script>window.document.forms[0].elements['btnSearch'].click
    ();</script>");
    > }
    > but unfortunately the browser says that this element is not valid.
    > Am I on the right way or are there better options to do what I try to

    accomplish ?
    > Thanks for any help
     
    S. Justin Gengo, Feb 10, 2004
    #3
  4. Hi Carlo,


    Thanks for posting in the community!
    From your description, currently you add a clientside "onclick" event for a
    server button which will open a ne w browser window. However, now you'd
    like to do some serverside operations before execute the clientside
    script(open a browser window) yes?
    If there is anything I misunderstood, please feel free to let me know.

    As for this question, I quite agree to Suresh's suggestion that we can
    register the clientside script(open a new browser window) in the certain
    button's serverside click even handler after you've finished some certain
    operations. Then, after the page turns back to client, the new browser
    window will be opened. For example, the button's server click event is as
    below:
    private void btnSearch_Click(object sender, System.EventArgs e)
    {
    //do some operations here

    string script = "<script
    language=\"JavaScript\">window.open('http://www.google.com');</script>";
    Page.RegisterStartupScript("tempscript",script);
    }


    To make it clearly, I've made a sample page to show this means, you may
    have a look if you feel anything unclear above:
    -------------------aspx page------------------
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm2</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5"
    name="vs_targetSchema">
    <script language="javascript">
    function checkField()
    {
    if(document.all("txtMain").value.length>4)
    {
    document.all("btnPostBack").click();
    }
    }
    </script>
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <table width="500" align="center">
    <tr>
    <td><FONT face="ËÎÌå"></FONT></td>
    </tr>
    <tr>
    <td><FONT face="ËÎÌå">
    <asp:Button id="btnSearch" runat="server"
    Text="Search"></asp:Button></FONT></td>
    </tr>
    </table>
    </form>
    </body>
    </HTML>


    -----------------code behind class --------
    public class WebForm2 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button btnSearch;

    private void Page_Load(object sender, System.EventArgs e)
    {
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    InitializeComponent();
    base.OnInit(e);
    }

    private void InitializeComponent()
    {
    this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
    this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion



    private void btnSearch_Click(object sender, System.EventArgs e)
    {
    //do some operations here
    Response.Write("<br>Page is posted back at: "+
    DateTime.Now.ToLongTimeString());
    string script = "<script
    language=\"JavaScript\">window.open('http://www.google.com');</script>";
    Page.RegisterStartupScript("tempscript",script);
    }


    }
    ---------------------------------------------

    Please check out the above things. If you have any further quesitons or
    need any help, please feel free to post here.



    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Feb 11, 2004
    #4
  5. A lot of thanks to all of you - it works great.
     
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 11, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Purvi T
    Replies:
    0
    Views:
    864
    Purvi T
    Oct 19, 2004
  2. Amy
    Replies:
    0
    Views:
    998
  3. Nathan Sokalski
    Replies:
    0
    Views:
    1,077
    Nathan Sokalski
    Jan 15, 2009
  4. MU
    Replies:
    0
    Views:
    569
  5. Replies:
    0
    Views:
    341
Loading...

Share This Page