Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - dropdownlist default selected

 
Thread Tools Search this Thread
Old 11-01-2004, 04:19 PM   #1
Default dropdownlist default selected


How do i make a dropdownlist selected value based on the value i retrive from
the database.

Basically i have an edit page and like to display the default value in a
dropdown list from the database.

for example: if the ddl_value is 2 from the database i want the second list
item to be selected by default.


<aspropDownList id="ddl" runat="server">
<asp:ListItem Value="0">value 1</asp:ListItem>
<asp:ListItem Value="1">value 2</asp:ListItem> // make this dynamically
selected
<asp:ListItem Value="0">value 3</asp:ListItem>
<asp:ListItem Value="1">value 4</asp:ListItem>
</aspropDownList>

I am using the dropdownlist inside a repeater.. and only retriving single
record at a time based on a querystring... and using a datareader to fill the
form.

Many thanks in advance..




=?Utf-8?B?aHV6eg==?=
  Reply With Quote
Old 11-01-2004, 05:18 PM   #2
Ken Cox [Microsoft MVP]
 
Posts: n/a
Default Re: dropdownlist default selected
Hi,

You need to find the item that has the text value "value 2" and then get its
index value. Once you have the index value, you can set the SelectedIndex to
that value.

Here's a quick way to accomplish all three:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
ddl.SelectedIndex = ddl.Items.IndexOf _
(ddl.Items.FindByText("value 2"))
End If
End Sub

<aspropDownList id="ddl" runat="server">
<asp:ListItem Value="0">value 1</asp:ListItem>
<asp:ListItem Value="1">value 2</asp:ListItem>
<asp:ListItem Value="0">value 3</asp:ListItem>
<asp:ListItem Value="1">value 4</asp:ListItem>
</aspropDownList>

Does this help?

Ken
Microsoft MVP [ASP.NET]



"huzz" <> wrote in message
news:C6490473-714C-44D1-855B-...
> How do i make a dropdownlist selected value based on the value i retrive
> from
> the database.
>
> Basically i have an edit page and like to display the default value in a
> dropdown list from the database.
>
> for example: if the ddl_value is 2 from the database i want the second
> list
> item to be selected by default.
>
>
> <aspropDownList id="ddl" runat="server">
> <asp:ListItem Value="0">value 1</asp:ListItem>
> <asp:ListItem Value="1">value 2</asp:ListItem> // make this dynamically
> selected
> <asp:ListItem Value="0">value 3</asp:ListItem>
> <asp:ListItem Value="1">value 4</asp:ListItem>
> </aspropDownList>
>
> I am using the dropdownlist inside a repeater.. and only retriving single
> record at a time based on a querystring... and using a datareader to fill
> the
> form.
>
> Many thanks in advance..
>
>




Ken Cox [Microsoft MVP]
  Reply With Quote
Old 11-01-2004, 05:25 PM   #3
Chris Austin
 
Posts: n/a
Default Re: dropdownlist default selected
You have to use either FindByValue() or FindByText() method of the items
collection. If found, it returns the ListItem otherwise, the method returns
null.

ListItem item = MyDropDown.items.FindByValue("TX");
if(item != null)
{
MyDropDown.SelectedItem.Selected = false;
item.Selected = true;
}

Here is the reference link to the FindByValue and FindByText methods
http://msdn.microsoft.com/library/de...valuetopic.asp

HTH
-Chris
~
http://weblogs.austinspad.com/caustin

"huzz" <> wrote in message
news:C6490473-714C-44D1-855B-...
> How do i make a dropdownlist selected value based on the value i retrive

from
> the database.
>
> Basically i have an edit page and like to display the default value in a
> dropdown list from the database.
>
> for example: if the ddl_value is 2 from the database i want the second

list
> item to be selected by default.
>
>
> <aspropDownList id="ddl" runat="server">
> <asp:ListItem Value="0">value 1</asp:ListItem>
> <asp:ListItem Value="1">value 2</asp:ListItem> // make this dynamically
> selected
> <asp:ListItem Value="0">value 3</asp:ListItem>
> <asp:ListItem Value="1">value 4</asp:ListItem>
> </aspropDownList>
>
> I am using the dropdownlist inside a repeater.. and only retriving single
> record at a time based on a querystring... and using a datareader to fill

the
> form.
>
> Many thanks in advance..
>
>





Chris Austin
  Reply With Quote
Old 02-18-2009, 01:00 AM   #4
ramzihawa
Junior Member
 
Join Date: Feb 2009
Posts: 1
Smile solution to this issue
hello all,
The solution is to add OnDataBound="SomeFunction" in your aspx (or ascx) file.

a sample dropdownlist would be

<asp: DropDownList ID="ddlPages" runat="server" DataSourceID="SqlDsListPagesOfThisModule"
DataTextField="ModulePageTitle" DataValueField="ModulePageId"
AutoPostBack="True" OnDataBound="RamOne" OnSelectedIndexChanged="ddlPages_SelectedIndexChan ged" >
</asp: DropDownList>
//note that asp :dropdownlist DOES NOT contain spaces but the post replaces it with a smiley
then in the code behind

protected void RamOne(System.Object sender, System.EventArgs e)
{

if (!Page.IsPostBack)
{

if (ddlPages.Items.Count > 0)
{
ddlPages.SelectedIndex = 0;//the default value is -1 for nothing selected
//selectedindexchanged will NOT be triggered automatically
//so we trigger it (which is exactly what we need !!!!)
ddlPages_selectedindexchanged(sender,e);


}

}


}


protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
{
//force showing the list of dbinteractions on first load (!postback, before page load because this is called from ondatabaound)
if (!Page.IsPostBack)
{
something.databind();//for example Gridview1 or dropdownlist2 etc
}
else
{
//put here what you would normally put, for example something.databind()
//and for example a call to a function that logs preferences in a db for this user ("update memberprefs set dropdownvalue= "+ddlPages.selectedvalue+"...where userid=...");
}
}


good luck coding!!!
Eng. Ramzi Hawa
Database SqL 2005 expert, Dnn expert...
email ramzi at charlottecoders.com
website rolamzi.com


ramzihawa

Last edited by ramzihawa : 02-18-2009 at 02:12 PM.
ramzihawa is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic validations for checkboxlist and dropdownlist mrugesh_dulera Software 0 06-26-2007 01:56 AM
DropdownList in gridview visj4u Software 0 04-27-2007 01:11 PM
Set default path of <input type = file> vj_india Software 1 09-15-2006 06:17 PM
Computer with default language in Chinese AG A+ Certification 3 04-04-2005 12:40 AM
Re: Default media player natural_4u A+ Certification 1 02-16-2004 09:35 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46