aspx thread: Setting Values of Drop Down Lists
Hello Everyone and thanks for your help in advance. I am developing an
application that allows users to add categories to a database. The
database then populates a dropdownlist in several webforms. However, I
need to be able to set the selected item of this drop down within the
form based on the value stored in another table. For example:
<asp:DropDownList id="Title" runat="server">
<asp:ListItem Value="1">President</asp:ListItem>
<asp:ListItem Value="2">Vice-President</asp:ListItem>
<asp:ListItem Value="3">Secretary</asp:ListItem>
</asp:DropDownList>
MyReader("Title") may return "Vice-President". I need the selected item
to be "Vice-President".
Each of these categories can be added, edited, or deleted within a
separate function. The list box is populated from another table of
contacts. The appropriate item should be selected based on the value in
the table. My problem is that since the list can be dynamically changed,
I do not know how to set the selected item property. Item index wont
work since the number of items may change. I am not sure where to go
with this. Any help would be greatly appreciated. Thanks.
Have you used IsPostBack method before you add data to dropdownlist ?
try this :
(using C#)
if(!IsPostBack)
{
//Bind your dropdownlist...
}
hope help..
regards,
-ali-
At 04:19 AM 8/13/2002 +0000, you wrote:
>Hello Everyone and thanks for your help in advance. I am developing an
>application that allows users to add categories to a database. The
>database then populates a dropdownlist in several webforms. However, I
>need to be able to set the selected item of this drop down within the
>form based on the value stored in another table. For example:
>
><asp:DropDownList id="Title" runat="server">
> <asp:ListItem Value="1">President</asp:ListItem>
> <asp:ListItem Value="2">Vice-President</asp:ListItem>
> <asp:ListItem Value="3">Secretary</asp:ListItem>
></asp:DropDownList>
>
>MyReader("Title") may return "Vice-President". I need the selected item
>to be "Vice-President".
>
>Each of these categories can be added, edited, or deleted within a
>separate function. The list box is populated from another table of
>contacts. The appropriate item should be selected based on the value in
>the table. My problem is that since the list can be dynamically changed,
>I do not know how to set the selected item property. Item index wont
>work since the number of items may change. I am not sure where to go
>with this. Any help would be greatly appreciated. Thanks.
Here is one way that works:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
SqlDataAdapter1.Fill(DataSet11)
DropDownList1.DataBind()
Dim item As ListItem
For Each item In DropDownList1.Items
If item.Text = "Vice President, Sales" Then
item.Selected = True
Exit For
End If
Next
End Sub
Depending on your project scenario, your DropDownList maybe/maynotbe
databound.
You may also want to enclose the above code in a 'If Not Page.IsPostBack
Then' Block.
Previous message:
> Hello Everyone and thanks for your help in advance. I am developing an
a> pplication that allows users to add categories to a database. The
d> atabase then populates a dropdownlist in several webforms. However, I
n> eed to be able to set the selected item of this drop down within the
f> orm based on the value stored in another table. For example:
> <asp:DropDownList id="Title" runat="server">
> <asp:ListItem Value="1">President</asp:ListItem>
> <asp:ListItem Value="2">Vice-President</asp:ListItem>
> <asp:ListItem Value="3">Secretary</asp:ListItem>
<> /asp:DropDownList>
> MyReader("Title") may return "Vice-President". I need the selected item
t> o be "Vice-President".
> Each of these categories can be added, edited, or deleted within a
s> eparate function. The list box is populated from another table of
c> ontacts. The appropriate item should be selected based on the value in
t> he table. My problem is that since the list can be dynamically
changed,
I> do not know how to set the selected item property. Item index wont
w> ork since the number of items may change. I am not sure where to go
w> ith this. Any help would be greatly appreciated. Thanks.
|





