.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Questions about setting dropdownlist selection programatically

Posted By:      Posted Date: September 01, 2010    Points: 0   Category :ASP.Net
I don't understand some things about setting the current selection of dropdownlists from code. I have a dropdown list of people's names where the Value is a Person_ID.  This gets loaded from SQL, and I added an Item which is "Please select..." to the top of the list and gave it a value of 0.  AppendDataBoundItems is true.   If I want to select an item from code can I use dropdownlist.SelectedValue=8 (for example)? Do I also have to set the SelectedItem.Text or does tha above do that for me? Can I instead set the SelectedIndex to some number if I happen to know it?  Will that set the SelectedValue and SelecteItem for me? If I want to set the list back so that nothing is selected (and Please select... shows) should I set SelectedIndex=0?  Is that the same as setting SelectedValue=0 in this case (since "Please select" has a value of 0)? I guess I'm confused on which of these 3 properties I should be using, to select an item (or deselect all items) in the list, using code. Thanks.

View Complete Post

More Related Resource Links

DropDownList SelectedIndexChanged event not fired when programatically setting SelectedValue


In our asp.net web application we load the dropdown list boxes in the page load.  We than set the SelectedValue of each dropdown list box to an object from session.  We have event handlers for many of the SelectedIndexChanged events of the drop down lists boxes, but these do not get executed when we set the selected value programmatically.


Someone else mentioned this is by design, and only when a user manually changes the drop down list selected index, will the event get raised.


I would think this is a common scenario of wanting the selectedindexchanged event handled when setting the selectedvalue/selectedindex/listitem's selected property, of a DropDownList.  What is the suggested way to handle this?  Is there a significant drawback of creating a custom control inherited from DropDownList that has the functionality of raising this event when changing the SelectedIndex or SelectedValue?



ASP.NET Multiple Selection DropDownList with AJAX HoverMenuExtender

Recently, I was looking for a multiple selection dropdownlist control for my new project. After spending some time researching for it, I decided to put together all my finding in one web user control. This web user control consists of an ASP.NET AJAX HoverMenuExtender, JavaScript, StyleSheet and CheckBoxListExCtrl . The final product will work with or without a MasterPage and you can drag and drop more than one instances of the control on to the page.

2 questions - Dropdownlist from SQL Table and Insert into several tables

We're trying to convert an older access app with a sql server back end (with many tables) to Sharepoint. There are entry forms, update forms, look up forms, reports, etc the 2 questions I have are: 1. How can I create a dropdownlist that gets its items from a SQL table? 2. How can I create a form that, when filled out, puts items into multiple tables? Basically, I'd like to know if any of this is possible in 2010, and does anyone have any tips on where to start?    

Setting printer duplex programatically

How do you programmatcally set a printer to duplex?   The information I've found online indicates that GetPrinter/SetPrinter should do the trick, but so far I've had mixed results.    I'm automating Microsoft Word to print documents.  Some of the documents required to be duplexed.  So far in my research I've found the following web pages that provide somewhat helpful information. http://support.microsoft.com/default.aspx/kb/315720 How to enable and disable a printer by using the SetPrinter function in Visual C# http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q140/2/85.asp&NoWebContent=1 How To Modify Printer Settings by Using SetPrinter http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q167/3/45.asp&NoWebContent=1 How To Modify Printer Settings with the DocumentProperties() Function http://support.microsoft.com/kb/828638  How to set duplex printing for Microsoft Word Automation clients (This article appears to be starting pointing for the CodeProject article) http://www.codeproject.com/dotnet/NET_Printer_Library.asp  Changing printer settings using C# I've included the code I'm using now.  I don't get any errors and everything appears to work, but the Duplex setting on the printer is not set reliably.  My check is to open the printer's properties and the "Pr

DropdownList Selection to Populate TextBox

