Dynamic Controls and Postback

S

Steve Roszko

I am creating a set of dynamic text boxes on my page.

When I do a postback the dynamic controls seem to always disappear. I have
tried setting the EnableViewstate = True and still no luck. I would like
them to remain and persist any data entered by the user on postback.

Any ideas?

-Steve
 
A

Alex Papadimoulis

Steve,

They'll need to be recreated everytime. The veiwstate only stores
information about the control, not the control it self.

-- Alex Papadimoulis
 
S

Steve Roszko

They'll need to be recreated everytime. The veiwstate only stores
information about the control, not the control it self.
-- Alex Papadimoulis


Yeah, that's what conclusion I seemed to come to but thought I'd see if I
was missing something. Here is some code for basically how I solved it.

ASPX PAGE

<body>
<form id="Form1" method="post" runat="server">
<asp:Button Runat=server ID=btnAdd Text=Add />
<asp:Button Runat=server ID=btnPost Text=Post/>
<asp:placeHolder ID=place Runat=server EnableViewState=True />
</form>
</body>


CODE BEHIND

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()

'--> RECREATE DYNAMIC CONTROLS IF NEEDED <--
If Request.Form("txtb") <> "" Then
Dim txtbox As New TextBox
txtbox.Text = Request.Form("txtb")
txtbox.ID = "txtb"
txtbox.EnableViewState = True

Me.place.Controls.Add(txtbox)

End If
End Sub

Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
Dim txtbox As New TextBox
txtbox.Text = Now.ToString
txtbox.ID = "txtb"
txtbox.EnableViewState = True

Me.place.Controls.Add(txtbox)

End Sub


-Steve
 
Joined
Nov 19, 2010
Messages
3
Reaction score
0
how to get values in dynamic added textbox‏

i have same kind of problem but with ajax ,i need to store dynamic control name and its values etc etc

Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged


Dim index As Integer

For index = 1 To RadioButtonList1.SelectedValue

Dim newdiv As New HtmlGenericControl("div")
newdiv.ID = "mydiv" & index
'newdiv.Attributes.Add("id", "mydiv")

newdiv.EnableViewState = True
Dim ht As New HtmlTable()
ht.ID = "mytable" & index.ToString()
ht.EnableViewState = True

ht.Width = "100%"
ht.CellSpacing = "0"
ht.CellPadding = "0"
ht.Border = 0


Dim htr, htr1, htr2 As New HtmlTableRow()
Dim cell, htc, htc1, htc2, htc3, htc4, htc5, htc6, htc7, htc8 As New HtmlTableCell()

Dim name_tb As New TextBox()
Dim sal_ddl As New DropDownList

'validator
Dim name_req As New RequiredFieldValidator
Dim tel_req As New RequiredFieldValidator
Dim email_req As New RequiredFieldValidator


name_tb.ID = "name_tb" & index
sal_ddl.ID = "sal_ddl" & index
name_req.ID = "name_req" & index
tel_req.ID = "tel_req" & index
email_req.ID = "email_req" & index
name_tb.EnableViewState = True
sal_ddl.EnableViewState = True
tel_req.EnableViewState = True

sal_ddl.Items.Add("MR")
sal_ddl.Items.Add("Ms")
sal_ddl.Items.Add("Mrs")



Dim Tel_tb1 As New TextBox
Tel_tb1.ID = "Tel_tb1" & index
Tel_tb1.EnableViewState = True
name_tb.TextMode = TextBoxMode.SingleLine

Dim email_tb1 As New TextBox
email_tb1.ID = "email_tb1" & index
email_tb1.TextMode = TextBoxMode.SingleLine

htc.InnerText = "Name : "
htc.Width = "20%"
htc.Align = "Left"


htc1.Controls.Add(sal_ddl)
htc1.Width = "10%"

htc2.Width = "70%"
htc2.Align = "left"



htc2.Controls.Add(name_tb)
htc2.Controls.Add(name_req)
htr.Controls.Add(htc)
htr.Controls.Add(htc1)
htr.Controls.Add(htc2)


ht.Controls.Add(htr)
name_req.ControlToValidate = "name_tb" & index
name_req.Display = ValidatorDisplay.Dynamic
name_req.ErrorMessage = ("Please Enter name of attendenes")


'make 2nd row

htc3.InnerText = "Telephone : "
htc3.Width = "20%"
htc3.Align = "Left"


htc4.InnerHtml = "<strong></strong>"
htc4.Width = "10%"

htc5.Width = "70%"
htc5.Align = "left"

htc5.Controls.Add(Tel_tb1)
htc5.Controls.Add(tel_req)
htr1.Controls.Add(htc3)
htr1.Controls.Add(htc4)
htr1.Controls.Add(htc5)


ht.Controls.Add(htr1)

tel_req.ControlToValidate = "Tel_tb1" & index
tel_req.Display = ValidatorDisplay.Dynamic
tel_req.ErrorMessage = ("Please Enter Phone")


'make 3rd row

htc6.InnerText = "Email : "
htc6.Width = "20%"
htc6.Align = "Left"


htc7.InnerHtml = "<strong></strong>"
htc7.Width = "10%"

htc8.Width = "70%"
htc8.Align = "left"

htc8.Controls.Add(email_tb1)
htc8.Controls.Add(email_req)
htr2.Controls.Add(htc6)
htr2.Controls.Add(htc7)
htr2.Controls.Add(htc8)

ht.Controls.Add(htr2)

email_req.ControlToValidate = "email_tb1" & index
email_req.Display = ValidatorDisplay.Dynamic
email_req.ErrorMessage = ("Please Enter Email")

'Add a blank rows

cell.ColSpan = 3
cell.InnerHtml = "<br/>"
Dim row As New HtmlTableRow
row.Cells.Add(cell)
ht.Rows.Add(row)



newdiv.Controls.Add(ht)

newdiv.Attributes.Add("table", "ht")



newdiv.Attributes.Add("Style", "border-bottom :#ccc solid 0px")
newdiv.Attributes.Add("style", "height:23px")
newdiv.Attributes.Add("style", "padding-left:20px")


newdiv.EnableViewState = True
Me.PlaceHolder1.Controls.Add(newdiv)
PlaceHolder1.EnableViewState = True


Next


'End Sub


End Sub

and here is teh code behind of submitt where i need to get name of textboxes

Protected Sub btn_submitt_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_submitt.Click
Dim index As Integer
'MsgBox(Me.RadioButtonList1.SelectedItem.Value)
For index = 1 To RadioButtonList1.SelectedValue
Dim plholder As PlaceHolder = DirectCast(FindControl("PlaceHolder1"), PlaceHolder)
'Dim name_tb As TextBox = DirectCast(plholder.FindControl("name_tb" & index.ToString()), TextBox)

Dim newdiv As HtmlGenericControl = DirectCast(plholder.FindControl("mydiv"), HtmlGenericControl)

If newdiv Is Nothing Then
Response.Write("Nothing")
End If
next
end sub
i m getting nothing in if block means its not getting div on postback
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top