Dynamic Adding Controls to Placeholder & presisit during postback
-
I want to add textboxes on the click of button to the placeholser, i did this by adding this code on the click event of button. TextBox txtBox = new TextBox(); PlaceHolder1.Controls.AddAt(0, txtBox); txtBox.ID = "1"; but when i click on another button (on postback), there is no control in placeholder, i did google and found that i need to save state of contorl in viewstate, so after initilization, i can access these controls, but dont know how to implement this, will you please guide me with code snipt which event needs to be override or something else any help will be highly appriciated. Many thanks & Best Regards, adnan
Many Thanks, Adnan Rafiq muhammadadnanrafiq@gmail.com
-
I want to add textboxes on the click of button to the placeholser, i did this by adding this code on the click event of button. TextBox txtBox = new TextBox(); PlaceHolder1.Controls.AddAt(0, txtBox); txtBox.ID = "1"; but when i click on another button (on postback), there is no control in placeholder, i did google and found that i need to save state of contorl in viewstate, so after initilization, i can access these controls, but dont know how to implement this, will you please guide me with code snipt which event needs to be override or something else any help will be highly appriciated. Many thanks & Best Regards, adnan
Many Thanks, Adnan Rafiq muhammadadnanrafiq@gmail.com
I would guess you are only adding the controls if Page.IsPostBack = false, you need to add the dynamic controls on every postback, try this:
Partial Public Class _Default Inherits System.Web.UI.Page Protected Overrides Sub CreateChildControls() MyBase.CreateChildControls() Dim textBox As New TextBox textBox.ID = "TextBox1" PlaceHolder1.Controls.Add(textBox) Dim button As New Button button.ID = "Button1" button.Text = "Click Me" AddHandler button.Click, AddressOf Button1_Click PlaceHolder1.Controls.Add(button) End Sub Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Dim label As New Label label.ID = "Lable1" label.Text = "I was just posted back" PlaceHolder1.Controls.Add(label) End Sub End Class
Hope this helps Tom -
I would guess you are only adding the controls if Page.IsPostBack = false, you need to add the dynamic controls on every postback, try this:
Partial Public Class _Default Inherits System.Web.UI.Page Protected Overrides Sub CreateChildControls() MyBase.CreateChildControls() Dim textBox As New TextBox textBox.ID = "TextBox1" PlaceHolder1.Controls.Add(textBox) Dim button As New Button button.ID = "Button1" button.Text = "Click Me" AddHandler button.Click, AddressOf Button1_Click PlaceHolder1.Controls.Add(button) End Sub Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Dim label As New Label label.ID = "Lable1" label.Text = "I was just posted back" PlaceHolder1.Controls.Add(label) End Sub End Class
Hope this helps TomThanks for reply. It helps me, but not in my current scenrio. *I want to add controls to placeholder when IsPostBack==True I have this scenrio: one course can have many dates, and i am inteneded to save course info & dates info on single click event of button. For that i want to put a placeholder control in the ajax update pannel, and a button to add text box to enter date in it, each time the button is clicked a one more text box should be addto that place holder, and previous text boxes should also retain there values selected by user. And at the end when i click on save button, i want to get the values of textboxes in placeholder control. Many Thanks & Best Regards, Adnan
Many Thanks, Adnan Rafiq muhammadadnanrafiq@gmail.com
-
Thanks for reply. It helps me, but not in my current scenrio. *I want to add controls to placeholder when IsPostBack==True I have this scenrio: one course can have many dates, and i am inteneded to save course info & dates info on single click event of button. For that i want to put a placeholder control in the ajax update pannel, and a button to add text box to enter date in it, each time the button is clicked a one more text box should be addto that place holder, and previous text boxes should also retain there values selected by user. And at the end when i click on save button, i want to get the values of textboxes in placeholder control. Many Thanks & Best Regards, Adnan
Many Thanks, Adnan Rafiq muhammadadnanrafiq@gmail.com
The following will do what you are after. It assumes you have a ASPX page with an UpdatePanel containing a PlaceHolder and a Button:
Partial Public Class _Default Inherits System.Web.UI.Page Private Property TextBoxCount() As Integer Get If ViewState("TextBoxCount") Is Nothing Then ViewState("TextBoxCount") = 1 End If Return CInt(ViewState("TextBoxCount")) End Get Set(ByVal value As Integer) ViewState("TextBoxCount") = value End Set End Property Protected Overrides Sub CreateChildControls() MyBase.CreateChildControls() For textBoxCounter As Integer = 1 To TextBoxCount AddButton() Next End Sub Private Sub AddButton() Dim textBox As New TextBox textBox.ID = "TextBox" + PlaceHolder1.Controls.Count.ToString PlaceHolder1.Controls.Add(textBox) End Sub Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click AddButton() TextBoxCount += 1 End Sub End Class
I can send you the sample project if you want. Cheers Tom