Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

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

so 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;
                    
            }
        }
}


SO, how can i get the value of dropdown in submit button or another event.

Please Help Me...


Thanks and Regards
Mitesh
Posted
Comments
boogac 21-Feb-13 6:42am    
i am just guessing, if you declare dropdownlist ht= new dropdownlist() public or static maybe cant you reach it from anywhere ?
[no name] 21-Feb-13 6:44am    
so what can i do in this case. please tell me

1 solution

The best way is to publish DropDownList (or just SelectedValue) through a property:
C#
public class AddTemplateToGridView : ITemplate
{
    ListItemType _type;
    string _colName;
    DropDownList _ht;

    //  publish DropDownList:
    public DropDownList InnerDropDown { get { return _ht; } } 

    //  or publish just SelectedValue:
    public string SelectedValue { get { return _ht == null ? string.Empty : _ht.SelectedValue; } }

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

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

                _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;
                
        }
    }
}
 
Share this answer
 
Comments
[no name] 21-Feb-13 7:35am    
but i can not get the value of drop down when i click on submit button. so how can i do this. please told me.
sjelen 21-Feb-13 8:39am    
To access a control on postback you can use FindControl method on a container control.
For this to work, the whole control hierarchy must be created. It's done automatically for designer controls by designer-generated code during page Init.
But for runtime generated controls you have to make sure all controls are re-created on postback.
(Read on ASP.NET page life-cycle)


Post a example of how you use AddTemplateToGridView if you need more help.
[no name] 21-Feb-13 23:53pm    
so, how can i do this.i am not sure about it what you say. so please do the example for access the value of dropdown in Submit button
sjelen 22-Feb-13 8:01am    
Post a example of how you use AddTemplateToGridView if you need more help.
[no name] 23-Feb-13 1:23am    
please tell me actually how can i get the value from the Dropdown ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900