G
Guest
I am unable to retrieve the values of DropDownLists upon PostBack. Before
you ask, yes I am using Page_Load to handle the PostBack and no I'm not
overwriting the old values by repopulating the DDL's.
I had a page with some quite complex code but soon found that when I tried
to read the values posted back from DDL's they were all empty strings.
ASP.Net textboxes are fine and I am able to retreive those values but not the
DDL's. I cut back the code trying to resolve the problem until I eventually
decided to try the simplest example possible to see if it worked. I should
point out that if the DDL is hard coded i.e. not populated with a databind it
works fine.
Here's the aspx page:
<form id="ddlForm" runat="server">
<aspropDownList ID="ddl" runat="server"></aspropDownList>
<input type="submit" value="submit" />
<p id="ddlValue" style="color:Red;" runat="server"></p>
</form> and here's the code behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not IsPostBack Then
populateDDL(ddl)
Else 'UNABLE TO RETREIVE VALUE HERE
ddlValue.InnerHtml = ddl.SelectedValue
End If
End Sub
Private Sub populateDDL(ByVal ddl As DropDownList)
Dim counter As Integer
For counter = 0 To 12
ddl.Items.Add(New ListItem("Item" & counter.ToString))
Next
End Sub
All pretty simple as you can see and as you would expect it works perfectly
when I try it on my home computer. If I try it on my PC at work however I am
unable to get the values of the DDL where commented. I can only assume that
there is something wrong with my .Net install or something similar.
I have tried all sorts of things like the text property, value property etc.
In fact the ddl.selectedItem is Nothing. When the page reloads, my DDL has
nothing in it on the page i.e it hasn't kept its state whatsoever. When i try
it on the working machine the <p> tag gets the correct value i.e. Item0 or
Item4 etc and the DDL has all the values in it with the value i choose still
selected as you would expect. The ViewState hidden field has an expectedly
large encrypted value in it. On the broken machine however, the ViewState is
only about 30 charcters long and the DDL has no values and as such the <p>
has no value. Interestingly though I can retreive the value through
Request.Form.
I've come to the conclusion that there is something wrong with my ViewState
perhaps. Clearly this is a major problem. Any idea on how to fix it would be
appreciated. I've compared my Machine.Config and Web.Config from a working
machine to the one on the problem machine and they are the same.
Any idea as to the problem?
Thanks in advance,
Chris
you ask, yes I am using Page_Load to handle the PostBack and no I'm not
overwriting the old values by repopulating the DDL's.
I had a page with some quite complex code but soon found that when I tried
to read the values posted back from DDL's they were all empty strings.
ASP.Net textboxes are fine and I am able to retreive those values but not the
DDL's. I cut back the code trying to resolve the problem until I eventually
decided to try the simplest example possible to see if it worked. I should
point out that if the DDL is hard coded i.e. not populated with a databind it
works fine.
Here's the aspx page:
<form id="ddlForm" runat="server">
<aspropDownList ID="ddl" runat="server"></aspropDownList>
<input type="submit" value="submit" />
<p id="ddlValue" style="color:Red;" runat="server"></p>
</form> and here's the code behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not IsPostBack Then
populateDDL(ddl)
Else 'UNABLE TO RETREIVE VALUE HERE
ddlValue.InnerHtml = ddl.SelectedValue
End If
End Sub
Private Sub populateDDL(ByVal ddl As DropDownList)
Dim counter As Integer
For counter = 0 To 12
ddl.Items.Add(New ListItem("Item" & counter.ToString))
Next
End Sub
All pretty simple as you can see and as you would expect it works perfectly
when I try it on my home computer. If I try it on my PC at work however I am
unable to get the values of the DDL where commented. I can only assume that
there is something wrong with my .Net install or something similar.
I have tried all sorts of things like the text property, value property etc.
In fact the ddl.selectedItem is Nothing. When the page reloads, my DDL has
nothing in it on the page i.e it hasn't kept its state whatsoever. When i try
it on the working machine the <p> tag gets the correct value i.e. Item0 or
Item4 etc and the DDL has all the values in it with the value i choose still
selected as you would expect. The ViewState hidden field has an expectedly
large encrypted value in it. On the broken machine however, the ViewState is
only about 30 charcters long and the DDL has no values and as such the <p>
has no value. Interestingly though I can retreive the value through
Request.Form.
I've come to the conclusion that there is something wrong with my ViewState
perhaps. Clearly this is a major problem. Any idea on how to fix it would be
appreciated. I've compared my Machine.Config and Web.Config from a working
machine to the one on the problem machine and they are the same.
Any idea as to the problem?
Thanks in advance,
Chris