Dynamic Controls and their Values

Daspoo

Regular
Joined
Jan 27, 2003
Messages
99
Location
Fort Walton Beach, Florida
Ok, new question concerning the whole notion of dynamically creating controls on a web form. I've got an ASPX page building dynamic controls within an HTML table (e.g., textboxes). I write the HTML table in the HTML design area of the ASPX page and then build the rows and cells of the table, and the controls within the cells, in the code-behind page.

Now I'm stuck as to how I will be able to update the database with information within those textboxes, keeping in mind that between the Page_Load and the Update call, the user may change the values within those textboxes. When a postback happens, don't those dynamic controls disappear, and as a result, their respective values as well? And rebuilding the controls will make the "text" properties empty, no? Anyone have any ideas about how I can grab the text in a dynamically-generated textbox and use it after the postback? Thanks in advance!
 
Re:

Thanks for your response. I took a look at that post, but I didn't see anything there that would address the fact that the .text value of the dynamic textbox will be emptied on the postback. I have no problems re-creating the textbox; it's storing its value, to be used on a postback, that's causing the problem.

In other words:
1) The dynamic textbox is created in Page_Load event,
2) After the Page_Load but before the Update routine, the user enters something into the textbox,
3) The .text value of the textbox must be stored somehow; (since the control is dynamic, the value won't be there after postback like a typical, static textbox control on the form would be - so how can I store its value),
4) On postback, the textbox is dynamically created once more, and the .text value is set from its previous value before postback occurred.

The problem is in step 3. I don't think the post you referred to addresses user-input into the dynamic control; just a hard-coded value.
 
Daspoo said:
Thanks for your response. I took a look at that post, but I didn't see anything there that would address the fact that the .text value of the dynamic textbox will be emptied on the postback. I have no problems re-creating the textbox; it's storing its value, to be used on a postback, that's causing the problem.

In other words:
1) The dynamic textbox is created in Page_Load event,
2) After the Page_Load but before the Update routine, the user enters something into the textbox,
3) The .text value of the textbox must be stored somehow; (since the control is dynamic, the value won't be there after postback like a typical, static textbox control on the form would be - so how can I store its value),
4) On postback, the textbox is dynamically created once more, and the .text value is set from its previous value before postback occurred.

The problem is in step 3. I don't think the post you referred to addresses user-input into the dynamic control; just a hard-coded value.
You need to read/write the dynamic controls value to the viewstate
 
That's a great idea, Joe. But when would I set the viewstate information? Remember, the user may change the contents of the textbox between the page_load and calling the update command. If I set it during the update command, the page has already performed the postback, right? And if I set it on postback, the viewstate value would then be empty since the textbox has just been created. Am I following the process correctly here? A user clicks an Update button, the page is posted back, and then the Update button click routine is run. Am I wrong here? Thanks for all the help.
 
Quoted directly from the very last entry of the post I referred you to:

Dim txt1 as new TextBox

Private Sub Load(....)
if txt1.text <> "" then
me.controls.add(txt1)
end if
End Sub

Private Sub Button1_Click(.....) Handles Button1.Click
txt1.text = "Hello"
me.controls.add(txt1)
End Sub
 
Re:

Thanks for your offers of help, evaleah. I think I needed a bit more than just an old post quoted back to me with no explanation, but I appreciate the effort nevertheless. I believe I have things working now. It seems that even when creating the textbox dynamically, its value persists. So I do have to build the textbox dynamically even on postback, but as long as I don't attempt to populate it and it has the same .ID value, the page "remembers" the value. Totally contradictory to my mindset, but hey, it works (as far as I know..heh) so I'm not gonna lose any sleep over it. Hope this information helps someone else out there. :cool:
 
Dim txt1 as new TextBox

Private Sub Load(....)
if txt1.text <> "" then
me.controls.add(txt1)
end if
End Sub

Private Sub Button1_Click(.....) Handles Button1.Click
txt1.text = "Hello"
me.controls.add(txt1)
End Sub

These codes might be a bit out if you are looking for a quality piece of code.
Anyway, if you wish to do it in a proper way, you could try on creating a class for the textbox. And add a function called IsExisted for it.

Private ExistedFlag as Boolean
Function GetIsExisted() As Boolean
return ExistedFlag
End Function

Sub SetIsExisted()
ExistedFlag = True
End Sub

So, when the textbox is created/initialized, you should call SetIsExisted.
Everytime you postback, you check as below.

If objName.GetIsExisted Then
'show objName
End If
 
Daspoo said:
That's a great idea, Joe. But when would I set the viewstate information? Remember, the user may change the contents of the textbox between the page_load and calling the update command. If I set it during the update command, the page has already performed the postback, right? And if I set it on postback, the viewstate value would then be empty since the textbox has just been created. Am I following the process correctly here? A user clicks an Update button, the page is posted back, and then the Update button click routine is run. Am I wrong here? Thanks for all the help.
As soon as the client starts a post, the values in the form are in the viewstate.
The page recreates the controls. . . you 'suck' the value out of viewstate in page_load and load it into the newly created empty dynamic control.

In other words, page load happens first, then the event.
All postback form control values are available in the viewstate at page load.
Remember Viewstate contains the values that are posted back!
 
Re:

Thanks, Joe. I think that's the first it's been explained to me regarding ViewState in such a clear and concise way. That makes a LOT more sense. I've got my routine working, and after recreating the dynamic controls, ViewState is obviously putting the latest information (from before the postback) back into each, just as you've described. Thanks for your help! Much appreciated!
 
Back
Top