Problem with Controls generated at Runtime

A

anandv81

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
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top