PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

Adding Server Side code in a Client Side Script???

 
 
=?Utf-8?B?TWlrZSBSYW5k?=
Guest
Posts: n/a
 
      30th Jan 2006
Hi,
I am trying to add some server side code in some Client Side Script. I can
get it to work properly with the code embedded directly in the .aspx page.
However, if I try to use the Page.ClientScript.RegisterClientScriptBlock I
get an error and the code does not execute.

Here is an example:

protected void Page_Load(object sender, EventArgs e)
{
FileStream strm = File.OpenRead("<<Path to Script goes here>>");
StreamReader rdr = new StreamReader(strm);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OCL",
rdr.ReadToEnd());
}

-- Script in File. --
<script language="javascript" type="text/javascript">
function GetClientIPAddress()
{
alert("<%
Response.Write(Request.ServerVariables["REMOTE_ADDR"].ToString()); %>");
}
<script/>

Any ideas?
Thanks,
- Mike

 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      30th Jan 2006
Mike,
if you view source on your generated page, you wll see that "what you did is
what you get":

<script language="javascript" type="text/javascript">
function GetClientIPAddress()
{
alert("<%
Response.Write(Request.ServerVariables["REMOTE_ADDR"].ToString()); %>");
}
<script/>

The Solution is to get the REMOTE_ADDR string in your server side code, and
add this as a string literal rather than in server-side script delimiters to
the client script you add to the page, e.g.:

string remoteIp=Request.ServerVariables["REMOTE_ADDR"].ToString());

then,

string scr=@"<script language=\"javascript\" type=\"text/javascript\">
function GetClientIPAddress()
{
alert(" +remoteIp +");
}
<script/>";

then RegisterClientScriptBlock with string "str"




HTH,
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




"Mike Rand" wrote:

> Hi,
> I am trying to add some server side code in some Client Side Script. I can
> get it to work properly with the code embedded directly in the .aspx page.
> However, if I try to use the Page.ClientScript.RegisterClientScriptBlock I
> get an error and the code does not execute.
>
> Here is an example:
>
> protected void Page_Load(object sender, EventArgs e)
> {
> FileStream strm = File.OpenRead("<<Path to Script goes here>>");
> StreamReader rdr = new StreamReader(strm);
> Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OCL",
> rdr.ReadToEnd());
> }
>
> -- Script in File. --
> <script language="javascript" type="text/javascript">
> function GetClientIPAddress()
> {
> alert("<%
> Response.Write(Request.ServerVariables["REMOTE_ADDR"].ToString()); %>");
> }
> <script/>
>
> Any ideas?
> Thanks,
> - Mike
>

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Server-side script with input parameter from Client-side script Magnus Blomberg Microsoft ASP .NET 3 14th Apr 2005 01:21 PM
execute client side script + server side code for button click =?Utf-8?B?a2s=?= Microsoft C# .NET 1 17th Mar 2005 12:20 AM
Problem in adding template of server side control using client side scripts Lau Lei Cheong Microsoft ASP .NET 1 13th Oct 2004 11:41 AM
Calling a client side script from server side. =?Utf-8?B?RHVzdGluIElJLg==?= Microsoft ASP .NET 3 16th Aug 2004 11:10 PM
Help Needed!!! client-side script, server-side code Shawn Mehaffie Microsoft ASP .NET 4 22nd Jan 2004 05:02 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:46 AM.