getting values of dynamically added controls

J

June Blasey

I am adding controls(dropdowns and checkboxes) dynamically to a placeholder or a htmltable control on a web form. I am able to load the page successfully. However, upon selecting a button(submit) on the page I am unable to get the values entered in the controls that were added. I can not even use the findcontrol method to find the controls.
Has anyone done this successfully? It seems to do with state?!?!?!
PLEASE, any ideas would be GREATLY APPRECIATED!!

Thanks,
June

___
Newsgroups brought to you courtesy of www.dotnetjohn.com
 
T

Teemu Keiski

Hi,

do you add the controls to the PlaceHolder on postback as well? If you
don't, you need to do that as controls need to be recreated on postback for
them to keep state, be searchable (in other words exist in Controls
collection)

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist


I am adding controls(dropdowns and checkboxes) dynamically to a placeholder
or a htmltable control on a web form. I am able to load the page
successfully. However, upon selecting a button(submit) on the page I am
unable to get the values entered in the controls that were added. I can not
even use the findcontrol method to find the controls.
Has anyone done this successfully? It seems to do with state?!?!?!
PLEASE, any ideas would be GREATLY APPRECIATED!!

Thanks,
June

___
Newsgroups brought to you courtesy of www.dotnetjohn.com
 
J

June Blasey

Hey, Thanks. I am able to now access the controls. Will I also be able to get the value of selected item(s) in a dropdown list. Assuming a dropdown list is one of the dynamically added controls?

Thanks again,
June
Hi,

do you add the controls to the PlaceHolder on postback as well? If you
don't, you need to do that as controls need to be recreated on postback for
them to keep state, be searchable (in other words exist in Controls
collection)

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist


I am adding controls(dropdowns and checkboxes) dynamically to a placeholder
or a htmltable control on a web form. I am able to load the page
successfully. However, upon selecting a button(submit) on the page I am
unable to get the values entered in the controls that were added. I can not
even use the findcontrol method to find the controls.
Has anyone done this successfully? It seems to do with state?!?!?!
PLEASE, any ideas would be GREATLY APPRECIATED!!

Thanks,
June

___
Newsgroups brought to you courtesy of www.dotnetjohn.com

___
Newsgroups brought to you courtesy of www.dotnetjohn.com
 
J

josema

Juney, how you solve that?, im getting the same problem since a long time...

I create controls dinamically but when i click the button, the controls dissapear, and i can get the values...

Thanks Juney.
Josema
 
T

Teemu Keiski

Hi,

yes you are if you just add the Control to the Controls collection before
populating (populating happening on initial request).

e.g:
1. Create control instance
2. Add to Controls collection
3. populate inside IsPostBack check

Another solution is to populate before adding to the Controls collection (in
this case populating on every request). This way values are considered to be
initial values and aren't stored to ViewState, but on postback repopulation
happens before postback data processing, so actual selection can be kept.
( Control will save its state only after it is added to the Controls
collection, therefore first to the collection and then populating)

1. Create control instance
2. Populate on every request
3. Add to Controls collection

With some controls like DataGrid etc the latter will mix up with postback
events (databinding loses old state so events cannot be raised). With some
controls this latter one will work, but might be easier to use the first
one.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist


Hey, Thanks. I am able to now access the controls. Will I also be able to
get the value of selected item(s) in a dropdown list. Assuming a dropdown
list is one of the dynamically added controls?

Thanks again,
June
Hi,

do you add the controls to the PlaceHolder on postback as well? If you
don't, you need to do that as controls need to be recreated on postback for
them to keep state, be searchable (in other words exist in Controls
collection)

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist


I am adding controls(dropdowns and checkboxes) dynamically to a placeholder
or a htmltable control on a web form. I am able to load the page
successfully. However, upon selecting a button(submit) on the page I am
unable to get the values entered in the controls that were added. I can not
even use the findcontrol method to find the controls.
Has anyone done this successfully? It seems to do with state?!?!?!
PLEASE, any ideas would be GREATLY APPRECIATED!!

Thanks,
June

___
Newsgroups brought to you courtesy of www.dotnetjohn.com

___
Newsgroups brought to you courtesy of www.dotnetjohn.com
 
J

June Blasey

Thanks Teemu - I really appreciate you assistance.
Please forgive my ignorance. Below is a snipet of my code and how I am
adding the controls can you help me apply what you are saying about
creating the control, populating, then adding it??

Thanks again,
June
---------------
'Getting page layout and controls from database

SQL = "Select * from WebDSSSearch where SearchPage Like '" & SearchPage
& "' order by [Rw], [Col]"
SearchDA = New OleDb.OleDbDataAdapter(SQL, DSSConn)
SearchDA.Fill(SearchDS, "Search")
SearchTbl = SearchDS.Tables("Search")
NumRows = SearchTbl.Rows.Count
numcontrols = 0
If NumRows > 0 Then
SearchTable.Visible = True
Do While (I < NumRows) And (SearchTbl.Rows(I).Item("rw")
= PrevRow)
Dim sCell As New TableCell
ControlStr = Trim(SearchTbl.Rows(I).Item("Control"))

Select Case ControlStr
Case "ListBox"
Dim AClistlabel As New Label
AClistlabel.ID =
SearchTbl.Rows(I).Item("Fieldname")
sCell.Controls.AddAt(0, AClistlabel)

AClistlabel.Text =
SearchTbl.Rows(I).Item("Fieldname")
AClistlabel.Font.Size = FontUnit.XSmall
AClistlabel.Font.Bold = True
AClistlabel.Font.Name = "ariel"
AClistlabel.ForeColor = Color.Navy

Dim listDR As OleDb.OleDbDataReader
Query = SearchTbl.Rows(I).Item("DBMethod")


SqlCommand2.CommandText = Query
listDR = SqlCommand2.ExecuteReader

Dim AClist As New ListBox
AClist.ID =
Trim(SearchTbl.Rows(I).Item("ControlID"))
sCell.Controls.AddAt(1, AClist)

AClist.SelectionMode =
ListSelectionMode.Multiple
AClist.Rows = 2
AClist.DataSource = listDR
AClist.DataValueField =
Trim(SearchTbl.Rows(I).Item("DataField"))
AClist.DataBind()
listDR.Close()

sCell.ID = "cell" & I
numcontrols = numcontrols + 1
End Select
srow.Cells.Add(sCell)

PrevRow = SearchTbl.Rows(I).Item("rw")

Loop 'while row = prevrow
SearchTable.Rows.Add(srow)
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top