Default value for Dropdownlist that get's it's value from Database.


I am using an Object Data Souce to retrieve the value of a Dropdownlist from a table on MS SQL server 2005. I want to make a default value appear on the Dropdownlist intially, should i enter the default value into the database table or is that a better way of doing it?

Any suggestions will be very much appreciated.


5/25/2008 10:30:24 AM 91979 articles. 4 followers. Follow

9 Replies

Similar Articles

[PageSpeed] 37

 No need for database just set sekected value after databingind

 protected void Page_Load(object sender, EventArgs e)
ListItem defaultItem = new ListItem("Select a Person", "-1");
this.PersonDL.Items.Insert(0, defaultItem);
this.PersonDL.SelectedIndex = 0;


Refer to this thread too Setting DropDownList Default Value


Hosam Kamel

Remember to click on Mark as answer on the post that helped you
5/25/2008 12:31:56 PM

Better way:

<asp:DropDownList runat="server" ID='ddl1' AppendDataBoundItems='true' DatasourceID=...>

<asp:ListItem Text='default' Value='default' Selected="True"></asp:ListItem>


5/25/2008 12:57:26 PM

Hi Heartattack,

i wrote code similar to yours, but i now get this error message:

Incorrect syntax near the keyword 'Default'.

This is my version of the code:

<asp:DropDownList ID="AgeDropDownList" runat="server" DataSourceID="AgeObjectDataSource"

DataTextField="Age" DataValueField="AgeID" AppendDataBoundItems='true'>

<asp:ListItem Text='Default' Value='Default' Selected="True"></asp:ListItem>


5/25/2008 5:30:10 PM

i think the error message suggests default is a reserved word.  try to change default to some other word.


Please mark replies that have helped you as answers.
5/25/2008 5:33:37 PM

Code's working for me. Note, however, that on insert or edit or databinding, you must make sure that the selected value of the drop down is not the default OR the default is an acceptable value in the database column.

5/25/2008 5:40:44 PM

HeartAttack : you should use a Double Quotation around the Attribute values !

<asp:DropDownList runat="server" ID="ddl1" AppendDataBoundItems="true" DatasourceID=...>

<asp:ListItem Text=" Please Select  Item "  Value="" Selected="True"></asp:ListItem>



See this Thread : 


Anas Ghanem.

Note:Please Don't hesitate to click "Report Abuse" link if you noticed something wrong on the forums (like duplicate ,Off-topic,offensive,or any post that violates the website "TERMS OF USE"). -- Thanks!

5/25/2008 5:49:46 PM

Actually, it can be single or double quotes in aspx markup :)

5/25/2008 5:57:52 PM

You are correct  , Thanks for the note ,

yes HearAttack code works well ,

    <asp:DropDownList runat="server" ID='DropDownList1' AppendDataBoundItems='true' >
        <asp:ListItem Text='default' Value='default' Selected="True"></asp:ListItem>

Anas Ghanem.

Note:Please Don't hesitate to click "Report Abuse" link if you noticed something wrong on the forums (like duplicate ,Off-topic,offensive,or any post that violates the website "TERMS OF USE"). -- Thanks!

5/25/2008 6:05:45 PM

Thanks, my value was not accepted on database column that's why.

5/25/2008 7:42:17 PM

Similar Artilces:

Getting Repeater's EVAL value from within DropDownList's SelectedIndexChanged sub
Hello, I placed the dropdownlist directly into Repeater's ItemTemplate like this: <asp:Repeater ID="rptOrderHistory" runat="server" DataSourceID="sqlDS_OrderHistoryList"> <ItemTemplate> <tr> <td><img src="../images/Icon_Gentleman.jpg" /><br /><%#Eval("UserID").ToString.PadLeft(5, "0"c)%></td> <td><img src="../images/Icon_Parcel.jpg" /><br /><%#Eval("OrderID").ToString....

