none
DropDownLists in Visual WebParts when created in Visual Studio 2010

    Question

  • In MS Access, a form's recordsource is based on a table and the recordsource of a control is that of a field on the table the form is based on.  A recordsource of a control such as a combo box can be linked to a Table/Query where you can show a description and hide the primary key but bind that column to the control source of that control. 

    I need the same type of functionality for Visual WebParts; can anyone assist me?

    I am creating Visual WebParts in Visual Studio 2010.

    I can find how to bind a dropdownlist for a Visual WebPart however I can't find anything on what I need it to do.  I don't need it to be cascading or anything like that. 

    thank you.


    Sharon

    Thursday, June 14, 2012 2:28 PM

Answers

  • Hi,

    You need to populate the dependent DropDownlist on the SelectedIndexChanged event of the parent DropDownlist.

    The code should be something like this:

    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList ddlParent = new DropDownList();
        DropDownList ddlChild = new DropDownList();
                
        // method to bind data to the dropdownlist
        DataBindParent();
    
        ddlParent.SelectedIndexChanged += new EventHandler(ddlParent_SelectedIndexChanged);
        ddlParent.AutoPostBack = true;
    
    }
    
    void ddlParent_SelectedIndexChanged(object sender, EventArgs e)
    {
        // method to bind data to the dropdownlist
        DataBindChildren(ddlParent.SelectedValue);
    }
    

    Cheers,

    Dan.


    You can find my blog here: http://developertrack.blogspot.com

    Thursday, June 14, 2012 3:00 PM

All replies

  • Hi,

    You need to populate the dependent DropDownlist on the SelectedIndexChanged event of the parent DropDownlist.

    The code should be something like this:

    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList ddlParent = new DropDownList();
        DropDownList ddlChild = new DropDownList();
                
        // method to bind data to the dropdownlist
        DataBindParent();
    
        ddlParent.SelectedIndexChanged += new EventHandler(ddlParent_SelectedIndexChanged);
        ddlParent.AutoPostBack = true;
    
    }
    
    void ddlParent_SelectedIndexChanged(object sender, EventArgs e)
    {
        // method to bind data to the dropdownlist
        DataBindChildren(ddlParent.SelectedValue);
    }
    

    Cheers,

    Dan.


    You can find my blog here: http://developertrack.blogspot.com

    Thursday, June 14, 2012 3:00 PM
  • Hi,

    Lets say you want to bind a drop down list from a sql table using LINQ to SQL, the code would look something like this:

    I have a connection string in the web.config file called dbconnectionString,

    I have a table in SQL called ItemTable, that has two fields, Id (Primary key), and a text field called Name

    I have a drop down list called ddlItems.

    using (sqlDataContext context = new sqlDataContext(ConfigurationManager.ConnectionStrings["dbconnectionString"].ToString())
    {
    //get the items
    var itemList = context.ItemTable.ToList();
    
    //bind and set what users will see
    ddlItems.DataSource = itemList;
    ddlItems.DataValueField = "Id";
    ddlItems.DataTextField = "Name"
    ddlItems.DataBind();
    }
    Regards


    I find it distasteful to beg for 'Mark as Answer' and 'Mark as helpful'. It's supposed to be about helping people, not about getting the high score.

    Monday, June 18, 2012 1:13 PM
  • Thanks, I think this is what I need.  I'm testing it now.

    Sharon

    Monday, June 18, 2012 2:53 PM
  • Thanks for the reply however I this doesn't seem like what I need.

    Sharon

    Monday, June 18, 2012 2:54 PM