passing javascript variables to asp.net

Hi...all experts.

I want to passing javascript variables to asp.net (C#)

Here are my code.

      string xmlURL;
      string javascript_output_url;

    protected void Page_Load(object sender, EventArgs e)
    {
            string SearchWord=Server.UrlEncode("Some Word");
   
            Type cstype = this.GetType();
            string cskey1 = "PopupScript";
            string csname1 = "<script type='text/javascript'>"
                        + "var SearchString='<%=SearchWord %>';"
                        + "var api_call_url5='http://www.test.com/search/news?&q=" + SearchString + "&result=5&start=1&output=xml&sort=date&condition=all';"
                        + "var sigalg='1111';"
                        + "var sigkey='2222';"
                        + "signed_api_url5 = daumOpenAPI_Sign_URL ( api_call_url5, sigalg, sigkey );"
                        + "window.open(signed_api_url5);"
                        + "</script>";


            ClientScript.RegisterStartupScript(cstype, cskey1, csname1);
            
            xmlURL= "***I want the get URL from JavaScript Value which is signed_api_url5***";

      }
coverboyAsked:
Who is Participating?
 
Michel PlungjanIT ExpertCommented:
And Austin Powers wanted "...a toilet seat made of solid gold, but it's not in the cards..."

Your server cannot read a JS var unless you SEND it to it using a request.

Ajax is the thing you want. http://asp.net/ajax/
0
 
Dirar Abu KteishCommented:
You can add the value of xmlURL in a hidden field before submitting or you can use ajax, you can also send as querystring with the URL
0
 
coverboyAuthor Commented:
Hi....dxz2...

How can I use hidden field?
I'm new to asp.net
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Dirar Abu KteishCommented:
are you submitting the page? Or you want to pass the variable on the first page open?
0
 
coverboyAuthor Commented:
hi...dxz2

I want First Page Open...not submitting page
0
 
Dirar Abu KteishCommented:
but then the Javascript won't yet get executed. When you open a page, the Server side language such as Asp.net is executed. maybe you want to pass variables from C# to javascript? can u explain what is the xmlURL?
0
 
coverboyAuthor Commented:
xmlURL is a string to get XML Contents.

and then, I want to re-orginize xml contents to html...
But this part(re-orginizing) is not my concern for this question.

Simpley, you only focus "api_call_url5" value...
If "signed_api_url" value is confused...

This means I want javascript api_call_url5 value to xmlURL String...OK?
0
 
Dirar Abu KteishCommented:
It goes like this:
First you request a page, then the server side code is compiled then the page is returned to client side and only after that is the javascript code executed, first server side code (ASP.Net) then Client side script(Javascript). As I said, because asp.net is executed before javascript u can set javascript variables.

If u want to set c# variable javascript u have to send parameters to the page in the url or in the form or use Ajax.

to set javascript variables from asp.net as an example I did this:
in the head tags I worte:
<script type="text/javascript">
        var myVar = '<%Page.Response.Write("123");%>';
</script>
then when you display the source u'll see:
<script type="text/javascript">
        var myVar = '123';
</script>

no you can put any value instead of the 123.

another way to do it is like this, in the Page_Load of your page write:
string myCSharpVar = "hello";
string popupScript = "<script language='javascript'>" +
                            "var myVariable ='" + myCSharpVar + "';</script>";
Page.RegisterStartupScript("PopupScript", popupScript);

From javascript to .net take a look at these links:
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_20777800.html
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_22700755.html
http://www.thescripts.com/forum/thread91051.html

-dirar
0
 
Dirar Abu KteishCommented:
you can do this:

I click on a link like this:
<a href="javascript:getMyXML();">Get Xml</a>

in javascript:
var api_call_url5 = 'myxmlfile.xml'
function getMyXML(){
 location.href = 'mypage.asp?' + api_call_url5;
}

in C#:
protected void Page_Load(object sender, EventArgs e)
{
 xmlURL=  Page.Request.QueryString("api_call_url5");
}

0
 
coverboyAuthor Commented:
Hum....That's not what I want.

Your Instruction said Link to another page, or need postback.

But, I want to read javascript variables without any postback, or any clicking links.
0
 
Dirar Abu KteishCommented:
mplungjan is right,
I gave you all the possible ways to communicate with javascript. U can't get a value from javascript in the server side because at the server side javascript is just a text. If server side and client side communicated easily together they anyone can easly log to your database or delete all the files on your computer.
How do you set the value of api_call_url5? I am sure you are setting it with Javascript, and what do you do after setting it with Javascript? you want to re-orginize xml contents to html! Then use Ajax, it's very easy, and you don't need to refresh your page to get to Server Side..

A lot of javascript Ajax clients are available to make your life easier, check the link mplungjan posted to know about Ajax, also see mootools.net and http://aspnetresources.com/blog/prototype_vs_atlas.aspx.
I use mootools because of the cools effects and cross browser support

-dirar
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.