|
 |
aspx thread: Dropdown lists
Message #1 by "Julian Roberts" <jules@c...> on Sun, 26 Nov 2000 20:53:43 -0000
|
|
I've been adapting code from this page
http://www.aspng.com/quickstart/util/srcview.aspx?path=/quickstart/aspplus/s
amples/webforms/ctrlref/webctrl/DropDownList/DropDownList2.src
to populate a dropdown with rows from a DataSet:
If Not IsPostBack Then
Dim Row as DataRow
Dim values as ArrayList= new ArrayList()
for each Row in oDS.Tables("MyTable").Rows
values.Add (Row(1))
next
DropDown1.DataSource = values
DropDown1.DataBind
End If
This works quite well but what I'd like to have is that each option has a
value that comes from a different field in the database. eg:
<option value="1">Oranges</option>
I just can't find a way to tell the server control to display a different
option value to that of it's label. ie, My code above will always generate:
<option value="Oranges">Oranges</option>
Any pointers would be appreciated.
Jules
http://charon.co.uk
Ultradev beerfest - 3rd Dec
http://www.beertobelly.co.uk
Message #2 by =?iso-8859-1?Q?Fredrik_Norm=E9n?= <fredrik.normen@e...> on Mon, 27 Nov 2000 10:10:43 +0100
|
|
hi,
you can use this instead of an arraylist.
If Not IsPostBack Then
Dim Row as DataRow
for each Row in oDS.Tables("MyTable").Rows
'This will add an Item to you dropdown list.
DropDown1.Items.Add(New ListItem(Row(0),Row(1))
next
If you use this you dont need to use DataBind or DataSource
/Fredrik Normén
-----Original Message-----
From: Julian Roberts [mailto:jules@c...]
Sent: den 26 november 2000 21:54
To: ASP+
Subject: [aspx] Dropdown lists
I've been adapting code from this page
http://www.aspng.com/quickstart/util/srcview.aspx?path=/quickstart/aspplus/s
amples/webforms/ctrlref/webctrl/DropDownList/DropDownList2.src
to populate a dropdown with rows from a DataSet:
If Not IsPostBack Then
Dim Row as DataRow
Dim values as ArrayList= new ArrayList()
for each Row in oDS.Tables("MyTable").Rows
values.Add (Row(1))
next
DropDown1.DataSource = values
DropDown1.DataBind
End If
This works quite well but what I'd like to have is that each option has a
value that comes from a different field in the database. eg:
<option value="1">Oranges</option>
I just can't find a way to tell the server control to display a different
option value to that of it's label. ie, My code above will always generate:
<option value="Oranges">Oranges</option>
Any pointers would be appreciated.
Jules
http://charon.co.uk
Ultradev beerfest - 3rd Dec
http://www.beertobelly.co.uk
Message #3 by Nilesh_Dhoot <Nilesh_Dhoot@s...> on Mon, 27 Nov 2000 15:24:04 +0530
|
|
just try this...The DataValueField(Value) Property of the DataList will help
you...
<asp:DropDownList id="DropDownList1" runat="server"
DataSource="<% databindingexpression %>"
DataTextField="DataSourceField"
DataValueField="DataSourceField"
AutoPostBack="True|False"
OnSelectedIndexChanged="OnSelectedIndexChangedMethod"
/>
<asp:Listitem value="value" selected="True|False">
Text
</asp:Listitem>
</asp:DropDownList>
> ----------
> From: Julian Roberts[SMTP:jules@c...]
> Reply To: ASP+
> Sent: Monday, November 27, 2000 2:23 AM
> To: ASP+
> Subject: [aspx] Dropdown lists
>
> I've been adapting code from this page
> http://www.aspng.com/quickstart/util/srcview.aspx?path=/quickstart/aspplus
> /samples/webforms/ctrlref/webctrl/DropDownList/DropDownList2.src
>
>
> to populate a dropdown with rows from a DataSet:
>
>
> If Not IsPostBack Then
> Dim Row as DataRow
> Dim values as ArrayList= new ArrayList()
> for each Row in oDS.Tables("MyTable").Rows
> values.Add (Row(1))
> next
> DropDown1.DataSource = values
> DropDown1.DataBind
> End If
>
> This works quite well but what I'd like to have is that each option has a
> value that comes from a different field in the database. eg:
>
> <option value="1">Oranges</option>
>
> I just can't find a way to tell the server control to display a different
> option value to that of it's label. ie, My code above will always
> generate:
>
> <option value="Oranges">Oranges</option>
>
> Any pointers would be appreciated.
>
> Jules
> http://charon.co.uk
> Ultradev beerfest - 3rd Dec
> http://www.beertobelly.co.uk
>
>
Message #4 by "Julian Roberts" <jules@c...> on Mon, 27 Nov 2000 21:38:34 -0000
|
|
Thanks for replying. Your suggestion worked perfectly. I couldn't find any
reference to this method in the documentation. I guess I'll be trawling
through the resource sites looking for more advanced examples. All part of
the fun of developing :)
Jules
http://charon.co.uk
Ultradev beerfest - 3rd Dec
http://www.beertobelly.co.uk
----- Original Message -----
From: "Fredrik Normén" <fredrik.normen@e...>
To: "ASP+" <aspx@p...>
Sent: Monday, November 27, 2000 9:10 AM
Subject: [aspx] RE: Dropdown lists
> hi,
>
> you can use this instead of an arraylist.
>
> If Not IsPostBack Then
> Dim Row as DataRow
>
> for each Row in oDS.Tables("MyTable").Rows
> 'This will add an Item to you dropdown list.
> DropDown1.Items.Add(New ListItem(Row(0),Row(1))
> next
>
> If you use this you dont need to use DataBind or DataSource
>
> /Fredrik Normén
>
>
>
> -----Original Message-----
> From: Julian Roberts [mailto:jules@c...]
> Sent: den 26 november 2000 21:54
> To: ASP+
> Subject: [aspx] Dropdown lists
>
>
> I've been adapting code from this page
>
http://www.aspng.com/quickstart/util/srcview.aspx?path=/quickstart/aspplus/s
> amples/webforms/ctrlref/webctrl/DropDownList/DropDownList2.src
>
> to populate a dropdown with rows from a DataSet:
>
>
> If Not IsPostBack Then
> Dim Row as DataRow
> Dim values as ArrayList= new ArrayList()
> for each Row in oDS.Tables("MyTable").Rows
> values.Add (Row(1))
> next
> DropDown1.DataSource = values
> DropDown1.DataBind
> End If
>
> This works quite well but what I'd like to have is that each option has a
> value that comes from a different field in the database. eg:
>
> <option value="1">Oranges</option>
>
> I just can't find a way to tell the server control to display a different
> option value to that of it's label. ie, My code above will always
generate:
>
> <option value="Oranges">Oranges</option>
>
> Any pointers would be appreciated.
>
> Jules
> http://charon.co.uk
> Ultradev beerfest - 3rd Dec
> http://www.beertobelly.co.uk
>
>
|
|
 |