471,066 Members | 1,617 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

Example for dynamically added controls

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
Nov 6 '07 #1
13 1420
"Konrad Hammerer" <ko*************@tesis.dewrote in message
news:11***************@gzpc.tesis.de...
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?
http://aspnet.4guysfromrolla.com/articles/092904-1.aspx
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 6 '07 #2
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
KHHi!
KH>
KHCan somebody tell me or show me an example how to add a textbox and
KHa button dynamically by code to an apsx page and how to read the
KHfilled in value from the textbox after the user has clicked the
KHbutton?
KH>
KHI have tried this without success because after the user has clicked
KHthe button, the textbox seems to be disappeard! I have added it to a
KHplaceholder and tried to read it with
KHctype(placeholder1.controls.item(0),textbox).tex t, but it did not
KHwork...
KH>
KHThe goal is to provide some textboxes and read the input data of
KHthem in the "click"-ActionHandler of the button!
KH>
KHThanks,
KHKonrad
Nov 6 '07 #3
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

KHHi!
KHKHCan somebody tell me or show me an example how to add a textbox and
KHa button dynamically by code to an apsx page and how to read the
KHfilled in value from the textbox after the user has clicked the
KHbutton?
KHKHI have tried this without success because after the user has
clicked
KHthe button, the textbox seems to be disappeard! I have added it to a
KHplaceholder and tried to read it with
KHctype(placeholder1.controls.item(0),textbox).tex t, but it did not
KHwork...
KHKHThe goal is to provide some textboxes and read the input data of
KHthem in the "click"-ActionHandler of the button!
KHKHThanks,
KHKonrad

Nov 6 '07 #4
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

Mark Rae [MVP] schrieb:
"Konrad Hammerer" <ko*************@tesis.dewrote in message
news:11***************@gzpc.tesis.de...
>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?

http://aspnet.4guysfromrolla.com/articles/092904-1.aspx

Nov 6 '07 #5
"Konrad Hammerer" <ko*************@tesis.dewrote in message
news:11***************@gzpc.tesis.de...
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...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 6 '07 #6
"Konrad Hammerer" <ko*************@tesis.dewrote in message
news:11***************@gzpc.tesis.de...
>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...
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
>
Nov 6 '07 #7
"Konrad Hammerer" <ko*************@tesis.dewrote in message
news:11***************@gzpc.tesis.de...
>"Konrad Hammerer" <ko*************@tesis.dewrote in message
news:11***************@gzpc.tesis.de...
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...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 6 '07 #8
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
>
Nov 7 '07 #9
"Konrad Hammerer" <ko*************@tesis.dewrote in message
news:11***************@gzpc.tesis.de...
>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
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...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 7 '07 #10
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...
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
>
Nov 7 '07 #11
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
Mark Rae [MVP] schrieb:
"Konrad Hammerer" <ko*************@tesis.dewrote in message
news:11***************@gzpc.tesis.de...
>>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

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...

Nov 7 '07 #12
"Konrad Hammerer" <ko*************@tesis.dewrote in message
news:11***************@gzpc.tesis.de...
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... ;-)
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 7 '07 #13
I did actually tell you that... ;-)

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Bas Groeneveld | last post: by
5 posts views Thread by Chris | last post: by
5 posts views Thread by Nathan Sokalski | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.