Solved

Using ClientScript.RegisterStartupScript

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

752 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