RegisterStartupScript written in a button click server event not working.

Discussion in 'ASP .Net Building Controls' started by swapna.munukoti@gmail.com, Apr 23, 2007.

  1. Guest

    Hi all,

    To my knowledge we can execute the javascript any where using
    RegisterStartupScript method. I have written the same javascript code
    in button click event and tried to execute using RegisterStartupScript
    metod in a composite custom control. I have written code to open
    another aspx page in that javascript.

    It didnt opened the page. But when debugging the code, it was going
    thru that code.

    If i execute the samne code using addattribute to that button control,
    its displaying the page as expected.

    Am I missing some thing.

    Here is the code I was trying to execute:
    void ctrlButton_Click(object sender, EventArgs e)
    {
    string strButtonClickScript = "javascript:var WinSettings
    = 'center:yes;resizable:no;dialogHeight:300px'; ";
    strButtonClickScript = strButtonClickScript + "var ParmA
    =''; ";
    strButtonClickScript = strButtonClickScript +
    "window.showModalDialog('http://localhost:4620/WebSite8/
    Default3.aspx', ParmA, WinSettings)";
    Page.ClientScript.RegisterStartupScript(this.GetType(),
    scriptKey, strButtonClickScript);
    }
    The code thatz working:
    protected override void AddAttributesToRender(HtmlTextWriter writer)
    {
    writer.AddAttribute(HtmlTextWriterAttribute.Type,
    "ctrlButton");
    writer.AddAttribute("language", "javascript");
    string strButtonClickScript = "javascript:var WinSettings =
    'center:yes;resizable:no;dialogHeight:300px'; ";
    strButtonClickScript = strButtonClickScript + "var ParmA
    =''; ";
    strButtonClickScript = strButtonClickScript +
    "window.showModalDialog('http://localhost:4620/WebSite8/
    Default3.aspx', ParmA, WinSettings)";
    writer.AddAttribute("onclick", strButtonClickScript);
    }


    Thanks in Advance,
    Swapna.
     
    , Apr 23, 2007
    #1
    1. Advertising

  2. RE: RegisterStartupScript written in a button click server event not w

    swapna,

    In the first example, you register the javascript and it should appear on
    your page - you can check that by viewing the page source in the browser.
    A few points worth mentioning here:
    - if you just need to open a modal dialog - there is no need to make a
    round trip to the server, and you will want to employ your working example
    (attaching javascript to button client click)
    - if you need to visit your server (say for validation; to get some
    parameters based on the user input, etc), then you will need to correct your
    server-side code:
    - in strButtonClickScript - remove "javascript:" at the start of the string
    - in RegisterStartupScript add fourth parameter (true) to wrap your
    javascript in "script" tags, so that the browser can read it

    HTH

    "" wrote:

    > Hi all,
    >
    > To my knowledge we can execute the javascript any where using
    > RegisterStartupScript method. I have written the same javascript code
    > in button click event and tried to execute using RegisterStartupScript
    > metod in a composite custom control. I have written code to open
    > another aspx page in that javascript.
    >
    > It didnt opened the page. But when debugging the code, it was going
    > thru that code.
    >
    > If i execute the samne code using addattribute to that button control,
    > its displaying the page as expected.
    >
    > Am I missing some thing.
    >
    > Here is the code I was trying to execute:
    > void ctrlButton_Click(object sender, EventArgs e)
    > {
    > string strButtonClickScript = "javascript:var WinSettings
    > = 'center:yes;resizable:no;dialogHeight:300px'; ";
    > strButtonClickScript = strButtonClickScript + "var ParmA
    > =''; ";
    > strButtonClickScript = strButtonClickScript +
    > "window.showModalDialog('http://localhost:4620/WebSite8/
    > Default3.aspx', ParmA, WinSettings)";
    > Page.ClientScript.RegisterStartupScript(this.GetType(),
    > scriptKey, strButtonClickScript);
    > }
    > The code thatz working:
    > protected override void AddAttributesToRender(HtmlTextWriter writer)
    > {
    > writer.AddAttribute(HtmlTextWriterAttribute.Type,
    > "ctrlButton");
    > writer.AddAttribute("language", "javascript");
    > string strButtonClickScript = "javascript:var WinSettings =
    > 'center:yes;resizable:no;dialogHeight:300px'; ";
    > strButtonClickScript = strButtonClickScript + "var ParmA
    > =''; ";
    > strButtonClickScript = strButtonClickScript +
    > "window.showModalDialog('http://localhost:4620/WebSite8/
    > Default3.aspx', ParmA, WinSettings)";
    > writer.AddAttribute("onclick", strButtonClickScript);
    > }
    >
    >
    > Thanks in Advance,
    > Swapna.
    >
    >
     
    Sergey Poberezovskiy, May 16, 2007
    #2
    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:
    930
    Purvi T
    Oct 19, 2004
  2. jorge
    Replies:
    2
    Views:
    1,348
    jorge
    May 26, 2004
  3. gce
    Replies:
    2
    Views:
    3,308
  4. Amy
    Replies:
    0
    Views:
    1,035
  5. Max2006
    Replies:
    2
    Views:
    1,639
    Walter Wang [MSFT]
    Sep 19, 2007
Loading...

Share This Page