473,499 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with Controls generated at Runtime

Hi,

I encountered a strange problem while working on an application, the
problem goes like this. I am generating
a few textboxes at runtime at the server side and added to a
placeholder, a value is set for each of the textboxes.
There is a button that causes the page to be posted back and the
texboxes are re-rendered. When the textboxes are re-rendered the values
are
reset and these texboxes are again added to the placeholder. The names
of the textboxes follow a naming convention ie they have the format
texbox_<intcounter> example textbox_1, textbox_2.

The problem thats happening is that the values of the textboxes are not
getting reset . i.e if the id of the first textbox is textbox_1 and has
a value of 10 after
postback when I generate another textbox with the same id and set a
value of 50 when the page gets rendered the textbox has a value of
10 instead of 50!!!

Here's some code that replicates the problem that Ive encountered. Can
someone please point out why this is happening?? I think this is
happening because the data posted by the control overwrites the value
that is set for the textbox when the it is re-rendered (If this is the
case then could someone please send me a link/information about details
of this step). Thanks in advance.
<!-- Html -->
<form id="Form1" method="post" runat="server">
<asp:PlaceHolder ID="ph" Runat="server"></asp:PlaceHolder>
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 48px; POSITION:
absolute; TOP: 168px" runat="server" Text="Button"></asp:Button>
</form>

<!-- Html -->

<!-- Server side code -->

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then
abc()
Else
abc2()
End If
End Sub

Private Sub abc()
Dim intcounter As Integer
Dim obj As System.Web.UI.HtmlControls.HtmlInputText
Dim btn As Button
Try
For intcounter = 0 To 2
obj = New System.Web.UI.HtmlControls.HtmlInputText
obj.ID = intcounter.ToString
obj.Value = intcounter
ph.Controls.Add(obj)
Next
Catch ex As Exception
Throw ex
End Try
End Sub

Private Sub abc2()
Dim intcounter As Integer
Dim obj As System.Web.UI.HtmlControls.HtmlInputText
Dim btn As Button
Try
For intcounter = 0 To 1
obj = New System.Web.UI.HtmlControls.HtmlInputText
obj.ID = intcounter.ToString
obj.Value = CInt(intcounter + 99)
ph.Controls.Add(obj)

Next

Catch ex As Exception
Throw ex
End Try
End Sub
<!-- Server side code -->

Hi,

I encountered a strange problem while working on an application, the
problem goes like this. I am generating
a few textboxes at runtime at the server side and added to a
placeholder, some values are set for each of the textboxes.
There is a button that causes the page to be posted back and the
texboxes are re-rendered. When the textboxes are re-rendered the values
are
reset and then these texboxes are again added to the placeholder.

The problem thats happening is that the values of the textboxes are not
getting reset. i.e if the id of the first textbox is textbox_1 and has
a value of 10 after
postback when I generate another textbox with the same name and set a
value of 50 when the page gets rendered the textbox has a value of
10 again!!!

Here's some code that replicates the problem that Ive encountered. Can
someone please point out why this is happening??
<!-- Html -->
<form id="Form1" method="post" runat="server">
<asp:PlaceHolder ID="ph" Runat="server"></asp:PlaceHolder>
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 48px; POSITION:
absolute; TOP: 168px" runat="server" Text="Button"></asp:Button>
</form>

<!-- Html -->

<!-- Server side code -->

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then
abc()
Else
abc2()
End If
End Sub

Private Sub abc()
Dim intcounter As Integer
Dim obj As System.Web.UI.HtmlControls.HtmlInputText
Dim btn As Button
Try
For intcounter = 0 To 2
obj = New System.Web.UI.HtmlControls.HtmlInputText
obj.ID = intcounter.ToString
obj.Value = intcounter
ph.Controls.Add(obj)
Next
Catch ex As Exception
Throw ex
End Try
End Sub

Private Sub abc2()
Dim intcounter As Integer
Dim obj As System.Web.UI.HtmlControls.HtmlInputText
Dim btn As Button
Try
For intcounter = 0 To 1
obj = New System.Web.UI.HtmlControls.HtmlInputText
obj.ID = intcounter.ToString
obj.Value = CInt(intcounter + 99)
ph.Controls.Add(obj)

Next

Catch ex As Exception
Throw ex
End Try
End Sub
<!-- Server side code -->

Andy

Apr 4 '06 #1
0 1572

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

Similar topics

1
4003
by: Rhy Mednick | last post by:
I'm creating a custom control (inherited from UserControl) that is displayed by other controls on the form. I would like for the control to disappear when the user clicks outside my control the...
1
3077
by: rp | last post by:
Hi, How would I create runtime tabpages with runtime controls(textBoxes,comboBox, checkedListBox), inserting,deleting, and modifing the tabpages whenever I want to in C#? Thanks in advance. rp
9
2799
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am...
0
1445
by: Venus | last post by:
Hello, After trying some ways to do it I wanted to use something like the code below but for some reason is not working (I have to generate the entire form dynamically (not only the controls)):...
6
436
by: Nathan Sokalski | last post by:
I am trying to dynamically add controls to my page, but am having trouble with controls such as buttons. I have been able to add simple controls such as Label controls, because they can be placed...
2
4426
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
0
2189
by: Slawomir Nasiadka | last post by:
Hi, I'am new to this group so I would like to say "Hello" everyone and here is my problem: I'm writing a simple application (code is at the end of this message) witch would list all mails...
1
1098
by: vingomail | last post by:
I want a sample code for how to retrieve the values of dynamically generated text box controls at runtime. One thing I ve more number of textbox controls and also generated ids for that controls. i...
2
6124
by: sagarbakliwal | last post by:
hi, I am new to this forum but i have received a lot of help reading through this forum. I am creating a VB.NET application for Patient Record Management. In this there are various combo boxes and...
0
7130
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,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4918
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...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3098
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...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
295
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...

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.