views:

140

answers:

2

Hello,

I am using page.registerstartupscript in my code behind C#. Its like this:

string item1="category1"; string Script = "window.program = '" + item1 + "';"; Page.RegisterStartupScript("PopupScript", Script);

item1 gets generated dynamically from some value in code behind. But its not working. Any suggestoins where I m getting wrong?

A: 

You need it wrapped in script tags, there's an overload for this in the newer replacement ClientScript:

string item1="category1";
string Script = "window.program = '" + item1 + "';";
Page.ClientScript.RegisterStartupScript(GetType(), "PopupScript", Script, true);

Unless you're on .Net 1.1 I believe, Page.RegisterStartupScript is deprecated.

Nick Craver
I tried what you said....but I m facing some problem ...please look at my answer below.
AB
@AB - That's normal, something **else** is registering that script for output. To not repeat `<script></script>` for every single statement, ASP.Net batches them all together.
Nick Craver
I am not adding <script> tag in it..but its adding on its own and also this var _wpmExportWarning is coming attached to it...any idea how to remove it...or its standard...
AB
@AB - The `,true)` at the end tells it to add script tags. I have no idea what's adding the other bits, it's some other control on your page. Unless you delete that control it's going to show up. What's it hurting by being there? It's going to execute the same way whether it's in 1 set of script tags or two, so may as well not waste bandwidth with 2 sets.
Nick Craver
A: 

I did what you were saying but I m getting something like this when I m checking view source on page:

<script type="text/javascript">
//<![CDATA[
var __wpmExportWarning='This Web Part Page has been personalized. As a result, one or more Web Part properties may contain confidential information. Make sure the properties contain information that is safe for others to read. After exporting this Web Part, view properties in the Web Part description file (.WebPart) by using a text editor such as Microsoft Notepad.';var __wpmCloseProviderWarning='You are about to close this Web Part.  It is currently providing data to other Web Parts, and these connections will be deleted if this Web Part is closed.  To close this Web Part, click OK.  To keep this Web Part, click Cancel.';var __wpmDeleteWarning='You are about to permanently delete this Web Part.  Are you sure you want to do this?  To delete this Web Part, click OK.  To keep this Web Part, click Cancel.';window.program = 'category1';//]]>
</script>

but I only need

<script type="text/javascript">
window.program = 'category1';
</script>

Can you tell me how to achieve this

AB