Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Adding Server Side code in a Client Side Script???

Reply
Thread Tools

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

 
 
=?Utf-8?B?TWlrZSBSYW5k?=
Guest
Posts: n/a
 
      01-30-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.G etType(), "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
 
      01-30-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.G etType(), "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

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
LinkButton Client side code firing before server side code alexmac262@hotmail.com ASP .Net 1 03-22-2007 06:13 PM
server side code access the text of <asp:label> changed by client-side javascript code? nick ASP .Net 3 12-15-2004 06:26 PM
how client-side presentation code interacts with server-side sorting/filter code? jrefactors@hotmail.com Java 1 12-08-2004 12:30 AM
how client-side presentation code interact with server-side filter/sort processing code? jrefactors@hotmail.com Javascript 0 12-07-2004 11:15 PM
Adding custom client-side onClick handler with client-side validator controls Zoe Hart ASP .Net Web Controls 1 01-08-2004 10:45 PM



Advertisments