Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > The first intem in a DropDownList is vanishing!

Reply
Thread Tools

The first intem in a DropDownList is vanishing!

 
 
mark4asp
Guest
Posts: n/a
 
      06-01-2007
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:

<aspropDownList 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);
}

 
Reply With Quote
 
 
 
 
Dunc
Guest
Posts: n/a
 
      06-01-2007
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 <(E-Mail Removed)> 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:
>
> <aspropDownList 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);
> }



 
Reply With Quote
 
 
 
 
mark4asp
Guest
Posts: n/a
 
      06-01-2007
On 1 Jun, 16:30, Dunc <(E-Mail Removed)> 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 <(E-Mail Removed)> 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:

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DropDownList inside GridView: How to set SelectedIndex for the DropDownList? keithb ASP .Net 1 11-01-2006 05:24 AM
GridView: Filter DropDownList for another DropDownList =?Utf-8?B?SnVhbmpv?= ASP .Net 0 12-29-2005 07:44 AM
GridView: Filtr DropDownList from another DropDownList =?Utf-8?B?SnVhbmpv?= ASP .Net 0 12-23-2005 01:31 PM
databinding a Dropdownlist to another dropdownlist tshad ASP .Net 8 10-19-2005 10:00 PM
Using a data-bind dropdownlist to populate another data-bind dropdownlist mr2_93 ASP .Net 1 10-02-2005 05:07 PM



Advertisments