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

Discussion in 'ASP .Net' started by Harold, Dec 8, 2004.

  1. Harold

    Harold Guest

    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?
    Harold, Dec 8, 2004
    #1
    1. Advertising

  2. Harold

    David Wier Guest

    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
    http://aspnet101.com
    http://aspexpress.com
    Coming Soon - Data Management Toolkit
    Manage MSDE/SQL Server/MySQL/MS Access Databases from one location.
    http://augustwind.com/augustwind/dmt.aspx




    "Harold" <> wrote in message
    news:...
    > 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?
    >
    >
    David Wier, Dec 8, 2004
    #2
    1. Advertising

  3. Harold wrote:

    > 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.
    --
    Scott
    Scott Jacobsen, Dec 8, 2004
    #3
  4. Harold

    Harold Guest

    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" <> wrote in message
    news:...
    > 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
    > http://aspnet101.com
    > http://aspexpress.com
    > Coming Soon - Data Management Toolkit
    > Manage MSDE/SQL Server/MySQL/MS Access Databases from one location.
    > http://augustwind.com/augustwind/dmt.aspx
    >
    >
    >
    >
    > "Harold" <> wrote in message
    > news:...
    >> 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?
    >>
    >>

    >
    >
    Harold, Dec 8, 2004
    #4
  5. Harold

    Harold Guest

    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" <> wrote in message
    news:...
    >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" <> wrote in message
    > news:...
    >> 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
    >> http://aspnet101.com
    >> http://aspexpress.com
    >> Coming Soon - Data Management Toolkit
    >> Manage MSDE/SQL Server/MySQL/MS Access Databases from one location.
    >> http://augustwind.com/augustwind/dmt.aspx
    >>
    >>
    >>
    >>
    >> "Harold" <> wrote in message
    >> news:...
    >>> 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?
    >>>
    >>>

    >>
    >>

    >
    >
    Harold, Dec 8, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mad Scientist Jr
    Replies:
    2
    Views:
    425
    Mad Scientist Jr
    Jun 29, 2004
  2. Jim in Arizona
    Replies:
    7
    Views:
    892
    Badass Scotsman
    Nov 22, 2006
  3. rodchar
    Replies:
    0
    Views:
    376
    rodchar
    Nov 26, 2007
  4. AndyW

    Adding ListItem to start of ListBox

    AndyW, Nov 18, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    110
    Barry
    Nov 18, 2004
  5. Manu
    Replies:
    0
    Views:
    157
Loading...

Share This Page