3

我想存储dropdown在动态模板字段中动态创建的值。

如何访问dropdown提交按钮上选定值的值。

我在下面使用

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;

        }
    }
}
4

1 回答 1

6

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..

于 2014-05-02T06:30:26.100 回答