Link to home
Start Free TrialLog in
Avatar of emanstl
emanstl

asked on

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

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

Avatar of GavinMannion
GavinMannion

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?
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
Avatar of emanstl

ASKER

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
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?
Avatar of emanstl

ASKER

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 ?
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.
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
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 :)

Avatar of emanstl

ASKER

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..
ASKER CERTIFIED SOLUTION
Avatar of GavinMannion
GavinMannion

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of emanstl

ASKER

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..



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 :)
I think both scolja and myself gave outstanding answers to this question... Split :)
Hey scolja, this was one of my favourite questions ever :)