?
Solved

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

Posted on 2010-09-01
4
Medium Priority
?
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 

Assisted Solution

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

Assisted Solution

by:richard_hughes
richard_hughes earned 500 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 600 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 400 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

Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

800 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