473,657 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(placehold er1.controls.it em(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 1517
"Konrad Hammerer" <ko************ *@tesis.dewrote in message
news:11******** *******@gzpc.te sis.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.A dd(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(placeho lder1.controls. item(0),textbox ).text, 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.A dd(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(placeho lder1.controls. item(0),textbox ).text, 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.te sis.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.te sis.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.te sis.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_Sel ectedIndexChang ed), 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
(btnExecuteFilt er_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.P age

Private m_cDefaultLVStr ing As String = "(Parameterwert )"

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Load
If Session.Item("U ser") = "" Or Session.Item("P assword") = "" Then
Session.Add("La stError", "Es sind keine Anmeldedaten im System
hinterlegt!")
Response.Redire ct("./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.g etFilters(Sessi on.Item("User") ,
Session.Item("P assword"), oCol)

For Each oFilter As brlvFilter In oCol
cboFilters.Item s.Add(oFilter.T oString())
Next

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

End Sub

Protected Sub cboFilters_Sele ctedIndexChange d(ByVal sender As Object,
ByVal e As System.EventArg s) Handles cboFilters.Sele ctedIndexChange d

Dim oCol As Collection = Nothing

brlvDBHandler.g etFilters(Sessi on.Item("User") ,
Session.Item("P assword"), oCol)

For Each oFiler As brlvFilter In oCol
If cboFilters.Sele ctedItem.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.Na me = "Bezirksverband " And oFilterParam.Ty pe =
"Text" Then
txt.Text = Session.Item("B ezirksverband")
Else
txt.Text = m_cDefaultLVStr ing
End If

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

oCell1.Text = oFilterParam.Na me
oCell1.CssClass = "filter"

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

oCell3.Text = "Parameter-Typ: " & oFilterParam.Ty pe

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

tblParams.Rows. Add(oRow)
Next

End Sub

Protected Sub btnExecuteFilte r_Click(ByVal sender As Object, ByVal e
As System.EventArg s) Handles btnExecuteFilte r.Click

Dim oCol As Collection = Nothing

brlvDBHandler.g etFilters(Sessi on.Item("User") ,
Session.Item("P assword"), oCol)

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

End Sub

Private Sub executeFilter(B yVal oFilter As brlvFilter)

Dim txt As TextBox

lblError.Text = ""

For Each oRow As TableRow In tblParams.Rows
txt = CType(oRow.Cell s.Item(1).Contr ols.Item(0), TextBox)
If txt.Text = m_cDefaultLVStr ing 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.SqlPara ms.Clear()

End Sub
End Class
>
Nov 6 '07 #7
"Konrad Hammerer" <ko************ *@tesis.dewrote in message
news:11******** *******@gzpc.te sis.de...
>"Konrad Hammerer" <ko************ *@tesis.dewrote in message
news:11******* ********@gzpc.t esis.de...
After entering the values, the user clicks a button
(btnExecuteFilt er_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.te sis.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2494
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 the table. I know that I can dynamically add controls (eg a textbox) to the page controls collection of a web form in a server event which will then be rendered onto the form, as in the following snippet: System.Web.UI.WebControls.TextBox...
5
1956
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 some textboxes (dynamically added). I know I could move it around with CSS but I want to move towards an accessible site that will display forms in the right order without CSS. Regards, Chris.
5
4448
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 survive postback. This seems to be true. However, this seems to make dynamically adding controls rather pointless to me, since if you cannot retrieve the values of the controls when you perform a postback, why add the controls in the firstplace?...
1
4890
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 set text boxes and labels inside the table rows. I then added a button. All of these are done through code. The problem that i am having is i can get the value from a text box with resides inside the first panel (out side of panel that is...
7
6651
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 tabcontainer which has 1 panel already, however I want to try create the TabPanels dynamically. I followed the advice here: http://www.asp.net/learn/ajax-videos/video-156.aspx (3rd comment - Joe Stagner)
0
8395
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6166
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.