How can I get out of my Mozilla's browser the Winamp browser and get back the Google's or Mozilla's browser.
Please I have got to my browser ( I don't know how.) the Winamp browser he behave like virus stick in.But he doesn't has uninstalation section so I can't do anything to get him away from Mozilla's file. I'm not happy of Winamp's behave its like advertisement against a customer's will. Please is there any way to get him away from Mozilla?Please send me exact direction I have tried many ways but he is deeply inside the systeme so when I install new Mozilla he is always here ready to pester me. Thank you for an answer. Richard ...

Help! I cannot access the DataGrid's ItemTemplate's LinkButton's Text Atrribute 's Value!!!!!!!!!!!
I write a datagrid's ItemDataBound method as follows:   private void dgTable_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)  {   if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)   {    if(!isRole("Manager"))    {     LinkButton tmp = (LinkButton)e.Item.FindControl("lbPassed");     if(tmp.Text == "Passed")     {      ((LinkBu...

How To Get the Gridview's Field Value if the Column's Visible=False..?
Here is the scenario: My sql query returns Name, ID, SSNum. I use a gridview which has 3 columns Name, ID and SSNum (with SSNum is set visible=false) so the gridview only shows Name and ID. I need to use part of SSNum to form a NewUserID in the code behind. Im doing this by using the gvResult_OnRowDataBound. So far, if I set SSNum column's visible=false then I wont see this field on row data bound. Any suggestion as how to obtain this SSNum from the code behind?  you can set it as the datakey on the properties of the grid view and use this codethis.GridView1.DataKeys[this.Gri...

Dropdownlist's Value Updates Another Dropdownlist's list
Hi, I'm looking for the best way to update a dropdownlist's list based on a value choosen from another dropdownlist on the page. A common example of this is there are two DDLs.  One for state and one for county.  The county DDL is left blank until a state is choosen.  Once a state is choosen then the county dropdownlist is filled. Now here is part of my dilemma.  I'm entered other data on the form, so if I postback I need some way of knowing that the postback is just for the purpose of filling the secont dropdownlist.  In addition everytime that first DDL is change...

cant't get value from a dropdownList in DetailsView's TemplateField on update or insert mode
i insert a DropdownList into the detailview' InsertItemTemplate,and  want to the detailview can fetch the value of the DropdownList which i select. But i do not know how to code it.   Should i use the <asp:ControlParamete> section of  the ObjectDataSource? how to i  set the value of it's ControlID property? my code is  here: <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">    <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="100%" AutoGenerateRows="False"   &nb...

The value's length for key 'data source' exceeds it's limit of '128'.
Hii, When I tried to bind dropdownlist to database by declaring connection string in the code behind file I am getting this error: The value's length for key 'data source' exceeds it's limit of '128'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentException: The value's length for key 'data source' exceeds it's limit of '128'.Source Error: An...

Control's (object's) default value??
How or where can I see an control's default value? Say, for example the Visible property for webcontrols? Would be preferable if I could quickly see it in Visual Web Developer but MSDN online documentation would also be nice. Also how can I do this in general for all .Net objects?...

why a string column with a '' value synchronize from ASE to ASA, in ASA it's value become ' ' with a blank in it?
"darcy.zhai" <> wrote in news:43967064@forums-2-dub of sybase.public.sqlanywhere.mobilink: Please try to ask your question in the _body_ of the post, not the subject line. There are many people that never really look at the subject line (like myself most of the time). > why a string column with a '' value synchronize from ASE to ASA, in > ASA it's value become ' ' with a blank in it? This has to do with nullability. First, connect to ASE via ISQL and try the same query ML is executing. What is returned? An...

What's the simplest way to override a property's default value?
I'm trying to write a TCustomDBGrid descendant that's designed to feel like a TListBox. One of the things I want to change is the Options property's defaults. TCustomDBGrid defines Options as: property Options: TDBGridOptions read FOptions write SetOptions default [dgEditing, dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgConfirmDelete, dgCancelOnExit]; Trying to override that in my class with property Options: TDBGridOptions default [dgTitles, dgTabs, dgRowSelect, dgAlwaysShowSelection, dgCancelOnExit]; doesn't...

