• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

DropDownList problem in .NET

mooncancook

Platinum Member
I have a DropDownList (DDL) control and a button. I populate the DDL in the Page_Load sub. When the button is clicked the form is submitted, and I try to read the value of the selected item of the DDL. So something like this:

Sub Page_Load
if not isPostBack then
'Populate ddl_1 .......
end if
End Sub

Sub btn1_Clicked
dim selItem, selIndex
SelIndex = ddl_1.SelectedIndex
SelItem = ddl_1.SelectedItem.Value
End Sub

<form name=Form1 runat=server>
<asp: DropDownList id=ddl_1 runat=server />
<asp: button id=btn1 runat=server text="Submit" onClick="btn1_Clicked">
</form>

If I remove the isPostBack in Page_Load, I always get the first item in the DDL no matter what I select. With the isPostBack in place, SelIndex always get the value -1, and the SelItem line will give me an "null reference" exception. Then if I comment out the SelItem line, the SelIndex still gets -1, and the DDL will not have any data in it. What I am doing wrong here?
 
Please use [code][/code] tags

What sort of browser are you using? also, do you have a sample of who you are populating the dropdown box, I think it might be related to the issue that you are having.
 
Thanks but I got it out. It's a noob mistake. I had the ViewState disabled that's why it wouldn't work. Working now after I enable ViewState. I'll have to be more careful when copying and pasting codes
 
Thanks but I got it out. It's a noob mistake. I had the ViewState disabled that's why it wouldn't work. Working now after I enable ViewState. I'll have to be more careful when copying and pasting codes

🙂 Glad you got it. I'm just starting my entrance into ASP programming from a primarily c++ background. Lots to learn, but it looks pretty nifty.
 
Back
Top