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

    jeudi 14 juin 2012 14:28

Réponses

  • 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

    jeudi 14 juin 2012 15:00

Toutes les réponses

  • 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

    jeudi 14 juin 2012 15:00
  • 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.

    lundi 18 juin 2012 13:13
  • Thanks, I think this is what I need.  I'm testing it now.

    Sharon

    lundi 18 juin 2012 14:53
  • Thanks for the reply however I this doesn't seem like what I need.

    Sharon

    lundi 18 juin 2012 14:54