Using Hidden Variables in ASP.NET

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 175
Reputation: Letscode is an unknown quantity at this point 
Solved Threads: 6
Letscode's Avatar
Letscode Letscode is offline Offline
Junior Poster

Using Hidden Variables in ASP.NET

 
0
  #1
Jun 20th, 2005
I'm struck in a problem here.I have server side code that has to collect values from the client side code(Javascript).The Javascript code basically gets the frame height and width and the server side code(VB) has to collect the values and resize the control(lets say Image).I used Query string and it works but since i pass values ,the page is getting reloaded infinitely.I DONT WANT TO USE QUERY STRING..

Workaround:
1.I can create a server side variable something like <%var1%>..I did try that but I dont know for some reason it did not work..

2.I'm thinking of using hidden variables but I'm not sure how it works..

If somebody give me an example or an explanation or a workaround,it will be great...
Save White Tiger
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Using Hidden Variables in ASP.NET

 
0
  #2
Jun 20th, 2005
Hidden variables would work. The ASP.NET ViewState mechanism is just a hidden variable.

In HTML, you simply create:

<input type="hidden" id="myHiddenVar">

To set it's value in javascript:

document.getElementById("myHiddenVar").value = "myValue"

In ASP.NET code-behind, you use the Request object to retrieve the value.

So many ASP.NET programmers waste hours trying to find the "ASP.NET" way to do things, even though the "right" way has been around since the web's inception. Whenever I'm confronted with the stereotypical ASP.NET-induced brain-lock, I try to clear my mind of all Microsoft knowledge (I call it "blue fuzz"), and ask how I'd do this with just regular HTML/JavaScript. That usually produces an answer. Barring that, I try to use Request/Response objects on the server, ViewState variables or Session variables, and only as a last resort look at dynamic ASP.NET Web Controls.

My site has a view articles on ASP.NET topics that might be helpful. Of course keep ASP.NET discussions right here on DaniWeb.
Last edited by tgreer; Jun 14th, 2006 at 2:06 pm. Reason: added inline code tags
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 175
Reputation: Letscode is an unknown quantity at this point 
Solved Threads: 6
Letscode's Avatar
Letscode Letscode is offline Offline
Junior Poster

Re: Using Hidden Variables in ASP.NET

 
0
  #3
Jun 23rd, 2005
Hey Thomas,
Thanks for your reply.Sorry I did not get back to you soon,things have really got busy here.Read your article on Viewstate ,It was really helpful..

Yes,the hidden variable works,I tested it..

But Its not working on page load nor init()... I put a button and it worked fine on postback...
Not sure why?

But my application is now working.I did a work around to force the event and it works...

PS:I'll remember your advice on Bluefuzz...

Originally Posted by tgreer
Hidden variables would work. The ASP.NET ViewState mechanism is just a hidden variable.

In HTML, you simply create:

<input type="hidden" id="myHiddenVar">

To set it's value in JavaScript:

document.getElementById("myHiddenVar").value = "myValue"

In ASP.NET code-behind, you use the Request object to retrieve the value.

So many ASP.NET programmers waste hours trying to find the "ASP.NET" way to do things, even though the "right" way has been around since the web's inception. Whenever I'm confronted with the stereotypical ASP.NET-induced brain-lock, I try to clear my mind of all Microsoft knowledge (I call it "blue fuzz"), and ask how I'd do this with just regular HTML/JavaScript. That usually produces an answer. Barring that, I try to use Request/Response objects on the server, ViewState variables or Session variables, and only as a last resort look at dynamic ASP.NET Web Controls.

My site has a view articles on ASP.NET topics that might be helpful. Of course keep ASP.NET discussions right here on DaniWeb.
Save White Tiger
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Using Hidden Variables in ASP.NET

 
0
  #4
Jun 23rd, 2005
It probably doesn't work on Page Load, because you have no hidden variable until you click the button. Without seeing your code, I can't be sure.

But always realize, that the controls on the page, manipulated in the browser, using JavaScript, don't automatically correspond to ASP.NET server-side web controls.

It only appears that way! If you drag a textbox onto a form in Visual Studio, you are creating a "TextBox" Server control. When the ASP.NET code runs, that control CREATES or RENDERS an HTML <input> element. They are two separate things.

When you postback the page, IIS/ASP.NET RECREATEs the TextBox control, every time, and then digs back through the HTTP headers to find the <input> element and synch the two back together.

That's all wonderful, until you try to do any standard JavaScript control manipulation, such as dyamically creating hidden elements or setting values. It's like ruining a magician's beautiful card trick by shuffling the deck before giving it back to him.

