PC Review


Reply
Thread Tools Rate Thread

How to: Add "ListItems" to DropDownLists that are binded

 
 
=?Utf-8?B?Y2hhcmxpZXdlc3Q=?=
Guest
Posts: n/a
 
      17th Feb 2005
I have binded a dropdownlist control to a data view. I am trying to add a
listitem to this already binded control, however, i cannot figure this out.
My code is:

// code to create data view object (dv)

DropDownList1.DataSource = dv;
DropDownList1.DataMember = "vStA";
DropDownList1.DataTextField = "vStAText";
DropDownList1.DataValueField = "vStAId";
if (bNewQuestion)
{
DropDownList1.Items.Insert(0, (new ListItem(" ---- ", "")));
DropDownList1.SelectedIndex = 0;
}

Is it simply not possible to add ListItems to already binded controls? Or is
it my code?

Thanks!
 
Reply With Quote
 
 
 
 
ED_C#
Guest
Posts: n/a
 
      17th Feb 2005
You might want to try something like this

for (int i=0;i<=dv.Tables["Table"].rows.count-1;i++)
{
ListItem li=new ListItem();
li.DataTextField=dv.tables["table"].rows[i]["ColumnName"];
li.DataValueField=dv.tables["table"].rows[i]["ColumnName"];
DropDownList1.items.add(li);
}

This method doesn't bind the data to the control and you should be able to
add values later using the same method.

Good Luck.


"charliewest" <(E-Mail Removed)> wrote in message
news:74E17511-6D6C-462E-8A8B-(E-Mail Removed)...
>I have binded a dropdownlist control to a data view. I am trying to add a
> listitem to this already binded control, however, i cannot figure this
> out.
> My code is:
>
> // code to create data view object (dv)
>
> DropDownList1.DataSource = dv;
> DropDownList1.DataMember = "vStA";
> DropDownList1.DataTextField = "vStAText";
> DropDownList1.DataValueField = "vStAId";
> if (bNewQuestion)
> {
> DropDownList1.Items.Insert(0, (new ListItem(" ---- ", "")));
> DropDownList1.SelectedIndex = 0;
> }
>
> Is it simply not possible to add ListItems to already binded controls? Or
> is
> it my code?
>
> Thanks!



 
Reply With Quote
 
=?Utf-8?B?Y2hhcmxpZXdlc3Q=?=
Guest
Posts: n/a
 
      17th Feb 2005
For some reason this is not working either. I have this code inserted w/in a
switch block, which appears to be causing the problem... although i cannot
see how.

Using VS.NET, the intellisense doesn't automatically recognize the ListItem
when instatiated with the "new" key word. However, when i create the ListItem
"outside" the switch block, intellisense automatically recognizes the
ListItem object immediately right typing the "new" constructor to instantiate
it.

I know this makes no sense. Maybe it's a bug w/ my VS.NET?

"ED_C#" wrote:

> You might want to try something like this
>
> for (int i=0;i<=dv.Tables["Table"].rows.count-1;i++)
> {
> ListItem li=new ListItem();
> li.DataTextField=dv.tables["table"].rows[i]["ColumnName"];
> li.DataValueField=dv.tables["table"].rows[i]["ColumnName"];
> DropDownList1.items.add(li);
> }
>
> This method doesn't bind the data to the control and you should be able to
> add values later using the same method.
>
> Good Luck.
>
>
> "charliewest" <(E-Mail Removed)> wrote in message
> news:74E17511-6D6C-462E-8A8B-(E-Mail Removed)...
> >I have binded a dropdownlist control to a data view. I am trying to add a
> > listitem to this already binded control, however, i cannot figure this
> > out.
> > My code is:
> >
> > // code to create data view object (dv)
> >
> > DropDownList1.DataSource = dv;
> > DropDownList1.DataMember = "vStA";
> > DropDownList1.DataTextField = "vStAText";
> > DropDownList1.DataValueField = "vStAId";
> > if (bNewQuestion)
> > {
> > DropDownList1.Items.Insert(0, (new ListItem(" ---- ", "")));
> > DropDownList1.SelectedIndex = 0;
> > }
> >
> > Is it simply not possible to add ListItems to already binded controls? Or
> > is
> > it my code?
> >
> > Thanks!

>
>
>

 
Reply With Quote
 
=?Utf-8?B?Y2hhcmxpZXdlc3Q=?=
Guest
Posts: n/a
 
      19th Feb 2005
FYI:

I was able to make my code work (the original code) by creating and
inserting the new ListItem "AFTER" calling the DataBind() method on the drop
down list control.

"ED_C#" wrote:

> You might want to try something like this
>
> for (int i=0;i<=dv.Tables["Table"].rows.count-1;i++)
> {
> ListItem li=new ListItem();
> li.DataTextField=dv.tables["table"].rows[i]["ColumnName"];
> li.DataValueField=dv.tables["table"].rows[i]["ColumnName"];
> DropDownList1.items.add(li);
> }
>
> This method doesn't bind the data to the control and you should be able to
> add values later using the same method.
>
> Good Luck.
>
>
> "charliewest" <(E-Mail Removed)> wrote in message
> news:74E17511-6D6C-462E-8A8B-(E-Mail Removed)...
> >I have binded a dropdownlist control to a data view. I am trying to add a
> > listitem to this already binded control, however, i cannot figure this
> > out.
> > My code is:
> >
> > // code to create data view object (dv)
> >
> > DropDownList1.DataSource = dv;
> > DropDownList1.DataMember = "vStA";
> > DropDownList1.DataTextField = "vStAText";
> > DropDownList1.DataValueField = "vStAId";
> > if (bNewQuestion)
> > {
> > DropDownList1.Items.Insert(0, (new ListItem(" ---- ", "")));
> > DropDownList1.SelectedIndex = 0;
> > }
> >
> > Is it simply not possible to add ListItems to already binded controls? Or
> > is
> > it my code?
> >
> > Thanks!

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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
Field Names: "LongName", "ShortName", "Code", "Description","Comments" PeteCresswell Microsoft Access 2 25th Feb 2009 11:41 PM
ReVIEW (Erratum): some shortcut keys not working anymore-----help""""""""PhpApach...WORK WELL!!!! wbrowse@gmail.com Windows XP Help 0 13th Apr 2007 12:29 PM
<FORM METHOD="post" onSubmit="return fieldcheck()" name="orientation" action="http://ws-kitty.BU.edu/AT/survey/orientation/script/write.asp" language="JavaScript"> Joeyej Microsoft ASP .NET 0 4th Jun 2004 08:55 PM
Manual "Windows Update" produces "ActiveX/active scripting" error message even with "LOW" security level setting in "Trusted" Zone Ray2 Windows XP Help 1 14th Nov 2003 06:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:55 PM.