Mozilla get's no style tag's from .net server.
I made a webform in web matrix, and it looks fine in IE. If I open the same link whit Mozilla, it looks like a disaster. When I look at the HTML source, there are no style tags as in IE. But when I save the HTML source from IE (the one whit style tags) and open it in Mozilla, it looks perfect. So the onley reason the webform looks wrong in Mozilla, is because the .net server detects Mozilla and not IE and sends a style less HTML source. Wy? Mozilla does support style tags. Is there a solution for this bug? Or is this a uh.. feature? ..Mivz Update, I have got some more info. It is ...

Couldn't get control's value after integration
I have a personal website in asp. I would like to add a aspx file into it. After I did that aspx page and tested ok, I copied the page layout from asp to aspx. After doing this, my aspx didn't work anymore. The aspx page did perform an insertion query but there is not value in textbox control (both txtName and txtFeedback). I wonder what went wrong. Also, when I rollover to div in line 11, it says, "Validation (Internet Explorer 6): Element 'div' must be included within a parent element." and when when I roll over to asp in line 20, it says "Unrecognized ...

How to get a Html.CheckBox 's Ischecked Value using FormMethod.Get?
How to get a Html.CheckBox 's Ischecked Value using  FormMethod.Get? When I use the FormMethod.Post Method, I can get the Html.CheckBox 's Ischecked Value by string ischeck = Request.Form.GetValues("TestCheckBox")[0]; But when I use the FormMethod.Get  Method, How can I get the Html.CheckBox 's Ischecked Value ?  Lord Jesus, Thank you for creating me and guiding me through everyday. Mike108 ASP.NET MVC Blog: You actually just... can't. The client's browser will only submit the checkbox' value when it has bee...

Stored procedure to get column's default values
How can I use a stored procedure to get a column's default values from system tables? My purpose is to reset a lot columns' default values from many tables, so I attempt to prepare a stored procedure to do this. Thanks, DS not a proc, but a sql, you can put it into a proc and fine tune the results select Table_name = object_name(, Column_name = isnull(, 'NULL'), Type = isnull(convert(char(30), x.xtname), isnull(convert(char(30), get_xtypename(c.xtype, c.xdbid)),, Length = c.length, Prec = c.prec,...

Web resources about - Default value for Dropdownlist that get's it's value from Database. -

Tag Synonyms - Stack Overflow
Q&A for professional and enthusiast programmers

Accidental entities - what about the UI?
This post is a follow-up to my previous blog post " Accidental entities - you don't need that identity ". In that post, we followed a consultant ...

Flash Builder Plug-in for SAP Netweaver Gateway - Adobe Labs
Search Navigation Labs Home Wiki Home Technologies Alchemy Blueprint Camera Profiles and DNG Profile Editor Cirrus ColdFusion 10 DNG Codec Fireworks ...

Air Astana - Join Now
Thank you for considering membership of Nomad Club – Kazakhstan’s first Frequent Flyer Program. You could soon be on your way to earning free ...

Languages -> C# Articles, Tutorials, Examples: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Blog - Avisra
Avisra Web Development Home Blog Services Contact The Blog Latest Module Builder: How can we make it better? Sitefinity by Chris on July 2, 2013 ...

Online Booking Tool Support
This Online Booking Tool Support page might help you as you navigate through the process of booking travel through your OBT provided by CWT. ...

@Html and beyond
... ); } return string .Format( “{0}{1}” , me.Member.Name, id); } The Html building methods are quite simple. Building a dropdownlist for a ...

Top Ranked Articles
Top Ranked Articles - Free source code and tutorials for Software developers and Architects.; Updated: 23 Feb 2013

Unit Testing DateTime.Now Without Isolation
public virtual void SeedWithYearsSince(DropDownList list, int year){ for (int index = year; index

Resources last updated: 12/4/2015 11:04:50 PM