Value of Control created dynamically is not getting populated on post back

Hi

I am creating a table at run time. If I put some value to textbox, its not getting populated after post back. If I do it on Page onInit event everything works fine.. but I cant do this as I have to make rows on button click.
The code is like this
Sub MakeTable()
Dim ht As New Hashtable
ht("1") = "1"
ht("2") = "2"
For i As Integer = 0 To 2
Dim tr As New TableRow
For j As Integer = 0 To 1
Dim td As New TableCell
Select Case (j)
Case 0 : Dim dd As New DropDownList
dd.ID = "name" & i
dd.AutoPostBack = True
dd.DataSource = ht.Keys
dd.DataBind()
dd.Attributes.Add("runat", "server")
dd.Attributes.Add("onChange", "onComboChanged(" & i & "); ")
td.Controls.Add(dd)
Case 1
: Dim txt As New HtmlInputText
txt.ID = "address" & i
txt.Attributes.Add("runat", "server")
txt.Value = Request.Form("address" & i)
td.Controls.Add(txt)
End Select
tr.Cells.Add(td)
Next
tbl.Rows.Add(tr)
Next
End Sub

Private Sub Page_Load()
MakeTable()
If Not Page.IsPostBack Then
Session("address0") = "1"
End If
If Page.IsPostBack Then
FillTextBox()
End If
End Sub
Sub FillTextBox()
Dim ob As New HtmlInputText
ob = FindControl("address0")
ob.Value = Session("address" & "0")
End Sub

<HTML>
<script language="javascript">
function onComboChanged(i){

frm.submit() ;
}

</script>
<body>
<form id="frm" runat="server">
<asp:Table ID="tbl" Runat="server"></asp:Table>
</form>
</body>
</HTML>
Thanks in advance
1
prasoontripathi5
7/17/2004 1:21:06 PM
asp.net.web-forms 93655 articles. 3 followers. Follow

4 Replies
540 Views

Similar Articles

[PageSpeed] 38

The initial value of the input controls should be set one time (If Not Page.IsPostBack Then) . This information is maintained by the IPostBackDataHandler interface (implemented among others by HtmlInputText), making it unecessary to reset the value for subsequent posts.


Leon Langleyben

MCSD, ASP.NET MVP

Blog
-1
llangleyben
7/18/2004 3:20:59 PM
Thanks but if I am right my problem is somewhat different. I want to populate the input box value based on the value of combo box. eg; if combo value is "TX" then text box value should be "77005". Therefore I dont really want to set any initial value for the text box rather it should be set on postback done on combo changed. 

BTW if I check the input box value with Response.write it shows the desired value, but the textbox does not show that.
Private Sub Page_Load()
If Not Page.IsPostBack Then
Session("Name") = "previous"
End If
Session("Name") = "updated"
MakeTable()
End Sub
Sub MakeTable()
Dim ht As New Hashtable
ht("1") = "1"
ht("2") = "2"
For i As Integer = 0 To 2
Dim tr As New TableRow
For j As Integer = 0 To 2
Dim td As New TableCell
Select Case (j)
Case 0 : Dim dd As New DropDownList
dd.ID = "name" & i
dd.AutoPostBack = True
dd.DataSource = ht.Keys
dd.DataBind()
dd.Attributes.Add("runat", "server")
dd.Attributes.Add("onChange", "onComboChanged(" & i & "); ")
td.Controls.Add(dd)
Case 1
: Dim txt As New TextBox
txt.ID = "address" & i
txt.Attributes.Add("runat", "server")
Response.write(Session("Name"))
txt.Text = Session("Name")
Response.write(txt.Text)
td.Controls.Add(txt)
End Select
tr.Cells.Add(td)
Next
tbl.Rows.Add(tr)
Next
End Sub

<HTML>
<script language="javascript">
function onComboChanged(i){

frm.submit() ;
}

</script>
<body>
<form id="frm" runat="server">
<asp:Table ID="tbl" Runat="server"></asp:Table>
</form>
</body>
</HTML>
Here the statement
Response.write(txt.Text)
shows the value "next"
But the textbox on screen always shows "previuos"
-1
prasoontripathi5
7/19/2004 6:20:01 AM
Now I am confused. This way you should always receive "updated". You always override session variable to it in Page_Load. You never set Session("Name") to "next". Am I right?
Leon Langleyben

