Solved

Using ClientScript.RegisterStartupScript

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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