I have a dynamically generated dropdownlist, that lists e.g. Company Names. I want to be able to select a particular Company and display associated information e.g. Name, Address, City, State... etc. in text boxes adjacent to the list. I am trying to fire the following code from the dropdownlist "OnSelectedIndexChanged" property, but cannot 1) get the code to fire upon selection, and 2) populate the text boxes... any suggestions are appreciated!! Thanks CODE:     Public Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Facility.SelectedIndexChanged Dim FacilityAddressDetail As String  Dim FacilityAddress2 As String  Dim FacilityCity As String  Dim FacilityState As String  Dim FacilityZip As String Dim   FacilityAddressDetail = FacilityAddress2 = FacilityCity = FacilityState = FacilityZip =       mySettingsSIC = AppSettings   dgiSelectedIndexChange As DataGridItemFor Each dgiSelectedIndexChange In myDataGrid.ItemsCType(dgiSelectedIndexChange.FindControl("FacilityAddressDetail"), TextBox).TextCType(dgiSelectedIndexChange.FindControl("FacilityAddress2"), TextBox).TextCType(dgiSelectedIndexChange.FindControl("FacilityCity"), TextBox).TextCType(dgiSelectedIndexChange.FindControl("FacilityState"), TextBox).TextCType(dgiSelec

Dropdownlist selection

Hi All,          I have 4 columns in same table(Region,Site,Compaign,LOB).I should display each in seperate dropdownlist.Based on selection of region i should display region related sites based on Sites i should display compaign and finally LOB.Can anyone send me sample code on how to acheive this using selected index changed event with ajax extension not ajax toolkit.I think i should use update panel for all 4 dropdownlists.Is that correct.    

Populate dropdownlist in Edit mode programatically


I'm trying to programatically populate some dropdownlists inside a formview in EDIT mode, like this below.

 Dim FVM As String = FormViewMemberProfile.CurrentMode.ToString

        If FVM = "Edit" Then

            Dim ddlDays As DropDownList = CType(FormViewMemberProfile.FindControl("ddlDays"), DropDownList)
            Dim ddlYears As DropDownList = CType(FormViewMemberProfile.FindControl("ddlYears"), DropDownList)

            Dim i As Integer = 1

            For i = 1 To 31

            Dim k As Integer

            For k = 1930 To 2008

        End If

I orginally had this running as part of the pagelaod event, but I'm thinking that b/c the page loads first, it doesn't populate the fields in EDIT mode until after the data is saved, and then page is reloaded. Now it knows it's in "Edit" mode. Am I thinking clearly here? Not sure if this is the order of things... Anyhow...

So I thought I should just use a formview_modechanged (or similar event) so when the user switches modes, it populates the fields - but I'm not sure which event I should use. I've tried "Modechanging", "modechanged", "prer

DropDownList selection doesn't show


I've got two DropDownLists that are filled using a couple of SqlDataSources, like so:

    <asp:SqlDataSource ID="dsCategories" runat="server" 
    ConnectionString="<%$ ConnectionStrings:EPiServerDB %>" 
    SelectCommand="img_GetCategories" SelectCommandType="StoredProcedure">
            <asp:Parameter DefaultValue="1" Name="CountryID" Type="Byte" />
    <asp:DropDownList ID="categories" runat="server" AutoPostBack="True" 
    DataSourceID="dsCategories" DataTextField="Translation" 
    DataValueField="CategoryID" Width="220px">
    <asp:SqlDataSource ID="dsSubcategories" runat="server" 
    ConnectionString="<%$ ConnectionStrings:EPiServerDB %>" 
    SelectCommand="img_GetSubcategories" SelectCommandType="StoredProcedure">
            <asp:Parameter DefaultValue="1" Name="CountryID" Type="Byte" />
            <asp:ControlParameter ControlID="categories" DefaultValue="" Name

setting dropdownlist item in page load


 I can't get the following to set on page load.  I want the month selected be the current month, on page load.  What needs to change here?

DropDownListCCExpMo.SelectedItem.Selected = Date.Today.Month


Filter dropdownlist based on previous selection



In my newform.aspx page in Helpdesk template (when I create a new service request), I have added a dropdownlist (lookup type) that list series of issues that user can select.

I would like to add another dropdownlist that further give more options based on the selection made on the first dropdownlist.

How can I drive this second list from the first one? Thanks.


dropdownlist selection changes to first item on postback



I have been having some trouble with dropdown lists reverting back to selecting the initial item in the list after a postback once a selection has been made from the dropdown list. I have read up on this and found out that a "if not ispostback then" is required.

This works fine when I add in all the items in the dropdown list manually like:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

Hello Mr. MVP can you please reply to my questions,,,,,,"Creating SPContentDatabase programatically

Hi m trying to Create a SPContentDatabase programatically using Object Model, but getting error of "
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Deployment;

namespace CreateContentDB
  public partial class Test2 : System.Web.UI.Page
    SPWebApplication webApplication;
    //SPContentDatabaseCollection contentDBs;
    string nameDB = "CNB";
    protected void Page_Load(object sender, EventArgs e)


    protected void Button1_Click(object sender, EventArgs e)
      Uri WebAppUri = new Uri("http://amit:5003");

      SPWebApplication webApplication = SPWebApplication.Lookup(WebAppUri);

      SPContentDatabaseCollection contentDBs = webApplication.ContentDatabases;

      foreach (SPContentDatabase contentDB in contentDBs)

        if (contentDB.Status == SPObjectStatus.Online)
          contentDB.Status = SPObjectStatus.Disabled;
      //SPSite siteCollection = webApplication.Sites.Add();
    private void CreateContentDatabase(string suffix)

Cannot populate the textbox with selection from dropdownlist. Thanks

I am not able to get the selection from dropdown into the detailsview template field during update. 

 <asp:TemplateField HeaderText="SupplierName" SortExpression="SupplierName">
                  <asp:TextBox ID="TextBox1Sup" runat="server"  Text='<%# Bind("SupplierName") %>'  Visible="True"></asp:TextBox>
                  <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true" 
                  DataSourceID="AccessDataSource" DataTextField="CompanyName" 
                     DataValueField="CompanyName" AutoPostBack="True"  
                        > </asp:DropDownList>                    
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("SupplierName") %>'></asp:TextBox>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("SupplierName") %>'>&

Setting Windows Services App properties programatically


I've got a Windows Services app I've deployed and I've used the System.ServiceProcess.ServiceInstaller to set the StartType to Automatic and that works great.

How do I set the action on failure to "restart" and the wait time to one minute?

Can't find those properties but if you open up a Service's properties in the UI, there's a Recovery Tab with First Failure, Second Failure etc. and a "Restart service after __ minutes"

Where do I set these properties for my service at design time before deploying??



jQuery: Styling DropDownList Alternating Items (Rows)

In the following example, I will show how you can style any asp:dropdownlist very easily.

The example demonstrates how you can change alternating row items background color, using the fadeTo() to add a nice touch in the dropdownlist. You can change alt rows with a couple lines.

Maintain HTML Radio Button Selection Inside GridView After Postback

Long time ago I wrote an article about getting the value of the selected RadioButtons inside the GridView control. The code did not addresssed the postback issue which means that the selected radio button was cleared after the postback occurs.

Working with DropDownList and ListBox Controls in ASP.NET

"In my application i am using dropdownlist to display all categories from a table. When the user clicks this control I display all records in that category. Category table also contains column called color.

I wanted display some categories in dropdown list different color and others in different color using ASP.NET."-Anonymous
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend