|
 |
aspx thread: Bind DropDownList to ad-hoc list
Message #1 by "Ric Bernat" <ricb@h...> on Thu, 27 Sep 2001 17:03:48
|
|
I need to display a short dropdownlist of choices to my users.
Specifically, I need to allow them to choose one of the coming four weeks.
For example:
- Sep. 16 to Sep. 22
- Sep. 23 to Sep. 29
- Sep. 30 to Oct. 06
- Oct. 07 to Oct. 13
Of course, I can easily build up the list in a string using a <select> tag
and plain HTML.
However, it seems to make more sense to use an asp:dropdownlist control
and bind it to a memory-based list. I'm just not sure whether a DataSet
would be overkill for this, and whether an ArrayList or some simpler
collection would make sense.
I will want to set both MyDDL.DataValueField and MyDDL.DataTextField.
DataValueField will contain a single data (first day of the week), while
DataTextField will contain strings like the four listed above.
To use a DataSet, I suppose I would either manually build up the schema in
advance (one table with two columns), and then add my four rows. Or I
could build up the schema and data in XML and then populate the DataSet
from the the XML string.
Can anyone provide suggestions and/or sample code on this?
Thanks,
Ric
Message #2 by "Pete Ehli" <peteehli@a...> on Thu, 27 Sep 2001 14:26:20 -0700
|
|
I would say if your list is going to change then bind the list to data
source and use a datareader. Remember that a dataset is a disconnected
structure that makes it excellent to pass around to different objects that
need it say for instance in your DAL - so for only four values it is
definitely overkill. Here some simple code that should help. If you are
going to use this control on a number of pages you can build your own
control via a .ascx page and include the connection string below in the
codebehind.
- Pete Ehli -
if(!Page.IsPostBack)
{
string connString = this.connectionString;
if(connString == null)
{
connString = "server=localhost;uid=sa;pwd=;database=Northwind";
}
string sqlSelectStatement = this.sqlSelect;
if(sqlSelectStatement == null)
{
sqlSelectStatement = "SELECT * FROM Shippers";
}
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sqlSelectStatement,conn);
SqlDataReader reader;
try
{
conn.Open();
reader = cmd.ExecuteReader();
ddlShipMethod.DataTextField = "CompanyName";
ddlShipMethod.DataSource = reader;
ddlShipMethod.DataBind();
}
catch(Exception ex)
{
ex.ToString();
}
finally
{
if(conn.State == ConnectionState.Open)
{
conn.Close();
}
}
}
-----Original Message-----
From: Ric Bernat [mailto:ricb@h...]
Sent: Thursday, September 27, 2001 5:04 PM
To: ASP+
Subject: [aspx] Bind DropDownList to ad-hoc list
I need to display a short dropdownlist of choices to my users.
Specifically, I need to allow them to choose one of the coming four weeks.
For example:
- Sep. 16 to Sep. 22
- Sep. 23 to Sep. 29
- Sep. 30 to Oct. 06
- Oct. 07 to Oct. 13
Of course, I can easily build up the list in a string using a <select> tag
and plain HTML.
However, it seems to make more sense to use an asp:dropdownlist control
and bind it to a memory-based list. I'm just not sure whether a DataSet
would be overkill for this, and whether an ArrayList or some simpler
collection would make sense.
I will want to set both MyDDL.DataValueField and MyDDL.DataTextField.
DataValueField will contain a single data (first day of the week), while
DataTextField will contain strings like the four listed above.
To use a DataSet, I suppose I would either manually build up the schema in
advance (one table with two columns), and then add my four rows. Or I
could build up the schema and data in XML and then populate the DataSet
from the the XML string.
Can anyone provide suggestions and/or sample code on this?
Thanks,
Ric
Message #3 by "Ric Bernat" <ricb@h...> on Fri, 28 Sep 2001 02:30:58
|
|
Thanks for the input, Pete. The thing is, the data I'm working with isn't
in a database; it's just calculated based on the current date. I've seen
lots of examples of populating a DataSet from a database; I was hoping to
get some code samples of creating the schema for a DataSet manually and
then populating it.
In any case, you bring up another point which I'm wondering about. Can I
pass a DataSet from one object to another, as if it were a simple
variable? Would I just return it as the return value of a function? Would
it be better to instantiate the DataSet in the calling object first, then
pass it in to the called object by reference?
Message #4 by "Pete Ehli" <peteehli@a...> on Fri, 28 Sep 2001 16:26:16 -0700
|
|
Ric I would Just create a class that calculates the needed dates then
populates an ArrayList and makes the ArrayList available to whatever client
object needs it. This I think would be a lot easier, and makes much more
sense than using a DataSet.
- Pete Ehli -
-----Original Message-----
From: Ric Bernat [mailto:ricb@h...]
Sent: Friday, September 28, 2001 2:31 AM
To: ASP+
Subject: [aspx] RE: Bind DropDownList to ad-hoc list
Thanks for the input, Pete. The thing is, the data I'm working with isn't
in a database; it's just calculated based on the current date. I've seen
lots of examples of populating a DataSet from a database; I was hoping to
get some code samples of creating the schema for a DataSet manually and
then populating it.
In any case, you bring up another point which I'm wondering about. Can I
pass a DataSet from one object to another, as if it were a simple
variable? Would I just return it as the return value of a function? Would
it be better to instantiate the DataSet in the calling object first, then
pass it in to the called object by reference?
|
|
 |