![]() |
|
|
|||||||
![]() |
ASP Net - dropdownlist default selected |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
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. <asp <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> </asp 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==?= |
|
|
|
|
#2 |
|
Posts: n/a
|
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 <asp <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> </asp 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. > > > <asp > <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> > </asp > > 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] |
|
|
|
#3 |
|
Posts: n/a
|
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. > > > <asp > <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> > </asp > > 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 |
|
|
|
#4 |
|
Junior Member
Join Date: Feb 2009
Posts: 1
|
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. |
|
|
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |