Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

creating htmlinputhidden dynamically in code-behind, but values not posting back to client.

Posted on 2006-04-29
18
Medium Priority
?
1,363 Views
Last Modified: 2009-12-16
My aspx page only holds a content tag and an asp: form tag with "name="myform" method="post". Using master page of course.

In the code-behind I'm creating a label object, an htmlinputhidden object, and a radiobutton object.

I don't have the code in front of me while I"m writing this but here's some psuedo-code.


Page_Load
On PostBack take care of form inputs, read the values and process then do below again, if needed
Loop for x records
   Set Label.text
   Set value in htmlinputhidden.value (have also used .attributes.add("value", SomeInt.tostring))
   Loop for RAdioButtons
     Sub Add List Items (radiobuttons.controls.add(New LIst Items)
   End Loop
   Assign ID's to all objects
   myForm.Controls.Add(label)
   myForm.Controls.Add(htmlinputhidden)
   myForm.Controls.Add(radiobutton)
End Loop

The first time the page is sent to the client the values on the hidden field are exactly what they are suppose to be when viewing source. I process those values on return to server during postback, then call the subroutine that gets a new set and displays them. When the subsequent pages are loaded the values are exactly the same as the first page. I have tried putting these in Page_init, prerender, preInit, and everything else. I read the data, then create new objects with new values. The labels.text values change properly, the radio buttons and list items they use hold the correct text and values. What is going on with the htmlinputhidden control ? Everything else works like a charm.

I'm at wits end. I absolutely need those hidden field values. It's like using header and detail records. That value id's my header to the radiobutton detail. Is viewstate messing with this field ? Is there a hidden container I can use to gather this data ? What am I not doing that is not allowing me to use this necessary tool for programming on the web ?

Any help would be a godsend at this point. I'm using vb.net 2.0/asp.net 2.0.

e

0
Comment
Question by:emanstl
  • 8
  • 4
  • 3
15 Comments
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16595180
it makes it a bit difficult to find the problem without the real code.

I am not sure why you would want to use a Hidden field in the first place, could you not just store that value in ViewState or session or something like that?

If you could post the code on exactly how you are setting the field it would help.
Have you tried using a label instead and making it visible = false? Or putting a normal textbox in a div and hiding the div?
0
 
LVL 22

Expert Comment

by:Jason Scolaro
ID: 16595206
Just for the sake of a potentially easy answer, what about using the ViewState object instead of a hidden input control?  Is that possible, or do you need the hidden control to be on the page (for JavaScript reasons or something)?

If you're not opposed to the ViewState object, just try that, replace any references to the hidden input control with:

ViewState("MyKeyName") = value

Let me know if that's helpful or not.

-- Jason
0
 

Author Comment

by:emanstl
ID: 16595364
I was able to get around this issue by dynamically creating textbox controls with height and width = 0 and setting the value.

Picture a survey, that's created dynamically from SQL tables along with their answers(radio options). The admin may setup x amount of questions and y amount of options to those questions. As a programmer I don't hard code these controls, I must dynamically create them in the code-behind as to handle any number of x or y's that may come down the pike. So we go in and grab all the x records. The only thing that is displayed on the x records are labels. Becuase they are questions and we just need to display them. Then we have loop through and create a radio control for the y records, which are the "answers" tied to those questions. Well, you need some mechanism on the call back that tells you what x record goes with what y record selected from the radio selection.

Using a hidden field, with sequential numbered naming conventions allows easy reading of these on postback. So the client source would look something like this.

HiddenField name="ct1_QID1"
radio select name="ct1_AID1"
  option value="1"  
  option value="2"
  option value="3"

HiddenField name="ct1_QID2"
radio select name="ct1_AID2"
  option value="1"  
  option value="2"
  option value="3"

HiddenField name="ct1_QID3"
radio select name="ct1_AID3"
  option value="1"  
  option value="2"
  option value="3"

so on and so forth.. User does his/her thing, I get the postback, process the records then display more if need be using the same sequential numbering scheme.

Works like a charm. Have created some pretty involved dynamic surveys with full administration features that allows for all kinds of admin options.

The textbox trick got it to work but it looks ugly and not very elegant to me.

Using my example how would I use viewstate for this same purpose ?

e
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 15

Expert Comment

by:GavinMannion
ID: 16595568
If you put the answers in hidden controls on the page then a smart user could easily score 100%.

I would strongly recommend against doing this.

Since you are doing a postback why not just check the question/answer against the database where the real answer is?

Just label your textbox with an identifier which matches the question in the database?
0
 

Author Comment

by:emanstl
ID: 16601951
what are you talking about Gavin, aren't you reading my comments ?

First off, there is no 100% score.. secondly, it's not important for you to know the reasoning to what I'm doing. I've got 15 years development experience in several languages covering multiple environments, trust me, conceptually I know what I'm doing. You should be able to understand what I'm doing from my psuedo-code and explanation, you obviously don't. But thanks anyway.

Scolja, do you have any comments on my last post ?
0
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16602610
emansti,

15 Years and you are struggling with a dynamic survey :)

