Solved

Using ClientScript.RegisterStartupScript

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

12 Experts available now in Live!

Get 1:1 Help Now