Solved

.NET ASP C# how do you set dropdown default in pageload?

Posted on 2010-09-01
4
358 Views
Last Modified: 2013-11-08
Expert:
I have a 3.5 dot net C# web application with a drop down that is currently loaded from the database. However now I need to set the dropdown default to --- when the web page first appears. Can some one show me the code how this can be done in the page load function such that "--- " appears in the dropdown list when the first time page appears!
0
Comment
Question by:tcmmaxt
4 Comments
 

Assisted Solution

by:asutorius
asutorius earned 125 total points
ID: 33576156
Dropdownlist1.Items.Add("0",new ListItem(0,"---"));
0
 
LVL 9

Assisted Solution

by:richard_hughes
richard_hughes earned 125 total points
ID: 33576209
Hello tcmmaxt

After the dropdown has been populated, try this:

Thanks,

Richard Hughes
myDropDown.Items.Insert(0, new ListItem("---", "0"));

Open in new window

0
 
LVL 3

Accepted Solution

by:
vijayanandgawle earned 150 total points
ID: 33576323
ddlRouteGroup.Items.Add(new ListItem("No Route Groups", ""));

 ddlFamily.Items.Insert(0, new ListItem("No Addresses Found", "-1"));

while in inser we can give postion to insert that value.

All other cases covered in below links
Ref:http://peterkellner.net/2007/10/17/dropdownlistdefaultsetting/
http://forums.asp.net/p/1122833/1758287.aspx

0
 
LVL 11

Assisted Solution

by:princeatapi
princeatapi earned 100 total points
ID: 33576482
very important thing is you must add the code inside the

 if (!Page.IsPostBack)
 {
      // After binding your ddlist  from db
     // Code to insert your default list item at 0th position
}
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now