Example for dynamically added controls

  • Thread starter Konrad Hammerer
  • Start date
K

Konrad Hammerer

Hi!

Can somebody tell me or show me an example how to add a textbox and a
button dynamically by code to an apsx page and how to read the filled in
value from the textbox after the user has clicked the button?

I have tried this without success because after the user has clicked the
button, the textbox seems to be disappeard! I have added it to a
placeholder and tried to read it with
ctype(placeholder1.controls.item(0),textbox).text, but it did not work...

The goal is to provide some textboxes and read the input data of them in
the "click"-ActionHandler of the button!

Thanks,
Konrad
 
M

Michael Nemtsev [MVP]

Hello Konrad,

this.Controls.Add(new LiteralControl("<h3>Value: "));


---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


KH> Hi!
KH>
KH> Can somebody tell me or show me an example how to add a textbox and
KH> a button dynamically by code to an apsx page and how to read the
KH> filled in value from the textbox after the user has clicked the
KH> button?
KH>
KH> I have tried this without success because after the user has clicked
KH> the button, the textbox seems to be disappeard! I have added it to a
KH> placeholder and tried to read it with
KH> ctype(placeholder1.controls.item(0),textbox).text, but it did not
KH> work...
KH>
KH> The goal is to provide some textboxes and read the input data of
KH> them in the "click"-ActionHandler of the button!
KH>
KH> Thanks,
KH> Konrad
 
K

Konrad Hammerer

Hello Konrad,
this.Controls.Add(new LiteralControl("<h3>Value: "));

I do not really understand how this should solve my problem?

Best,
Konrad
---
WBR, Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour
"The greatest danger for most of us is not that our aim is too high and
we miss it, but that it is too low and we reach it" (c) Michelangelo

KH> Hi!
KH> KH> Can somebody tell me or show me an example how to add a textbox and
KH> a button dynamically by code to an apsx page and how to read the
KH> filled in value from the textbox after the user has clicked the
KH> button?
KH> KH> I have tried this without success because after the user has
clicked
KH> the button, the textbox seems to be disappeard! I have added it to a
KH> placeholder and tried to read it with
KH> ctype(placeholder1.controls.item(0),textbox).text, but it did not
KH> work...
KH> KH> The goal is to provide some textboxes and read the input data of
KH> them in the "click"-ActionHandler of the button!
KH> KH> Thanks,
KH> Konrad
 
K

Konrad Hammerer

Hi!

Thanks for the link! But my problem was not the part of adding the
controls to the page but reading the values of them after the user has
clicked the button. As I mentioned:

The goal is to provide some textboxes *AND read the input data of them
in the "click"-ActionHandler of the button*!


Do you have some information on that part as well?

Thanks,
Konrad
 
M

Mark Rae [MVP]

Thanks for the link! But my problem was not the part of adding the
controls to the page but reading the values of them after the user has
clicked the button. As I mentioned:

The goal is to provide some textboxes *AND read the input data of them in
the "click"-ActionHandler of the button*!

Do you have some information on that part as well?

The 4Guys article describes all that...

Please post your code...
 
K

Konrad Hammerer

Konrad Hammerer said:
The 4Guys article describes all that...

hmmm, maybe I have missed something there... I will read it again!
Please post your code...

My code (+ explanation):

1)
fillFilters(): fills a combobox with some values

2)
If the user changes the value of the combobox
(cboFilters_SelectedIndexChanged), paintTextBoxes() will add dynamically
rows, cells and textboxes within the cells to a table (added not by code
but with the designer). The Textboxes are filled with default values
which should be changed by the user!

3)
After entering the values, the user clicks a button
(btnExecuteFilter_Click). After this, the function executeFilter()
should read the values but I cannot find/read the controls (rows, cells
and textboxes) -> That is the problem!!!

Code:

Partial Class filter
Inherits System.Web.UI.Page

