Solved

Hot to access codebehind string from client side javascript

Posted on 2006-11-30
8
2,869 Views
Last Modified: 2008-01-16
in my code behind i have
string strname="Jack";

I want to access this string in my javascript function,how can i do it.I know how to do a control but not a string,Can you please help.

Thanks,
0
Comment
Question by:zameer21
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 12

Accepted Solution

by:
enachemc earned 250 total points
ID: 18049063
var strname = '<%=strname%>';
0
 
LVL 8

Expert Comment

by:tncbbthositg
ID: 18049186
What do you mean access it?  If you want to change it, you're going to have to pass the value back in a form.  If you only need to read it for something, then enachemc's solution will work.

TNC
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 18050935
I think that enachemc has the right idea.  It sounds like you have a string variable on your server that you would like to make available on your client.

? Is this correct?
? In what language is your server code written?
? Do you know if your server program is generating HTML?
0
 

Author Comment

by:zameer21
ID: 18057311
Sorry about the delay in responding the code behind is in C# and i want to access it in Javascript.
Let me be more clear.
if there is a textbox name txtName,You can access the value in javascript as document.getelementbyid("txtName").value.
In the same way i have string in C# code behind
string strName="dssfdfdsfdsfd";

I want to access this in javascrpt function,How can i do it,without sending it as parameter.

Sorry once again for late response.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:tncbbthositg
ID: 18057347
You can't.  If you want to change the value "on the server," you'll have to pass it through the request.  I'd like to just say that this is my beef with ASP.NET.  People who are used to application development use ASP.NET and think that there's somehow a syncronous connection with the server . . . which there isn't.  Zameer ... think of ASP.NET as a procedural language because that's how it gets executed.  Sure, .NET hides that fact very gracefully, but the fact remains that a request comes in, the server builds HTML and sends it back.  That's it.  Then, the connection is broken.  If you then want to interact with the codebehind again, a new connection has to be made.  That's assuming, of course, that I understand what you're asking.  The others feel like you're saying, "how can I print a variable from C#."  I think you're saying, "how can I use JS to change the value of a variable in the code behind."  Well, you can't.  Not without passing the value back to the server through a request object.  Keep in mind, unless you store it as a session variable, as a hidden form element, or in the view state, the next time a request is initiated, that variable will be reset because web pages function asyncronously.

TNC
0
 
LVL 12

Expert Comment

by:enachemc
ID: 18059283
as I said: var strname = '<%=strname%>';
the var strname part is JS.

The <%=strname%> will read the variable from C#
0
 
LVL 8

Expert Comment

by:tncbbthositg
ID: 18061119
enachemc, I think what he's looking for is . . .


<script type="text/javascript">
  strname = 'A new Value';
</script>



Such that in the codebehind . . .

if (strname = "A new Value")
{
  Response.write("I accessed a codebehind variable from javascript");
}


TNC
0
 

Author Comment

by:zameer21
ID: 18061397
thanks for everybody for your help,but enachemc had it. Actually i was acessinmg this variable on .js file which is lined to the aspx page,wheni call it directly,it does not bring the value.
I had to assign a variable to the string on the aspx page and then access the variable in the js file.I cant access it directly from js file.

Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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…

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now