Solved

Using ClientScript.RegisterStartupScript

Posted on 2007-11-17
2
5,107 Views
Last Modified: 2012-06-27
Greetings:
 
I've been trying to sort this out for a while, and have decided to get help...
 
The problem is that I'd like to create a script that requires an argument. My approach has been to use ClientScript.RegisterStartupScript as follows:
 
  string ls_scriptString ;

  scriptString = "<script language='javascript'> function MyJScript(SomeText)" +
     "{ alert(SomeText); return true;}</script>";

  Page.ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", ls_scriptString , false);
  iButn_DoIt.Attributes.Add("onClick", "return MyJScript('Hello World')");
 
The above works if I don't use the SomeText argument. Otherwise I get an runtime error (")" expected on the click event).

How do I set this up so that the script can receive arguments?

Thanks
0
Comment
Question by:RTQ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
McExp earned 250 total points
ID: 20305670
Your code needs to use "RegisterClientScriptBlock" instead of "RegisterStartupScript", see below for an example
<%@ Page Language="C#" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
    protected void Page_Load(object sender, EventArgs e)
    {
        string scriptString = "function MyJScript(SomeText) { alert(SomeText); return true;}";
 
        ClientScript.RegisterClientScriptBlock(this.GetType(), "ScriptBlock", scriptString, true);
 
        iButn_DoIt.Attributes.Add("onClick", "return MyJScript('Hello World')");
    }
    
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:LinkButton runat="server" ID="iButn_DoIt" Text="Testing" />
    </div>
    </form>
</body>
</html>

Open in new window

0
 

Author Comment

by:RTQ
ID: 20306197
It works great - thanks for the help
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question