How to retrieve back editable values in textboxes added in PlaceHolder?
I'm a newbie here and I'm stuck with the coding. So far I've created 3 buttons, first button is to load the values in textboxes added to placeholder. Then button2 is to extract the data from button 1 and display out. Now the 3rd step, I want to edit the values from the textboxes, after edit, how do I grab and display out the new values in label and textboxes?
Code:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Integer = 1
Dim message As String
For x = 1 To 36
Dim txt As New TextBox
'txt.ID = "Text" & ViewState("COUNT")
txt.ID = "Text" & x
txt.Text = x.ToString
PlaceHolder1.Controls.Add(txt)
Next
For x = 1 To 36
Dim stxt As String = "Text" & x.ToString
Dim txtxx As TextBox = TryCast(PlaceHolder1.FindControl(stxt), TextBox)
If txtxx IsNot Nothing Then
'There's your textbox value.
message = message & txtxx.Text & ","
End If
Next
'Label1.Text = message
Session("message") = message
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Label1.Text = Session("message")
Dim x As Integer
Dim message As String = ""
For x = 1 To 36
Dim txt As New TextBox
txt.ID = "Text" & x
txt.Text = x.ToString
PlaceHolder1.Controls.Add(txt)
Next
For x = 1 To 36
Dim stxt As String = "Text" & x.ToString
Dim txtxx As TextBox = TryCast(PlaceHolder1.FindControl(stxt), TextBox)
If txtxx IsNot Nothing Then
'There() 's your textbox value.
message = message & txtxx.Text
End If
Next
End Sub
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
End Sub
End Class
Re: How to retrieve back editable values in textboxes added in PlaceHolder?
Hello,
The first thing that I would have to ask for is the use case for what you are trying to do.
Although you "can" add controls to the page dynamically at runtime, it has to be understood that there are complexities involved in doing this in terms of maintaining those controls across postbacks, viewstate, etc. I would encourage you to read this article:
http://www.4guysfromrolla.com/articles/092904-1.aspx
To gain a better understanding of those complexities.
Once you have read that, and you still want to continue down this route, we can then extend the conversation.
Gary