Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing server-side data from the client-side 1

Status
Not open for further replies.

dpdoug

Programmer
Nov 27, 2002
455
US
Is there any possible way to put a string of data into a regular html text box from a code behind module? I don't seem to be able to access server controls from client-side script. I've tried putting text into html controls from a code behind page, but I can't get that to work either.

or...

Is it possible to populate a variable from a code-behind module and access that variable from client side scripting?

or...

Can I access a session variable from client side scripting?

I've been pulling my hair out over this one for several days and trying everything to access a single string of data from the client side. I've tried all kinds of things.

Could someone please give me a good suggestion for doing this? I've done all kinds of searches here and have not been able to come up with anything.

I'd be really greatful for any help. Thanks

David
 
You can access HTML controls from the server by giving them the runat="server" attribute e.g.
Code:
<input type="text" id="myHtmlTextbox" runat="server" />
then in your code-behind (or server script) you can access the control using
Code:
protected HtmlInputText myHtmlTextbox;

myHtmlTextbox.Value = "some value to access on the client";
And then using javascript on the client you could access the value using (assumes DOM compliant browser)
Code:
var textboxValue = document.getElementById["myHtmlTextbox"].value;
The same principle can be applied to any Html element. You can even access and manipulate elements such as <title> from your server code by giving them the type HtmlGenericControl.

Hope this helps

Rob



Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Thanks Bert,
That really, really helps!

One more question, Bert, if you don't mind...

If I store a value in a session variable, is there a way to access it from client side script? Or is that variable only accessable on the server?

also,

I have tried to pass an array to a client side script, but I get a type missmatch error, I would suppose because all scripting variables are variants and .net doesn't support variants. Is there any work around for that?

What I finally ended up doing was sending a long string to the page and then I used the Split function to convert it into an array.

David
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top