HighTechTalks DotNet Forums  

RegisterStartupScript written in a button click server event not working.

ASP.net Building Controls microsoft.public.dotnet.framework.aspnet.buildingcontrols


Discuss RegisterStartupScript written in a button click server event not working. in the ASP.net Building Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
swapna.munukoti@gmail.com
 
Posts: n/a

Default RegisterStartupScript written in a button click server event not working. - 04-23-2007 , 09:27 AM






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.GetTy pe(),
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.


Reply With Quote
  #2  
Old   
Sergey Poberezovskiy
 
Posts: n/a

Default RE: RegisterStartupScript written in a button click server event not w - 05-15-2007 , 10:27 PM






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

"swapna.munukoti (AT) gmail (DOT) com" wrote:

Quote:
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.GetTy pe(),
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.



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.