Adding a Default ListItem in a Databound Listbox in ASP.NET- Postback

H

Harold

I have read the following article
http://www.4guysfromrolla.com/webtech/073101-1.shtml. I added the empty
selection as the first option. When I click submit without choosing
anything it does not return my empty string but the 2nd value in listbox.
When the page reloads it selects the 2nd option by default even thou my
Empty field is in the list. What do I have to do to get the empty field and
databound data to act as one?
 
S

Scott Jacobsen

Harold said:
I have read the following article
http://www.4guysfromrolla.com/webtech/073101-1.shtml. I added the empty
selection as the first option. When I click submit without choosing
anything it does not return my empty string but the 2nd value in listbox.
When the page reloads it selects the 2nd option by default even thou my
Empty field is in the list. What do I have to do to get the empty field
and databound data to act as one?

After you insert the first item, you could do:
dropDownList.Items[0].Selected = true;

thus ensuring the first item is the selected item.
 
H

Harold

I have the if statement and it still gives me the same results. When I
debug the code ddlcatcode.Items(0).text = "" when the page loads. When I
press the search button I get ddlcatcode.Items(0).text = "13120" and I
should get ddlcatcode.Items(0).text = "" because I didn't choose anything
from that listbox. When the form is posted back it seems to loose the
dynamic listitem I added.
 
H

Harold

I am now doing this and it seems to be working. Not sure if this is how it
should be done or not.

If Not Page.IsPostBack Then

GetData(strSQL)

Else

ddlCatCode.Items.Insert(0, New ListItem("", ""))

End If

Private Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreRenderComplete

'Add an empty row to catcode dropdownlist

If Not Page.IsPostBack Then

ddlCatCode.Items.Insert(0, New ListItem("", ""))

End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top