MCSD, ASP.NET MVP

Blog
1
llangleyben
7/19/2004 5:38:48 PM

Edited by SomeNewKid. Please post code between <code> and </code> tags.


I m sorry, I did two mistakes in the last post.
1st one - in place of this code
Session("Name") = "updated"
MakeTable()
the code should be like this
MakeTable()
Session("Name") = "updated"
and in place of "next" , I meant to say "updated"

Actually the code I wrote in last post should be like this -
Private Sub Page_Load() 

If Not Page.IsPostBack Then
Session("Name") = "previous"
End If
MakeTable()
Session("Name") = "updated"

End Sub
Sub MakeTable()
Dim ht As New Hashtable
ht("1") = "1"
ht("2") = "2"
For i As Integer = 0 To 2
Dim tr As New TableRow
For j As Integer = 0 To 2
Dim td As New TableCell
Select Case (j)
Case 0 : Dim dd As New DropDownList
dd.ID = "name" & i
dd.AutoPostBack = True
dd.DataSource = ht.Keys
dd.DataBind()
dd.Attributes.Add("runat", "server")
dd.Attributes.Add("onChange", "onComboChanged(" & i & "); ")
td.Controls.Add(dd)
Case 1
: Dim txt As New TextBox
txt.ID = "address" & i
txt.Attributes.Add("runat", "server")
Response.write(Session("Name"))
txt.Text = Session("Name")
Response.write(txt.Text)
td.Controls.Add(txt)
End Select
tr.Cells.Add(td)
Next
tbl.Rows.Add(tr)
Next
End Sub


<HTML> 

<script language="javascript">
function onComboChanged(i){
frm.submit() ;
}
</script>
<body>
<form id="frm" runat="server">
<asp:Table ID="tbl" Runat="server"></asp:Table>
</form>
</body>
</HTML>

Here the statement
Response.write(txt.Text)
shows the value "updated"
But the textbox on screen always shows "previuos"
1
prasoontripathi5
7/20/2004 5:43:13 AM
Reply:

Web resources about - Value of Control created dynamically is not getting populated on post back - asp.net.web-forms

Placebase team at Apple file “Schematic Maps” patent dynamically detailing important data
In a report from Computerworld way back in 2009, we learned that Apple had quietly acquired Maps API company Placebase. Then, founder of Placebase ...

Apple's Lightning port dynamically assigns pins to allow for reversible use
... a closer look at how Apple's new Lightning connector works, and has come to the conclusion that the 8 pins on each side of the plug are dynamically ...

In a dynamically typed language, is it a bad idea to return different data types?
In some cases, returning different types from a single function works.

Urlbox Launches Screenshots-as-a-Service Platform to Dynamically Update Galleries
... changes. Launched yesterday, Urlbox allows anyone with a gallery displaying website screenshots to automate the process and keep them dynamically ...

Everything.me launcher dynamically adapts to what you’re doing right now
... phone according to what you’re doing. Perform a search for ‘football’ and your background will change to the NFL logo. A launcher that dynamically ...


A YouTube Experiment Resizes the Player Dynamically
There's a new YouTube experiment that changes the video player's size, depending on the browser window's size. When resizing Chrome's window, ...

Apple Granted “Dynamically Changing Cursor for User Interface” Patent, Originally Filed in 2003
... a utility patent that covers the use of the mouse cursor that changes according to the context of the task it is engaged in. Called a “Dynamically ...

Atmel's FPSLIC II Dynamically Reconfigurable SoC Supports "Silicon-Sharing" For Peripherals & Interfaces ...
Silicon-Sharing Fits 200K Gate Design into 40K Gate FPSLIC II's, Cuts Power Drain by 97%

Everything.me Launcher Wants to Dynamically Change Phones Based on Location, Interests, and Mental State ...
What if your phone’s launcher dynamically changed based upon interests, your location, or topics you had in mind at a specific moment? A new ...

Resources last updated: 1/17/2016 5:57:10 AM