Scolja do you have the answer? I would be interested to see.

Thanks for the appreciation emansti.

I hope you get there in the end.
0
 
LVL 22

Expert Comment

by:Jason Scolaro
ID: 16604736
Well, if you have it working then I'd stick with it.  I'm not sure why the Hidden input control didn't work (which is .NETized in 2.0 with an <asp:HiddenField>, but I'll assume you're still on 1.1).

Another idea to throw at the problem is to basically mimic what DataGrids do with their DataKeys collection, so based on the Row Index, you would have a collection that stores whatever value you need.  So if you wanted to make it extremely easy, let's just say the collection is an ArrayList object and it holds Strings.  Just pass it the RowIndex and grab your value....

Hopefully that makes sense.
-- Jason
0
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16607493
Hey I am back :)...

Just a thought, don't let me tread on your toes or anything....

But since you obviously have the questions in the database and you are creating the radio buttons dynamically why don't you just label the radio buttons correctly so you can link the answers back up to the question in the database.

Simple explanation...

QuestionTable
QID  -  QuestionText
1  -  Who are you

AnswerTable
QID - AID - AnswerText
1  -  1  -  I am Me
1  -  2  - I am You
1  -  3  - You are Me

So when you create your Radio list you name the radio group

radio_Q1

So when you get the response, you now it relates to the QID of 1 and whatever the one selected is obviously easy enough to figure out :)...

Let me know if I confused you here, I only have 2 months experience so you need to excuse me :)

0
 

Author Comment

by:emanstl
ID: 16610188
Gavin, my good buddy... I'm way, way past that. I know how to do all that. That's how it's done. I'm afraid to describe all the cool mathematics/recursion, graphic objects and other stuff this thing does. You might tell me what the function of an <head> tag is in html. But you're on the right track. Keep working it out and you'll have yourself the backbone of any good survey.

Anyway, scolja: I"m using 2.0, just as my original post said. Do you guys just read the first sentence and start responding to it without understanding the entire post ?

I know what an htmlinputhidden control is. I'm new to .net 2.0, but this stuff isn't difficult. Re-read my first post, all the way through this time.

When I dynamically create these hidden fields there values stay the same as the first page loaded. No matter how many times I try and change those values on postback when I loop through new records those values always stay the same as the original page. I don't know how to explain it more clear. I really don't. Someone who is familar working with asp.net 2.0 should get it.

I'll stay with what I've got, but I havne't gotten past "the understanding my question" part of this thread.

Thanks anyway guys.. I had to spend my 9.95 to find out what this place has..
0
 
LVL 15

Accepted Solution

by:
GavinMannion earned 1000 total points
ID: 16612171
Emanstl,

I know you don't want to hear it but in my personal opinion your logic of trying to hide a lookup value in a hidden field is flawed.

Instead of storing the value in a hidden field or a small textbox, :), try storing the link in a string array and saving that into the viewstate of the page.

By the sound of it you have done a lot of development in classic asp and are avoiding the benefits of asp.net

If you need to get the values out of the page using javascript then that is different, but Scolja brought that up and you ignored him.

If you do not need to access the values with Javascript then there is absolutely no reason to put them in a hidden control. Use Session, ViewState, Cookies or the database to store the link. It isn't difficult.

Anyway to avoid the fact that the values are not changing, change the ID of the hidden textbox when you load the new set or radio buttons. That will solve that problem but you will be hacking around a bad design. In my humble opinion.