Two other approaches for you to consider:

1) Set your own ViewState variables. ViewState.Add(). Server-side, set or get the value of your viewstate variable, and based on that value, Register a Script (Page.RegisterStartupScript) that contains your JavaScript. In other words, dynamically create your JavaScripts, server-side, that will run once the page is served back to the user.

2) Use Session variables.

Thanks for the words of appreciation.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 175
Reputation: Letscode is an unknown quantity at this point 
Solved Threads: 6
Letscode's Avatar
Letscode Letscode is offline Offline
Junior Poster

Re: Using Hidden Variables in ASP.NET

 
0
  #5
Jun 24th, 2005
I got a quick doubt,
What exactly does Page.RegisterStartupScript do?
My guess is that..
It executes the Javascript code which is declared as a string on the server side!!!!!

Questions
1.Does Postback have anything to do with Page.RegisterStartupScript..

2.Is this the was to force a Javascript to execute from the server..
Save White Tiger
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Using Hidden Variables in ASP.NET

 
0
  #6
Jun 24th, 2005
Repeat this to yourself 17 times in a row:

1. ASP.NET is Server side.
2. JavaScript is Client side.


JavaScript cannot execute on the server. Page.RegisterStartupScript() is extremely simplistic. All it does is add whatever is in your second parameter, as text, to the bottom of the response stream. So it's serving your page, right? And the last thing it puts on the page, is the string in RegisterStartupScript.

If that string is in the form "<script>alert('Hello');</script>", then what will happen? When that page loads in the users browser, that script will run, and "Hello" will popup. The script ran ON THE CLIENT. However, that script was put onto the page, via ASP.NET.

What does this have to do with PostBack? Absolutely nothing at all.

Now, go back and read those first two points aloud 17 more times.

And for further reading on the topic:
Integrating client-side scripts with ASP.NET.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 175
Reputation: Letscode is an unknown quantity at this point 
Solved Threads: 6
Letscode's Avatar
Letscode Letscode is offline Offline
Junior Poster

Re: Using Hidden Variables in ASP.NET

 
0
  #7
Jun 25th, 2005
Thanks
I'm learning a lot from you...
Save White Tiger
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 175
Reputation: Letscode is an unknown quantity at this point 
Solved Threads: 6
Letscode's Avatar
Letscode Letscode is offline Offline
Junior Poster

Re: Using Hidden Variables in ASP.NET

 
0
  #8
Jun 25th, 2005
Regarding the first question..
1.Postback...thanks it has nothing to do with Registerstartupscript..

I think you misconstrued my second question...

2.Is this the was to force a Javascript to execute from the server

I asked

2.Is this the was(WAY) to force a Javascript to execute FROM the server.(ASP.NET)

I did not mention ON the server side.Perhaps I should be little more specific in my question by mentioning something like
Is this the way to force a Javascript to execute from the server(ASP.NET) on
Client side..
..Sorry my fault..

So I think the answer is YES.

Thomas, you gave a good explanation.You moderators(Yourself and Paladine) are doing a great Job.I appreciate that.I can understand how annoying it feels when people ask something ambiguous and doesnt make sense.No two people think the same things and there is no way for you guys to know whats happening on the user side..

From next time I'll make my questions clear and unambiguous.. :cheesy:

Have a great weekend..
Save White Tiger
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Using Hidden Variables in ASP.NET

 
0
  #9
Jun 25th, 2005
Well, in that case, yes... RegisterStartupScript is ONE WAY to insert a script into the response stream. The script will execute as soon as the browser interprets it, because it isn't placed into the document HEAD nor as part of a function definition. The main point here is that the user's browser executes the script. Your ASP.NET code just puts the script on the page.

There are other ways to assign scripts to certain client-side events. The technique I like to use is to call the "Attributes.Add()" method of specific server controls.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1
Reputation: bendragon is an unknown quantity at this point 
Solved Threads: 0
bendragon bendragon is offline Offline
Newbie Poster

Re: Using Hidden Variables in ASP.NET

 
0
  #10
Oct 2nd, 2008
Try this:
<input runat="server" type="hidden" id="myHiddenVar">

With the Runat="server" you will be able to see the myHiddenVar id in the code-behind!

<anything runat="server" id="something" />

With <a runat="server"></a> you can set the href from the code-behind


1. ASP.NET is Server side.
2. JavaScript is Client side.


Today, this rule is obsolete ehehehe
Last edited by bendragon; Oct 2nd, 2008 at 11:35 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC