RegisterStartupScript written in a button click server event not working.

S

swapna.munukoti

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.
 
S

Sergey Poberezovskiy

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top