473,322 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 1501
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bas Groeneveld | last post by:
I am developing an ASP.NET application part of which consists of a data entry wizard defined by entries in a data table - ie the controls on each page of the wizard are determined by definitions in...
5
by: Chris | last post by:
I have a page with mixture of static and dynamically added controls is there any way of controlling the order which they are added to the page. My submit button (statically added) appears before...
5
by: Nathan Sokalski | last post by:
I have a custom control that I wrote (I inherit from System.Web.UI.WebControls.CompositeControl). I dynamically add this control to my Page, but I was told that dynamically added controls do not...
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
7
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.