Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Help asp:DropDownList

Could anyone give me an example of how to bind a drop down list to a data
store? Here is what i am trying to do.

pass in a value for a clientID

read that client id and prefill form data from my database

that about does it. I have all of the textboxes working perfectly, but I do
not even know which members belong ot the dropdownlist. Also, if anyone
knows a good place to get a list of which members belong to which control,
that would be great. Thank you for the help.

Jeremy Samkowiak
[543 byte] By [Jeremy Samkowiak] at [2007-11-9 16:23:54]
# 1 Re: Help asp:DropDownList
Try using Add or data binding methods:

1)
MyDropdownList.Items.Add( new ListItem("text to be displayed","1"));
MyDropdownList.Items.Add( new ListItem("text to be displayed","2"));
... ...

2)
ArrayList myList;
for( int i =0; i<n; i++)
myList.Add (MyDs.Tables[0].Rows[i][0]);

MyDropdownList.DataSource = myList;
MyDropdownList.DataBind();

M. Yang, MCP
-----------
http://www.foxinternet.net/web2/myang

"Jeremy Samkowiak" <j@samko.com> wrote:
>Could anyone give me an example of how to bind a drop down list to a data
>store? Here is what i am trying to do.
>
>pass in a value for a clientID
>
>read that client id and prefill form data from my database
>
>that about does it. I have all of the textboxes working perfectly, but
I do
>not even know which members belong ot the dropdownlist. Also, if anyone
>knows a good place to get a list of which members belong to which control,
>that would be great. Thank you for the help.
>
>Jeremy Samkowiak
>
>
M. Yang at 2007-11-11 23:26:13 >
# 2 Re: Help asp:DropDownList
Use the ClassBrowser to get all the information you want about the
assemblies (such as an object's members). I have found it to be a fantastic
resource tool. It is embedded in the Quickstart Tutorial that was
distributed with the SDK. (if you installed the quickstart with the default
settings the link will be:
http://localhost/quickstart/aspplus/samples/classbrowser/classbrowser.aspx)
You will need to modify the config.web to view other assemblies. So, if you
wanted to take a look a the System.Web.dll as well, change the following in
the config.web:

<ClassBrowser>
<set key="ASP+ Class Library" value="System.Web.dll" />
<set key="Base Class Library" value="mscorlib.dll" />
</ClassBrowser>

For your request specifically, Look at the System.Web.dll -->
System.Web.UI.WebControls --> DropDownList you will see a list of the
Properties and methods for the DropDownList.

Then, you drill down even further to look at the ListItemCollection, and you
will find additional properties for the actual list items in teh drop down.

Does this help?
Bob Lair

"Jeremy Samkowiak" <j@samko.com> wrote in message
news:39a3e2c2@news.dev-archive.com...
> Could anyone give me an example of how to bind a drop down list to a data
> store? Here is what i am trying to do.
>
> pass in a value for a clientID
>
> read that client id and prefill form data from my database
>
> that about does it. I have all of the textboxes working perfectly, but I
do
> not even know which members belong ot the dropdownlist. Also, if anyone
> knows a good place to get a list of which members belong to which control,
> that would be great. Thank you for the help.
>
> Jeremy Samkowiak
>
>
Robert Lair at 2007-11-11 23:27:20 >
# 3 Re: Help asp:DropDownList
Robert,
This is great information and solves a lot more than my original problem. I
thank you for the information, and the fast response. I have not unlocked
the power of config.web at this point in time, so it looks as though that is
going to have to be my next step. Make sure you have a good one, and thanks
again.

Jeremy

"Robert Lair" <ngws@asppages.com> wrote in message
news:39a5361f$1@news.dev-archive.com...
> Use the ClassBrowser to get all the information you want about the
> assemblies (such as an object's members). I have found it to be a
fantastic
> resource tool. It is embedded in the Quickstart Tutorial that was
> distributed with the SDK. (if you installed the quickstart with the
default
> settings the link will be:
>
http://localhost/quickstart/aspplus/samples/classbrowser/classbrowser.aspx)
> You will need to modify the config.web to view other assemblies. So, if
you
> wanted to take a look a the System.Web.dll as well, change the following
in
> the config.web:
>
> <ClassBrowser>
> <set key="ASP+ Class Library" value="System.Web.dll" />
> <set key="Base Class Library" value="mscorlib.dll" />
> </ClassBrowser>
>
> For your request specifically, Look at the System.Web.dll -->
> System.Web.UI.WebControls --> DropDownList you will see a list of the
> Properties and methods for the DropDownList.
>
> Then, you drill down even further to look at the ListItemCollection, and
you
> will find additional properties for the actual list items in teh drop
down.
>
> Does this help?
> Bob Lair
>
> "Jeremy Samkowiak" <j@samko.com> wrote in message
> news:39a3e2c2@news.dev-archive.com...
> > Could anyone give me an example of how to bind a drop down list to a
data
> > store? Here is what i am trying to do.
> >
> > pass in a value for a clientID
> >
> > read that client id and prefill form data from my database
> >
> > that about does it. I have all of the textboxes working perfectly, but
I
> do
> > not even know which members belong ot the dropdownlist. Also, if anyone
> > knows a good place to get a list of which members belong to which
control,
> > that would be great. Thank you for the help.
> >
> > Jeremy Samkowiak
> >
> >
>
>
Jeremy Samkowiak at 2007-11-11 23:28:19 >
# 4 Re: Help asp:DropDownList
The following is an completed example to bind a dropdownlist to a ds directly:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>

<html>

<script language="C#" runat="server" >
void Page_Load(Object Source, EventArgs E)
{
if(IsPostBack==false)
{
string sConn = "server=(local);Database=Northwind;User ID=Sa;Password=;";
string sSQL = "Select OrderID, CustomerID FROM Orders";
SQLConnection cn = new SQLConnection(sConn);
SQLDataSetCommand cmd= new SQLDataSetCommand(sSQL,cn);
DataSet ds = new DataSet();
cmd.FillDataSet(ds,"Orders");

OrderList.DataSource = ds.Tables[0].DefaultView;
OrderList.DataBind();

lblMessage.Text = "";
}
}

void btnOS_click(Object Sender, EventArgs E)
{
lblMessage.Text = "Order ID: " + OrderList.SelectedItem.Text;
}

</script>

<body>
<Form runat="server">

<h2>DropDownList Data Binding Example</h2>
<p />
<asp:DropDownList id="OrderList" runat="server" DataValueField="OrderID"
/>

<asp:Button id="btnOS" onclick="btnOS_click" Text="Select" runat="server"
/><br>
<asp:Label id="lblMessage" runat="server" />

</form>
</body>
</html>

"Jeremy Samkowiak" <j@samko.com> wrote:
>Could anyone give me an example of how to bind a drop down list to a data
>store? Here is what i am trying to do.
>
>pass in a value for a clientID
>
>read that client id and prefill form data from my database
>
>that about does it. I have all of the textboxes working perfectly, but
I do
>not even know which members belong ot the dropdownlist. Also, if anyone
>knows a good place to get a list of which members belong to which control,
>that would be great. Thank you for the help.
>
>Jeremy Samkowiak
>
>
Mingyong Yang at 2007-11-11 23:29:19 >
# 5 Re: Help asp:DropDownList
Thank you again, but after i bind the information, how do i tell it which
one i would like selected. so when it is binding the correct one form the
database is automatically shown? Any help will be greatly appreciated.

Thanks,
Jeremy

"Mingyong Yang" <myang@foxinternet.net> wrote in message
news:39a55510$1@news.dev-archive.com...
>
> The following is an completed example to bind a dropdownlist to a ds
directly:
>
> <%@ Import Namespace="System.Data" %>
> <%@ Import Namespace="System.Data.SQL" %>
>
> <html>
>
> <script language="C#" runat="server" >
> void Page_Load(Object Source, EventArgs E)
> {
> if(IsPostBack==false)
> {
> string sConn = "server=(local);Database=Northwind;User
ID=Sa;Password=;";
> string sSQL = "Select OrderID, CustomerID FROM Orders";
> SQLConnection cn = new SQLConnection(sConn);
> SQLDataSetCommand cmd= new SQLDataSetCommand(sSQL,cn);
> DataSet ds = new DataSet();
> cmd.FillDataSet(ds,"Orders");
>
> OrderList.DataSource = ds.Tables[0].DefaultView;
> OrderList.DataBind();
>
> lblMessage.Text = "";
> }
> }
>
> void btnOS_click(Object Sender, EventArgs E)
> {
> lblMessage.Text = "Order ID: " + OrderList.SelectedItem.Text;
> }
>
> </script>
>
> <body>
> <Form runat="server">
>
> <h2>DropDownList Data Binding Example</h2>
> <p />
> <asp:DropDownList id="OrderList" runat="server" DataValueField="OrderID"
> />
>
> <asp:Button id="btnOS" onclick="btnOS_click" Text="Select"
runat="server"
> /><br>
> <asp:Label id="lblMessage" runat="server" />
>
> </form>
> </body>
> </html>
>
>
>
> "Jeremy Samkowiak" <j@samko.com> wrote:
> >Could anyone give me an example of how to bind a drop down list to a data
> >store? Here is what i am trying to do.
> >
> >pass in a value for a clientID
> >
> >read that client id and prefill form data from my database
> >
> >that about does it. I have all of the textboxes working perfectly, but
> I do
> >not even know which members belong ot the dropdownlist. Also, if anyone
> >knows a good place to get a list of which members belong to which
control,
> >that would be great. Thank you for the help.
> >
> >Jeremy Samkowiak
> >
> >
>
Jeremy Samkowiak at 2007-11-11 23:30:22 >
# 6 Re: Help asp:DropDownList
Not very sure your question, just some guess:
1) In my example, all rows in OrderID field will be listed in the DropdownList
and the first item (Item 0 ) will be displayed as default. If you would like
to display an item other than the first orderID, do this:

OrderList.SelectedIndex = 2; // third item will be displayed

2) To check which item is selected by the user, I used a button control and
get the selected item in the OnClickEvent. You could also set AutoPost="true"
so that you can catch selected item whenerer a selected iten is changed.
See the dropdownlist example in my web site.

Mingyong Yang, MCP
--------
http://www.foxinternet.net/web2/myang

"Jeremy Samkowiak" <j@samko.com> wrote:
>Thank you again, but after i bind the information, how do i tell it which
>one i would like selected. so when it is binding the correct one form the
>database is automatically shown? Any help will be greatly appreciated.
>
>Thanks,
>Jeremy
>
>"Mingyong Yang" <myang@foxinternet.net> wrote in message
>news:39a55510$1@news.dev-archive.com...
>>
>> The following is an completed example to bind a dropdownlist to a ds
>directly:
>>
>> <%@ Import Namespace="System.Data" %>
>> <%@ Import Namespace="System.Data.SQL" %>
>>
>> <html>
>>
>> <script language="C#" runat="server" >
>> void Page_Load(Object Source, EventArgs E)
>> {
>> if(IsPostBack==false)
>> {
>> string sConn = "server=(local);Database=Northwind;User
>ID=Sa;Password=;";
>> string sSQL = "Select OrderID, CustomerID FROM Orders";
>> SQLConnection cn = new SQLConnection(sConn);
>> SQLDataSetCommand cmd= new SQLDataSetCommand(sSQL,cn);
>> DataSet ds = new DataSet();
>> cmd.FillDataSet(ds,"Orders");
>>
>> OrderList.DataSource = ds.Tables[0].DefaultView;
>> OrderList.DataBind();
>>
>> lblMessage.Text = "";
>> }
>> }
>>
>> void btnOS_click(Object Sender, EventArgs E)
>> {
>> lblMessage.Text = "Order ID: " + OrderList.SelectedItem.Text;
>> }
>>
>> </script>
>>
>> <body>
>> <Form runat="server">
>>
>> <h2>DropDownList Data Binding Example</h2>
>> <p />
>> <asp:DropDownList id="OrderList" runat="server" DataValueField="OrderID"
>> />
>>
>> <asp:Button id="btnOS" onclick="btnOS_click" Text="Select"
>runat="server"
>> /><br>
>> <asp:Label id="lblMessage" runat="server" />
>>
>> </form>
>> </body>
>> </html>
>>
>>
>>
>> "Jeremy Samkowiak" <j@samko.com> wrote:
>> >Could anyone give me an example of how to bind a drop down list to a
data
>> >store? Here is what i am trying to do.
>> >
>> >pass in a value for a clientID
>> >
>> >read that client id and prefill form data from my database
>> >
>> >that about does it. I have all of the textboxes working perfectly, but
>> I do
>> >not even know which members belong ot the dropdownlist. Also, if anyone
>> >knows a good place to get a list of which members belong to which
>control,
>> >that would be great. Thank you for the help.
>> >
>> >Jeremy Samkowiak
>> >
>> >
>>
>
>
Mingyong Yang at 2007-11-11 23:31:27 >
# 7 Re: Help asp:DropDownList
That answered my question in divine fashion, thank you so much.

Jeremy

"Mingyong Yang" <myang@foxinternet.net> wrote in message
news:39a6091e$1@news.dev-archive.com...
>
> Not very sure your question, just some guess:
> 1) In my example, all rows in OrderID field will be listed in the
DropdownList
> and the first item (Item 0 ) will be displayed as default. If you would
like
> to display an item other than the first orderID, do this:
>
> OrderList.SelectedIndex = 2; // third item will be displayed
>
> 2) To check which item is selected by the user, I used a button control
and
> get the selected item in the OnClickEvent. You could also set
AutoPost="true"
> so that you can catch selected item whenerer a selected iten is changed.
> See the dropdownlist example in my web site.
>
> Mingyong Yang, MCP
> --------
> http://www.foxinternet.net/web2/myang
>
> "Jeremy Samkowiak" <j@samko.com> wrote:
> >Thank you again, but after i bind the information, how do i tell it which
> >one i would like selected. so when it is binding the correct one form
the
> >database is automatically shown? Any help will be greatly appreciated.
> >
> >Thanks,
> >Jeremy
> >
> >"Mingyong Yang" <myang@foxinternet.net> wrote in message
> >news:39a55510$1@news.dev-archive.com...
> >>
> >> The following is an completed example to bind a dropdownlist to a ds
> >directly:
> >>
> >> <%@ Import Namespace="System.Data" %>
> >> <%@ Import Namespace="System.Data.SQL" %>
> >>
> >> <html>
> >>
> >> <script language="C#" runat="server" >
> >> void Page_Load(Object Source, EventArgs E)
> >> {
> >> if(IsPostBack==false)
> >> {
> >> string sConn = "server=(local);Database=Northwind;User
> >ID=Sa;Password=;";
> >> string sSQL = "Select OrderID, CustomerID FROM Orders";
> >> SQLConnection cn = new SQLConnection(sConn);
> >> SQLDataSetCommand cmd= new SQLDataSetCommand(sSQL,cn);
> >> DataSet ds = new DataSet();
> >> cmd.FillDataSet(ds,"Orders");
> >>
> >> OrderList.DataSource = ds.Tables[0].DefaultView;
> >> OrderList.DataBind();
> >>
> >> lblMessage.Text = "";
> >> }
> >> }
> >>
> >> void btnOS_click(Object Sender, EventArgs E)
> >> {
> >> lblMessage.Text = "Order ID: " + OrderList.SelectedItem.Text;
> >> }
> >>
> >> </script>
> >>
> >> <body>
> >> <Form runat="server">
> >>
> >> <h2>DropDownList Data Binding Example</h2>
> >> <p />
> >> <asp:DropDownList id="OrderList" runat="server"
DataValueField="OrderID"
> >> />
> >>
> >> <asp:Button id="btnOS" onclick="btnOS_click" Text="Select"
> >runat="server"
> >> /><br>
> >> <asp:Label id="lblMessage" runat="server" />
> >>
> >> </form>
> >> </body>
> >> </html>
> >>
> >>
> >>
> >> "Jeremy Samkowiak" <j@samko.com> wrote:
> >> >Could anyone give me an example of how to bind a drop down list to a
> data
> >> >store? Here is what i am trying to do.
> >> >
> >> >pass in a value for a clientID
> >> >
> >> >read that client id and prefill form data from my database
> >> >
> >> >that about does it. I have all of the textboxes working perfectly,
but
> >> I do
> >> >not even know which members belong ot the dropdownlist. Also, if
anyone
> >> >knows a good place to get a list of which members belong to which
> >control,
> >> >that would be great. Thank you for the help.
> >> >
> >> >Jeremy Samkowiak
> >> >
> >> >
> >>
> >
> >
>
Jeremy Samkowiak at 2007-11-11 23:32:27 >