Private m_cDefaultLVString As String = "(Parameterwert)"

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Session.Item("User") = "" Or Session.Item("Password") = "" Then
Session.Add("LastError", "Es sind keine Anmeldedaten im System
hinterlegt!")
Response.Redirect("./showerror.aspx")
End If


If Not Me.IsPostBack Then
'
' get filters from database
'
fillFilters()
End If

lblError.Text = ""

End Sub

Private Sub fillFilters()

Dim oCol As Collection = Nothing

brlvDBHandler.getFilters(Session.Item("User"),
Session.Item("Password"), oCol)

For Each oFilter As brlvFilter In oCol
cboFilters.Items.Add(oFilter.ToString())
Next

If oCol.Count > 0 Then paintTextBoxes(CType(oCol.Item(1), brlvFilter))

End Sub

Protected Sub cboFilters_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles cboFilters.SelectedIndexChanged

Dim oCol As Collection = Nothing

brlvDBHandler.getFilters(Session.Item("User"),
Session.Item("Password"), oCol)

For Each oFiler As brlvFilter In oCol
If cboFilters.SelectedItem.Text = oFiler.ToString Then
paintTextBoxes(oFiler)
End If
Next

End Sub

Private Sub paintTextBoxes(ByVal oFilter As brlvFilter)

Dim txt As TextBox
Dim oFilterParam As brlvFilterParam
Dim oRow As TableRow
Dim oCell1 As TableCell
Dim oCell2 As TableCell
Dim oCell3 As TableCell

oRow = New TableRow
oCell1 = New TableCell
oCell2 = New TableCell
oCell3 = New TableCell

For Each oFilterParam In oFilter.Params
'
' create and fill new textbox
'
txt = New TextBox

If oFilterParam.Name = "Bezirksverband" And oFilterParam.Type =
"Text" Then
txt.Text = Session.Item("Bezirksverband")
Else
txt.Text = m_cDefaultLVString
End If

oRow = New TableRow
oCell1 = New TableCell
oCell2 = New TableCell
oCell3 = New TableCell

oCell1.Text = oFilterParam.Name
oCell1.CssClass = "filter"

oCell2.Controls.Add(txt)
oCell2.CssClass = "filter"

oCell3.Text = "Parameter-Typ: " & oFilterParam.Type

oRow.Cells.Add(oCell1)
oRow.Cells.Add(oCell2)
oRow.Cells.Add(oCell3)

tblParams.Rows.Add(oRow)
Next

End Sub

Protected Sub btnExecuteFilter_Click(ByVal sender As Object, ByVal e
As System.EventArgs) Handles btnExecuteFilter.Click

Dim oCol As Collection = Nothing

brlvDBHandler.getFilters(Session.Item("User"),
Session.Item("Password"), oCol)

For Each oFilter As brlvFilter In oCol
If cboFilters.SelectedItem.Text = oFilter.ToString Then
paintTextBoxes(oFilter) ' geht so nicht, da sonst alle
User-Angaben wieder überschrieben werden! Und es ist auch irgendwie
unsinnig!
executeFilter(oFilter)
End If
Next

End Sub

Private Sub executeFilter(ByVal oFilter As brlvFilter)

Dim txt As TextBox

lblError.Text = ""

For Each oRow As TableRow In tblParams.Rows
txt = CType(oRow.Cells.Item(1).Controls.Item(0), TextBox)
If txt.Text = m_cDefaultLVString Then
lblError.Text = "Bitte geben Sie für den Parameter """ &
oRow.Cells.Item(0).Text & """ einen gültigen Wert an!"
Return
End If
Next

oFilter.SqlParams.Clear()

End Sub
End Class
 
M

Mark Rae [MVP]

After entering the values, the user clicks a button
(btnExecuteFilter_Click). After this, the function executeFilter() should
read the values but I cannot find/read the controls (rows, cells and
textboxes) -> That is the problem!!!

Are you recreating the controls every time the page loads, whether because
of postback or not...?

Dynamically created controls do not survive a postback; they need to be
recreated every time in Page_Init - Page_Load is (almost) always too far
into the page cycle to create dynamic controls...
 
K

Konrad Hammerer

Are you recreating the controls every time the page loads, whether
because of postback or not...?

No, check the code (fillFilters() creates the controls):

If Not Me.IsPostBack Then
'
' get filters from database
'
fillFilters()
End If

###
Dynamically created controls do not survive a postback; they need to be
recreated every time in Page_Init - Page_Load is (almost) always too far
into the page cycle to create dynamic controls...

Ok, let's assume I create the controls everytime single postback. Then I
would have access to my textboxes but how do I get the values the user
has typed in before the postback! I need this values and that is exactly
my problem! I can of course create them again, but then I have default
controls and the values are gone!

How can I solve this problem?

Thanks,
Konrad
 
M

Mark Rae [MVP]

No, check the code (fillFilters() creates the controls):

If Not Me.IsPostBack Then
'
' get filters from database
'
fillFilters()
End If

As I said, dynamically created controls do not survive a postback, so they
need to be created every time the page loads, irrespective of how it
loads...
Ok, let's assume I create the controls everytime single postback. Then I
would have access to my textboxes but how do I get the values the user has
typed in before the postback! I need this values and that is exactly my
problem! I can of course create them again, but then I have default
controls and the values are gone!

How can I solve this problem?

As I said, dynamic controls need to be created in Page_Init, not
Page_Load...
 
K

Konrad Hammerer

As I said, dynamically created controls do not survive a postback, so
they need to be created every time the page loads, irrespective of how
it loads...


As I said, dynamic controls need to be created in Page_Init, not
Page_Load...

I have heard that a lot, but still no success... Here is a little demo
code I have just tried:

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Init
Dim oRow As New TableRow
Dim oCell As New TableCell
Dim txt As New TextBox

oCell.ID = "cell1"
oRow.ID = "row1"
txt.ID = "txt1"

Me.Controls.Add(New LiteralControl("test" + txt.Text))

oCell.Controls.Add(txt)
oRow.Cells.Add(oCell)
Table1.Rows.Add(oRow)
End Sub

End Class

If I get you right, the new LiteralControl should have the content
"test" + the text from the textbox after the postback! But txt.text is
empty in the postback so it does not work (Table1 is a normal asp table
added by the designer)!

Maybe you can modify it so that I will see the difference.

I cannot see the connection of the first created textbox and the second
one after the postback? How should the second, recreated textbox have
the value of the first, initial create textbox?

Thanks again,
Konrad
 
K

Konrad Hammerer

I got it!!!

The thing is to recreate the controls in the init (before the the value
assignment is done), but read them later on for example in the page_load
(after the value assignment was done).

You were right, the 4Guys article describes all that ;-)

Thank you again!

Best,
Konrad
 
M

Mark Rae [MVP]

The thing is to recreate the controls in the init (before the the value
assignment is done), but read them later on for example in the page_load
(after the value assignment was done).

I did actually tell you that... ;-)
 
K

Konrad Hammerer

I did actually tell you that... ;-)

I'm very, very at the beginning of asp.net, so please don't blame me ;-)
 

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