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?
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?