?
Solved

passing javascript variables to asp.net

Posted on 2007-07-29
13
Medium Priority
?
2,379 Views
Last Modified: 2012-08-13
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***";

      }
0
Comment
Question by:coverboy
12 Comments
 
LVL 6

Expert Comment

by:Dirar Abu Kteish
ID: 19590378
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
 

Author Comment

by:coverboy
ID: 19590401
Hi....dxz2...

How can I use hidden field?
I'm new to asp.net
0
 
LVL 6

Expert Comment

by:Dirar Abu Kteish
ID: 19590429
are you submitting the page? Or you want to pass the variable on the first page open?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:coverboy
ID: 19590475
hi...dxz2

I want First Page Open...not submitting page
0
 
LVL 6

Expert Comment

by:Dirar Abu Kteish
ID: 19590539
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
 

Author Comment

by:coverboy
ID: 19590558
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
 
LVL 6

Expert Comment

by:Dirar Abu Kteish
ID: 19590651
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
 
LVL 6

Expert Comment

by:Dirar Abu Kteish
ID: 19590675
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
 

Author Comment

by:coverboy
ID: 19591649
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
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 1000 total points
ID: 19591823
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
 
LVL 6

Assisted Solution

by:Dirar Abu Kteish
Dirar Abu Kteish earned 1000 total points
ID: 19592751
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
 
LVL 1

Expert Comment

by:Computer101
ID: 20475802
Forced accept.

Computer101
EE Admin
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

864 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