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

Discussion in 'ASP .Net' started by =?Utf-8?B?TWlrZSBSYW5k?=, Jan 30, 2006.

  1. 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
     
    =?Utf-8?B?TWlrZSBSYW5k?=, Jan 30, 2006
    #1
    1. Advertising

  2. 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
    >
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jan 30, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shawn Mehaffie
    Replies:
    4
    Views:
    685
    Shawn Mehaffie
    Jan 22, 2004
  2. nick
    Replies:
    3
    Views:
    3,216
    Eliyahu Goldin
    Dec 15, 2004
  3. Magnus Blomberg
    Replies:
    3
    Views:
    728
    Magnus Blomberg
    Apr 14, 2005
  4. Replies:
    1
    Views:
    938
    Sudsy
    Dec 8, 2004
  5. Zoe Hart
    Replies:
    1
    Views:
    383
    Scott Wisniewski
    Jan 8, 2004
Loading...

Share This Page