469,926 Members | 2,156 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

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 1436

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Venus | last post: by
6 posts views Thread by Nathan Sokalski | last post: by
2 posts views Thread by ajikoe | last post: by
reply views Thread by Slawomir Nasiadka | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.