Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Setting Values of Drop Down Lists


Message #1 by "Hugh McLaughlin" <hugh@k...> on Tue, 13 Aug 2002 04:19:51
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.
Message #2 by Ali Ahmad H <aheryana@b...> on Tue, 13 Aug 2002 11:35:51 +0700
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.


Message #3 by "Mingkun Goh" <mangokun@h...> on Wed, 14 Aug 2002 09:10:33
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.

  Return to Index