PDA

View Full Version : Page_Load not working?!?


Punkcrib
08-29-2007, 11:38 PM
Hello. I have a seemingly simple problem that I cannot figure out.

I have 3 dropdownlists (Category, Subcategory, and Item). The Subcat. DDL is hidden until the 1st is changed by the user, and the Item DDL is hidden until the 2nd DDL is changed. Once the user selects an item from the "Item" DDL, a DetailView of the selected item becomes visible below the boxes. This is a very simple project so far.

Well My DDL's are pulling their items from a DB, and thus the 1st item of the list is automatically selected. This causes a problem when the list only contains 1 item, since there is no way to change that DDL's selectedvalue and cause an AutoPostBack.

So what I wanted to do was add a "blank" item to each DDL right before it's Visible parameter is changed to True. Well the 1st DDL is displayed with the page, so obviously this means I should place this code in the Page_Load function, right?


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
IF Not Page.IsPostBack Then
Me.DropDownList1.Items.Insert(0, new ListItem())
END IF
END SUB

So why isn't this working????????? The only thing I have been able to figure out is that if I remove the IF statement, then a blank line will ONLY be added to the DDL after a PostBack, and NOT on the initial Load. What is causing this? How do I get it to perform the statement on 1st load and not subsequently?

any help is much appreciated. I am pretty new to ASP.net and especially VB (I'm a Java guy, but my boss isn't, so that means I am not either:confused: ), but this seems relatively straight forward. yet I can't get it to work.

Thanks!

nikkiH
08-29-2007, 11:52 PM
Why don't you just hard-code it in the ListItems, and set AppendDataBoundItems to true?

(To Clarify, use an empty <asp:listitem>, not vb code)

ad5qa
09-04-2007, 10:35 PM
I think it is the Me.Load or Me.DropDownList maybe it should be mE.dROPdOWNlIST.

just kidding, you vbers are great.

dropdownlistA.AppendDataBoundItems = true

see if that works

jleone
09-19-2007, 04:29 PM
I create dropdown lists from DB too. I always hardcode a blank row to start off with, forcing the user to select a value, thus being able to use the SelectedIndex change. I just hardcoded in an empty string ("") in my subroutine in my .dll before I being my loop through my data reader.