Dynamically Generated Dropdownlist,

Last post 02-20-2008 10:29 AM by tbraga. 8 replies.

Sort Posts:

  • Dynamically Generated Dropdownlist,

    02-19-2008, 5:09 PM
    • Loading...
    • tbraga
    • Joined on 10-16-2007, 11:18 PM
    • Posts 132

    Hi All,

    I have a created a dynamic page that displays two columns of data. The first column shows the name and the second is a dropdownlist with their selected configuration(IT, accounting, marketing ect ect).  I have all this working so far but I'm stuck on the SelectedIndexChange event.  Because I'm autogenerating the dropdowns within a foreach statement I'm not sure what dropdowns ID is when the index is changed so I can populate the where clause on my sql update statement.  

    Here is the c# code, the only thing i have on the aspx page is a placeholder at the moment.

    protected void Mappings()

    {

    //For Each MappingID, display title and associated configuration
    SqlConnection MappingConn;
    SqlDataAdapter MappingComm;
    string connectionstring = ConfigurationManager.ConnectionStrings["Magic"].ConnectionString;
    MappingConn =
    new SqlConnection(connectionstring);

    MappingComm = new SqlDataAdapter("Select TOP 15 m.MappingID as MappingID, m.Title as Title, isnull(m.ConfigID,0) as ConfigID, s.[name] as [Config] From dbo.SDC_TitleMapping m " +
    "left outer join _Smdba_._StdCfg_ s on m.configid = s.[sequence] " +
    "order by m.title", MappingConn);

    //Create and Fill the Dataset, Create Column
    DataSet ds = new DataSet();
    DataColumn column;
    MappingComm.Fill(ds,
    "dbo.SDC_TitleMapping");int i = 0;

    //Create Report Line Break
    HtmlGenericControl lineBreak = new HtmlGenericControl("br");

     

    //Create Table
    Table tblMapping = new Table();
    tblMapping.ID =
    "tblMapping";
    tblMapping.BorderWidth = 1;
    tblMapping.Width = 450;
    tblMapping.BackColor = System.Drawing.
    Color.WhiteSmoke;
    tblMapping.HorizontalAlign =
    HorizontalAlign.Left;

    foreach (DataRow row in ds.Tables["dbo.SDC_TitleMapping"].Rows)

    {
    string Title = Convert.ToString(row["Title"]);
    string Config = Convert.ToString(row["Config"]);
    int MappingID = Convert.ToInt32(row["MappingID"]);
    int ConfigID = Convert.ToInt32(row["ConfigID"]);

    TableRow row_2 = new TableRow();
    row_2.ID =
    "row_2";
    row_2.HorizontalAlign =
    HorizontalAlign.Left;

    TableCell cell_2 = new TableCell();
    cell_2.ID =
    "cell_2" + i;
    cell_2.Width = 250;
    TableCell cell_3 = new TableCell();
    cell_3.ID =
    "cell_3" + i;
    cell_3.Width = 250;

    Label MappingLabel = new Label();
    MappingLabel.ID =
    "MappingLabel" + i;
    MappingLabel.Text = Title.ToString();

    DropDownList MappingDropdown = new DropDownList();
    MappingDropdown.ID = "MappingDropdown" + i;
    MappingDropdown.Width = 200;
    MappingDropdown.SelectedIndexChanged +=
    new EventHandler(MappingDropdown_SelectedIndexChanged);
    MappingDropdown.AutoPostBack =
    true;

    //Bind Dropdown
    SqlConnection conn;
    SqlCommand comm;
    SqlCommand commStatus;
    SqlDataReader reader;
    conn =
    new SqlConnection(connectionstring);

    try

    {
    comm =
    new SqlCommand("Select [Sequence], [Name] from _Smdba_._Stdcfg_ order by [Name]", conn);

    conn.Open();
    reader = comm.ExecuteReader();
    MappingDropdown.DataSource = reader;
    MappingDropdown.DataValueField =
    "Sequence";
    MappingDropdown.DataTextField =
    "Name";
    MappingDropdown.DataBind();
    }

    catch

    {

    dberrorlabel.Text =
    "Error: Could not load title dropdown";

    }

    finally

    {

     

    conn.Close();

    }

    if (ConfigID == 0)

    {

    MappingDropdown.Items.Insert(0,
    "");
    MappingDropdown.SelectedIndex = 0;

    }

    else

    {

    //System.Windows.Forms.MessageBox.Show(ConfigID.ToString());
    MappingDropdown.SelectedItem.Value = ConfigID.ToString();
    MappingDropdown.SelectedItem.Text = Config.ToString();

    }

    cell_2.Controls.Add(MappingLabel);
    cell_3.Controls.Add(MappingDropdown);
    row_2.Cells.Add(cell_2);
    row_2.Cells.Add(cell_3);
    tblMapping.Rows.Add(row_2);
    i = i + 1;

    }
    MappingPlaceholder1.Controls.Add(tblMapping);
    }

    Filed under: ,
  • Re: Dynamically Generated Dropdownlist,

    02-19-2008, 5:43 PM
    • Loading...
    • tbraga
    • Joined on 10-16-2007, 11:18 PM
    • Posts 132

    The MappingDropdown_SelectedIndexChanged event only has a messagebox showing "hello" in it, that doesn't fire either. 

  • Re: Dynamically Generated Dropdownlist,

    02-19-2008, 7:02 PM
    • Loading...
    • nmgomes
    • Joined on 04-09-2007, 11:53 PM
    • Portugal
    • Posts 216

    Hi,

    as far as I understand you have reported two problems:

        1. find out which DDL as triggered the event

        2. the event is not triggered at all

    I think you have only one problem related to dinamic control generation. When creating controls dinamically you must ensure that the controls are always created in the same order and that the recreation of controls must be done at OnLoad or earlier in order to make possible event triggering for those controls.

    If you follow these requirements you will endup with a set of DDL that are able of triggering the SelectedIndexChanged event.

    When you get there you can easilly get the source DDL from the first argument of the event handler using a cast to the expected type.

    good coding

    Nuno Gomes [visit my blog]
    Portugal - Europe's West Coast
    [Don't forget to click "Mark as Answer" on the post(s) that helped you.]
  • Re: Dynamically Generated Dropdownlist,

    02-20-2008, 9:06 AM
    • Loading...
    • tbraga
    • Joined on 10-16-2007, 11:18 PM
    • Posts 132

    I am calling the Mappings() function from the page_load as shown below....still not sure whats going on.  I've never dynamically created dropdowns and tried trying them to s SelectedIndexChange event, so I feel like I'm running through the fog...maybe walking at times ;)

    Anyways, thanks nmgomes, any further help would be great, I'm pretty stuck.
    Thanks!
    Tim

    protected void Page_Load(object sender, EventArgs e)
    {
      
    if (!IsPostBack)
       {
          Dropdowns();  //Populates static dropdowns
          Mappings();    //Creates mapping with dynamic dropdowns
       }
    }

  • Re: Dynamically Generated Dropdownlist,

    02-20-2008, 9:47 AM
    • Loading...
    • tbraga
    • Joined on 10-16-2007, 11:18 PM
    • Posts 132

    I made progress!    I have my SelectedIndexChange event firing, I get my dinky "hello" messagebox.  But I still need a little help with a small area.

    I'm still very uncertain how to figure out which dropdrown fired the event.  I'm looking to do something like someVar = ddl.SelectedItem.Value;

    Any idea's??

  • Re: Dynamically Generated Dropdownlist,

    02-20-2008, 10:08 AM
    Answer
    • Loading...
    • nmgomes
    • Joined on 04-09-2007, 11:53 PM
    • Portugal
    • Posts 216

    Hi Tim,

    you should do something like this

    private void DropdownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selectedValue = ((DropdownList)sender).SelectedItem.Value;
    }
     
    Nuno Gomes [visit my blog]
    Portugal - Europe's West Coast
    [Don't forget to click "Mark as Answer" on the post(s) that helped you.]
  • Re: Dynamically Generated Dropdownlist,

    02-20-2008, 10:09 AM
    Answer
    • Loading...
    • tbraga
    • Joined on 10-16-2007, 11:18 PM
    • Posts 132

    Ok I have it!!!  I'm using the "object s" in the selectedindexchange event.  Worked like a charm....hey do I get points if I answer my own post??  ;)

    Thanks everyone!

    //Answer
       DropDownList
    ddl = s as DropDownList;
       System.Windows.Forms.MessageBox.Show(ddl.SelectedItem.Text.ToString());

  • Re: Dynamically Generated Dropdownlist,

    02-20-2008, 10:25 AM
    • Loading...
    • nmgomes
    • Joined on 04-09-2007, 11:53 PM
    • Portugal
    • Posts 216

    Hi Tim,

    I'm not sure about that but you should always check to see if someone gave you the correct answer and credit those too ( please see my previous answer Wink)

    Nuno Gomes [visit my blog]
    Portugal - Europe's West Coast
    [Don't forget to click "Mark as Answer" on the post(s) that helped you.]
  • Re: Dynamically Generated Dropdownlist,

    02-20-2008, 10:29 AM
    • Loading...
    • tbraga
    • Joined on 10-16-2007, 11:18 PM
    • Posts 132

    I'm sure both would work, thanks again nmgomes for the help.  I appreciate it,

    tim

Page 1 of 1 (9 items)
Microsoft Communities
Page view counter