Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get dropdown value which is dynamically created

Tags:

c#

gridview

I want to store the value of dropdown which is dynamically created in dynamically template field.

How can I access the value of selected dropdown value on the submit button.

I use below

public class AddTemplateToGridView : ITemplate
{
    ListItemType _type;
    string _colName;

    public AddTemplateToGridView ( ListItemType type, string colname )
    {
        _type = type;
        _colName = colname;
    }

    void ITemplate.InstantiateIn ( System.Web.UI.Control container )
    {
        switch ( _type )
        {
            case ListItemType.Item:

                DropDownList ht = new DropDownList();
                ht.ID = "ht" + _colName;
                ht.Width = 50;
                ht.Items.Add(new ListItem("Select", "Select"));
                ht.Items.Add(new ListItem("P", "P"));
                ht.Items.Add(new ListItem("A", "A"));
                ht.Items.Add(new ListItem("H", "H"));
                ht.Items.Add(new ListItem("S", "S"));
                ht.Items.Add(new ListItem("L", "L"));
                ht.DataBinding += new EventHandler(ht_DataBinding);
                container.Controls.Add(ht);
                break;

        }
    }
}
like image 562
Mitesh Machhi Avatar asked May 06 '26 06:05

Mitesh Machhi


1 Answers

Here is a my custom code: Create a windows form "test" and on its load write the following code:

        private void test_Load(object sender, EventArgs e)
        {

            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("Values");


            dt.Rows.Add(new object[] {"Sagar","Sagar_Value" });
            dt.Rows.Add(new object[] { "Hari", "Hari_Value" });
            dt.Rows.Add(new object[] { "Ram", "Ram_Value" });

            LoadDynamicCombo(comboBox1, dt);

            dt.Clear();
            dt.Rows.Add(new object[] { "one", "one" });

            LoadDynamicCombo(comboBox2, dt);
        }


        public void LoadDynamicCombo(ComboBox _cmb,DataTable dt )
        {
            _cmb.DataSource = dt.Copy();
            _cmb.DisplayMember = "Name";
            _cmb.ValueMember = "Values";

        }

Datatable with columns "Name" and "Values" passed to function "LoadDynamicCombo" accepts argument of your combo and datatable. Combo1 is filled with first set of datas whereas Combo2 is filled with second set of datas..

like image 128
Sagar Dev Timilsina Avatar answered May 08 '26 18:05

Sagar Dev Timilsina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!