October 17th, 2007How to Set the Default Value of a DropDownList in an ASP.NET Page
(Using Membership ObjectDataSource From MSDN Article)
I’m doing a project where I have lots of DropDownList’s on a page and want a simple way to set the initial values based on some known value. The DropDownLists are retrieved using ObjectDataSource but I don’t want the first value displayed. I wrestled some with how to do this using page_load or page_prerender but didn’t come up with good solutions. I did finally
decide that programming the databound event of the dropdownlist was probably the best way to go.
The solution is pasted below. The MembershipUtilities.MembershipODS c# code can be downloaded from MSDN or on my web site at the following location.
http://peterkellner.net/2006/01/09/microsoft-aspnet-20-memberrole-management-with-iis/.
<%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <script runat=”server”> protected void DropDownListUser_DataBound(object sender, EventArgs e) { DropDownListUser.SelectedIndex = DropDownListUser.Items.IndexOf (DropDownListUser.Items.FindByValue(Context.User.Identity.Name)); } </script> <html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”> <title>DropDownList Initialize</title> </head> <body> <form id=”form1″ runat=”server”> <div> <asp:DropDownList ID=”DropDownListUser” runat=”server” DataSourceID=”ObjectDataSourceUser” DataTextField=”UserName” DataValueField=”UserName” OnDataBound=”DropDownListUser_DataBound”> </asp:DropDownList> <asp:ObjectDataSource ID=”ObjectDataSourceUser” runat=”server” SelectMethod=”GetMembers” TypeName=”MembershipUtilities.MembershipUserODS”> <SelectParameters> <asp:Parameter Name=”sortData” Type=”String” /> </SelectParameters> </asp:ObjectDataSource> </div> </form> </body> </html>









December 2nd, 2007 at 8:58 am
Why you don’t use just
DropDownListUser.SelectedValue = Context.User.Identity.Name;
instead of
DropDownListUser.SelectedIndex =
DropDownListUser.Items.IndexOf(
DropDownListUser.Items.FindByValue(Context.User.Identity.Name));
?
January 2nd, 2008 at 12:56 am
I dont agree with george, since it replaces the selectedvalue.
But Peter
why cant we use
DropDownListUser.Items.FindByValue(Context.User.Identity.Name).Selected = true;
is it not more simpler?
April 3rd, 2008 at 11:45 am
I agree with NaveenJ. His approach is just want i’m using :p
July 2nd, 2008 at 8:23 am
I use Naveen’s approach too but at run time when databind occurs, if the SelectedIndex is not cleared to -1 first then you can get an http error that you cannot have more than one item selected. It goes something like
DropDownList.SelectIndex = -1;
DropDownListUser.Items.FindByValue(Context.User.Identity.Name).Selected = true;
December 3rd, 2008 at 1:29 pm
Be careful.
DropDownListUser.Items.FindByValue(Context.User.Identity.Name) may return null. When it does, you may get “object is not an instance” exception.
May 20th, 2009 at 1:21 am
I see no reason why you need to do it in server side. is there anyway to do it in the client side?
May 20th, 2009 at 4:37 am
I’m not sure how, but I’m sure you could do it client side. -Peter
June 2nd, 2009 at 5:03 pm
in the Page_Load method in the code behind for my apsx page I added this to set the initial value of a drop down list:
protected void Page_Load(object sender, EventArgs e)
{
v_state.SelectedValue = “AK”;
}
June 25th, 2009 at 3:07 am
I recommend a mixture of the abpve methods. This way you get the default value and protect against exceptions
protected void Page_Load(object sender, EventArgs e)
{
if (DropDownListOrg.Items.FindByValue(“-1″) != null)
DropDownListOrg.SelectedValue = “-1″;
}
June 25th, 2009 at 4:01 am
Correction – The above worked for FF but not for IE (I’ve no idea why since its server side code) Anyway the below worked for both
protected void DropDownListOrg_DataBound(object sender, EventArgs e)
{
if (DropDownListOrg.Items.FindByValue(“-1″) != null)
DropDownListOrg.SelectedValue = “-1″;
}
October 1st, 2009 at 2:13 am
How can assign a item to dropdownlist in runtime..
that dropdownlist already loaded with somevalue like jan,feb,mar,apr
i want to assgin a value feb at runtime…
how can i assogn it…
June 9th, 2010 at 3:28 pm
Hi, although its a bit old now thanks for the above discussion as it lead me towards my solution. The problem is I wanted a dropdownlist to default to a specific value not the first in the list (from my database) and using:-
protected void Page_Load(object sender, EventArgs e)
{
maxCostDDList.SelectedValue = “1500″;
}
just set the dropdown list to 1500 every time the page was submitted, not good. But thanks to suggestions on here this worked for me:-
protected void maxCostDDList_DataBound(object sender, EventArgs e)
{
if (maxCostDDList.Items.FindByValue(“1500″) != null)
{
maxCostDDList.SelectedValue = “1500″;
}
}
did try “-1″ but that didn’t work for me.
Cheers to all.