The first intem in a DropDownList is vanishing!

Discussion in 'ASP .Net' started by mark4asp, Jun 1, 2007.

  1. mark4asp

    mark4asp Guest

    The first intem in a DropDownList is vanishing!

    My code to load a DropDownList is shown below. Yet when I load the
    page after a postback there is no zeroth item present.

    lstManager.Items.Clear();

    lstManager.DataSource = _dsData.Tables["manager"].DefaultView;
    lstManager.DataValueField = "ManagerID";
    lstManager.DataTextField = "Name";
    lstManager.DataBind();

    lstManager.Items.Insert(0, new ListItem("none", "0"));


    There's nothing special about the control definition:

    <asp:DropDownList ID="lstManager" runat="server" Height="20px"
    Width="250px" />

    What's going on here?



    This was the old code I replaced. Why does the old code (below) work
    and why is my new code (above) broke?

    lstManager.Items.Clear();

    ListItem li = new ListItem("none","0");
    lstManager.Items.Add(li);

    foreach (DataRow drManager in _dsData.Tables["manager"].Rows)
    {
    ListItem liManager = new ListItem((string)drManager["Name"],
    ((int)drManager["ManagerID"]).ToString());
    lstManager.Items.Add(liManager);
    }
     
    mark4asp, Jun 1, 2007
    #1
    1. Advertising

  2. mark4asp

    Dunc Guest

    I had this one a while back; turns out I was using simple properties
    on a Master Page that required a DataBind() call to bind them. When
    it was called, it was also rebinding the content page, which loses the
    manually added items.

    I recall this issue also happened with an ASCX file on the page, or if
    you call a generic DataBind() any time after the addition of
    ListItems, etc.

    Dunc
    http://www.fluidfoundation.com

    On 1 Jun, 15:24, mark4asp <> wrote:
    > The first intem in a DropDownList is vanishing!
    >
    > My code to load a DropDownList is shown below. Yet when I load the
    > page after a postback there is no zeroth item present.
    >
    > lstManager.Items.Clear();
    >
    > lstManager.DataSource = _dsData.Tables["manager"].DefaultView;
    > lstManager.DataValueField = "ManagerID";
    > lstManager.DataTextField = "Name";
    > lstManager.DataBind();
    >
    > lstManager.Items.Insert(0, new ListItem("none", "0"));
    >
    > There's nothing special about the control definition:
    >
    > <asp:DropDownList ID="lstManager" runat="server" Height="20px"
    > Width="250px" />
    >
    > What's going on here?
    >
    > This was the old code I replaced. Why does the old code (below) work
    > and why is my new code (above) broke?
    >
    > lstManager.Items.Clear();
    >
    > ListItem li = new ListItem("none","0");
    > lstManager.Items.Add(li);
    >
    > foreach (DataRow drManager in _dsData.Tables["manager"].Rows)
    > {
    > ListItem liManager = new ListItem((string)drManager["Name"],
    > ((int)drManager["ManagerID"]).ToString());
    > lstManager.Items.Add(liManager);
    > }
     
    Dunc, Jun 1, 2007
    #2
    1. Advertising

  3. mark4asp

    mark4asp Guest

    On 1 Jun, 16:30, Dunc <> wrote:
    > I had this one a while back; turns out I was using simple properties
    > on a Master Page that required a DataBind() call to bind them. When
    > it was called, it was also rebinding the content page, which loses the
    > manually added items.
    >
    > I recall this issue also happened with an ASCX file on the page, or if
    > you call a generic DataBind() any time after the addition of
    > ListItems, etc.
    >
    > Dunchttp://www.fluidfoundation.com
    >
    > On 1 Jun, 15:24, mark4asp <> wrote:
    >
    >
    >
    > > The first intem in a DropDownList is vanishing!

    >
    > > My code to load a DropDownList is shown below. Yet when I load the
    > > page after a postback there is no zeroth item present.

    >
    > > lstManager.Items.Clear();

    >
    > > lstManager.DataSource = _dsData.Tables["manager"].DefaultView;
    > > lstManager.DataValueField = "ManagerID";
    > > lstManager.DataTextField = "Name";
    > > lstManager.DataBind();

    >
    > > lstManager.Items.Insert(0, new ListItem("none", "0"));

    >
    > > There's nothing special about the control definition:

    >
    > > <asp:DropDownList ID="lstManager" runat="server" Height="20px"
    > > Width="250px" />

    >
    > > What's going on here?

    >
    > > This was the old code I replaced. Why does the old code (below) work
    > > and why is my new code (above) broke?

    >
    > > lstManager.Items.Clear();

    >
    > > ListItem li = new ListItem("none","0");
    > > lstManager.Items.Add(li);

    >
    > > foreach (DataRow drManager in _dsData.Tables["manager"].Rows)
    > > {
    > > ListItem liManager = new ListItem((string)drManager["Name"],
    > > ((int)drManager["ManagerID"]).ToString());
    > > lstManager.Items.Add(liManager);
    > > }- Hide quoted text -

    >
    > - Show quoted text -


    Thanks Dunc, for sharing that with me - that scenario you described
    sounds very like mine.
     
    mark4asp, Jun 1, 2007
    #3
    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. e
    Replies:
    1
    Views:
    3,634
    John Saunders
    Oct 24, 2003
  2. Michael

    Vanishing PARAM="ARCHIVE" tag

    Michael, Mar 4, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    343
    Michael
    Mar 4, 2004
  3. David
    Replies:
    1
    Views:
    348
    Mike Smith
    Jul 12, 2004
  4. No One

    Vanishing buttons mystery "solved"

    No One, Oct 28, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    342
    No One
    Oct 28, 2004
  5. =?Utf-8?B?bnVsbGRldmljZQ==?=

    Datagrid vanishing and taking all my nested controls with it.

    =?Utf-8?B?bnVsbGRldmljZQ==?=, Jun 24, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    457
    =?Utf-8?B?bnVsbGRldmljZQ==?=
    Jun 27, 2005
Loading...

Share This Page