Solved

Using ClientScript.RegisterStartupScript

Posted on 2007-11-17
2
5,102 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

Need Help in Real-Time?

Connect with top rated Experts

29 Experts available now in Live!

Get 1:1 Help Now