Good luck, if you are still not winning you can try and get raterus or bsdotnet to try and help.
0
 
LVL 22

Assisted Solution

by:Jason Scolaro
Jason Scolaro earned 1000 total points
ID: 16613813
emanstl,

You do realize neither GavinMannion or myself get paid for attempting to help you?  We volunteer our time to try and help.  I see how much you appreciate that assistance.  It is possible it might take more than 1 or 2 comments to finally get the ball rolling in the right direction, solving a problem is a process.

The only reason I bring up .NET 2.0 is because it baffles me that anyone would use an HTML <input> hidden control (htmlinputhidden?) if they don't have to, (especially if the TextBox server control works), but it is possible that you don't have a complete understanding of all of the .NET 2.0 controls available to you.  That is what I have to try and assess in my comments to help direct you one way or another.  I believe you have many, many options available to you, but it's our job as experts to help guide to which is best.

In any case, you've got a good guy in Gavin that no matter how much you hammer him, he'll continue to help you.  I on the otherhand see no reason to donate my time to you any further.  

I wish you the very best of luck in reaching a solution that is acceptable.  If you wish to leave this question open, please do so and post another pointer question to this question so that some new experts can have a look at it.  http://www.experts-exchange.com/help.jsp#hi5

Take care.
-- Jason
0
 

Author Comment

by:emanstl
ID: 16613890
I"m sure you are all wonderful fellows.. however don't assume something, ask for more input if you don't understand.

I've asked you a couple times for further explanation of your these "viewstate" options. But I get nonsensical responses.

If you have no answer for the htmlhiddeninput issue I first raised then please explain further on the viewstate option and how that relates to exactly what I'm trying to do.

I'll type that again:

If you have no answer for the htmlhiddeninput issue I first raised then please explain further on the viewstate option and how that relates to exactly what I'm trying to do.

If you have no answer for the htmlhiddeninput issue I first raised then please explain further on the viewstate option and how that relates to exactly what I'm trying to do.

Whether you are getting paid or not is not my concern. I have paid to come on here and hopefully get a response to my question. If those responses are professional and helpful then I will continue to pay, otherwise I won't. Not hard..

I Have to hold values that are changing from page to page. These values are related to other values chosen from a radio list. Doesn't have to be a radio listk, could be a dropdown list, or check box, or whatever. The point is these "Header" values that I describe, or QID's aren't seen, they are just identifiers. Here's what I get from that: "We don't know why the htmlhiddeninput doesn't update your changing values, but you should use something else"

Using session state, cookies, etc. is not practical. When you say things like that it tells me that you don't know what your talking about. That you don't understand what I'm trying to do. Netierh logically or syntactically. But you don't get that. You get defensive. It's all going over your head for some reason.

I'm very aware of those options you give.. I want you to describe to me how YOU would use session state or cookies to handle the process I describe in my first post ?

Thanks you wonderful people for trying to assist.. unfortunately you never understood the problem..



0
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16614215
You need to store a 2 field list right?

Question - AnswerReceived

Simple survey right?

Create a DataTable with three fields

ControlID - Question - AnswerReceived

When populating the controls on the page add a new row to the table on each iteration so at the end you have.

Radio1 - Who are you - null
Radio2 - Who needs manners - null
TextBox1 - Did you get picked on at school - null

Okay now the user selects his 'answers', then on postback you can pick up all the radio/dropdown/textbox controls and match their ID to one in the Table you have and insert the answer there. Now you have

Radio1 - Who are you - noone important
Radio2 - Who needs manners - you
TextBox1 - Did you get picked on at school - Is it that obvious :)

To save the DataTable in a Session or ViewState you must place it in a DataSet. Let me know if you don't know how.

Then all you do is

ViewState("Mytable") = MyDataTable

Then on page load cast it back to a datatable. Again let me know how.

Okay all clear?
If you need some clarification on this let me know, if it doesn't help you at all then ask the mods to close this question and create a new one that scolja and myself will avoid like the plague :)
0
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16813598
I think both scolja and myself gave outstanding answers to this question... Split :)
0
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16869496
Hey scolja, this was one of my favourite questions ever :)
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

564 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