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 |
![]() |
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 |
![]() |
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 |
![]() |
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 |
![]() |
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 |
![]() |