asp.net how to add TemplateField programmatically for about 10 dropdownlist...

Posted by dotnet-practitioner on Stack Overflow See other posts from Stack Overflow or by dotnet-practitioner
Published on 2009-05-03T01:34:03Z Indexed on 2010/04/15 12:03 UTC
Read the original article Hit count: 228

This is my third time asking this question. I am not getting good answers regarding this. I wish I could get some help but I will keep asking this question because its a good question and SO experts should not ignore this...

So I have about 10 dropdownlist controls that I add manually in the DetailsView control manually like follows. I should be able to add this programmatically. Please help and do not ignore...

          <asp:DetailsView ID="dvProfile" runat="server" 
                AutoGenerateRows="False" DataKeyNames="memberid" DataSourceID="SqlDataSource1" 
                OnPreRender = "_onprerender"

                Height="50px" onm="" Width="125px">

                <Fields>


                   <asp:TemplateField HeaderText="Your Gender">
                            <EditItemTemplate>
                                    <asp:DropDownList ID="ddlGender" runat="server" 
                                                    DataSourceid="ddlDAGender"
                                                    DataTextField="Gender" DataValueField="GenderID"
                                                    SelectedValue='<%#Bind("GenderID") %>'
                                        >

                                     </asp:DropDownList>
                            </EditItemTemplate>

                            <ItemTemplate >
                                        <asp:Label Runat="server" Text='<%# Bind("Gender") %>' ID="lblGender"></asp:Label>
                            </ItemTemplate>

so on and so forth...



                        <asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
                    </Fields>
                </asp:DetailsView>

=======================================================

Added on 5/3/09

This is what I have so far and I still can not add the drop down list programmatically.

private void PopulateItemTemplate(string luControl)
{
    SqlDataSource ds = new SqlDataSource();
    ds = (SqlDataSource)FindControl("ddlDAGender");
    DataView dvw = new DataView();
    DataSourceSelectArguments args = new DataSourceSelectArguments();

    dvw = (DataView)ds.Select(args);
    DataTable dt = dvw.ToTable();


    DetailsView dv = (DetailsView)LoginView2.FindControl("dvProfile");

    TemplateField tf = new TemplateField();
    tf.HeaderText = "Your Gender";
    tf.ItemTemplate = new ProfileItemTemplate("Gender", ListItemType.Item);
    tf.EditItemTemplate = new ProfileItemTemplate("Gender", ListItemType.EditItem);
    dv.Fields.Add(tf);
}


    public class ProfileItemTemplate : ITemplate
    {
        private string ctlName;
        ListItemType _lit;
        private string _strDDLName;
        private string _strDVField;
        private string _strDTField;
        private string _strSelectedID;
        private DataTable _dt;


        public ProfileItemTemplate(string strDDLName, 
                                                            string strDVField, 
                                                            string strDTField,
                                                            DataTable dt
                                                            )
        {
            _dt = dt;
            _strDDLName = strDDLName;
            _strDVField = strDVField;
            _strDTField = strDTField;
        }

        public ProfileItemTemplate(string strDDLName,
                                                        string strDVField,
                                                        string strDTField,
                                                        string strSelectedID,
                                                        DataTable dt
                                                        )
        {
            _dt = dt;
            _strDDLName = strDDLName;
            _strDVField = strDVField;
            _strDTField = strDTField;
            _strSelectedID = strSelectedID;
        }

        public ProfileItemTemplate(string ControlName, ListItemType lit)
        {
            ctlName = ControlName;
            _lit = lit;


        }



        public void InstantiateIn(Control container)
        {


            switch(_lit)
            {
                case ListItemType.Item : 
                    Label lbl = new Label();
                    lbl.DataBinding += new EventHandler(this.ddl_DataBinding_item);
                    container.Controls.Add(lbl);
                    break;
                case ListItemType.EditItem :
                    DropDownList ddl = new DropDownList();
                    ddl.DataBinding += new EventHandler(this.lbl_DataBinding);
                    container.Controls.Add(ddl);

                    break;
            }
        }

        private void ddl_DataBinding_item(object sender, EventArgs e)
        {

            DropDownList ddl = (DropDownList)sender;
            ddl.ID = _strDDLName;
            ddl.DataSource = _dt;
            ddl.DataValueField = _strDVField;
            ddl.DataTextField = _strDVField;
        }



        private void lbl_DataBinding(object sender, EventArgs e)
        {
            Label lbl = (Label)sender;
            lbl.ID = "lblGender";

            DropDownList ddl = (DropDownList)sender;
            ddl.ID = _strDDLName;
            ddl.DataSource = _dt;
            ddl.DataValueField = _strDVField;
            ddl.DataTextField = _strDTField;
            for (int i = 0; i < _dt.Rows.Count; i++)
            {
                if (_strSelectedID == _dt.Rows[i][_strDVField].ToString())
                {
                    ddl.SelectedIndex = i;
                }
            }

            lbl.Text = ddl.SelectedValue;
        }
    }

Please help me....

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about edititemtemplate