Board index » Web Programming » Adding a Default ListItem in a Databound Listbox in ASP.NET- Postback

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

Web Programming236
I have read the following article

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?


-
 

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

In your Page Load - around the BindData section - put an if/then/postback

block:

if not Page.IsPostback then

BindData

end if



David Wier

MCP, MVP ASP.NET, ASPInsider

aspnet101.com">aspnet101.com

aspexpress.com">aspexpress.com

Coming Soon - Data Management Toolkit

Manage MSDE/SQL Server/MySQL/MS Access Databases from one location.

augustwind.com/augustwind/dmt.aspx">augustwind.com/augustwind/dmt.aspx









"Harold" <reply@togroup.com>wrote in message

Quote
I have read the following article

www.4guysfromrolla.com/webtech/073101-1.shtml.">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?









-

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

Harold wrote:



Quote
I have read the following article

www.4guysfromrolla.com/webtech/073101-1.shtml.">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.

--

Scott

-

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

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.





"David Wier" <dwier@nospamASPNet101.com>wrote in message

Quote
In your Page Load - around the BindData section - put an if/then/postback

block:

if not Page.IsPostback then

BindData

end if



David Wier

MCP, MVP ASP.NET, ASPInsider

aspnet101.com">aspnet101.com

aspexpress.com">aspexpress.com

Coming Soon - Data Management Toolkit

Manage MSDE/SQL Server/MySQL/MS Access Databases from one location.

augustwind.com/augustwind/dmt.aspx">augustwind.com/augustwind/dmt.aspx









"Harold" <reply@togroup.com>wrote in message

news:enJeylT3EHA.4092@TK2MSFTNGP14.phx.gbl...

>I have read the following article

>www.4guysfromrolla.com/webtech/073101-1.shtml.">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?

>

>









-

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

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











"Harold" <reply@togroup.com>wrote in message

Quote
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.





"David Wier" <dwier@nospamASPNet101.com>wrote in message

news:OhyvK4T3EHA.3380@TK2MSFTNGP09.phx.gbl...

>In your Page Load - around the BindData section - put an if/then/postback

>block:

>if not Page.IsPostback then

>BindData

>end if

>

>David Wier

>MCP, MVP ASP.NET, ASPInsider

>aspnet101.com">aspnet101.com

>aspexpress.com">aspexpress.com

>Coming Soon - Data Management Toolkit

>Manage MSDE/SQL Server/MySQL/MS Access Databases from one location.

>augustwind.com/augustwind/dmt.aspx">augustwind.com/augustwind/dmt.aspx

>

>

>

>

>"Harold" <reply@togroup.com>wrote in message

>news:enJeylT3EHA.4092@TK2MSFTNGP14.phx.gbl...

>>I have read the following article

>>www.4guysfromrolla.com/webtech/073101-1.shtml.">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?

>>

>>

>

>









-