title: asp.net how to add TemplateField programmatically for about 10 dropdownlist
body: <p>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...</p>
  
  <p>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...</p>
  
  <pre><code>  &lt;asp:DetailsView ID="dvProfile" runat="server" 
          AutoGenerateRows="False" DataKeyNames="memberid" DataSourceID="SqlDataSource1" 
          OnPreRender = "_onprerender"
  
          Height="50px" onm="" Width="125px"&gt;
  
          &lt;Fields&gt;
  
  
             &lt;asp:TemplateField HeaderText="Your Gender"&gt;
                      &lt;EditItemTemplate&gt;
                              &lt;asp:DropDownList ID="ddlGender" runat="server" 
                                              DataSourceid="ddlDAGender"
                                              DataTextField="Gender" DataValueField="GenderID"
                                              SelectedValue='&lt;%#Bind("GenderID") %&gt;'
                                  &gt;
  
                               &lt;/asp:DropDownList&gt;
                      &lt;/EditItemTemplate&gt;
  
                      &lt;ItemTemplate &gt;
                                  &lt;asp:Label Runat="server" Text='&lt;%# Bind("Gender") %&gt;' ID="lblGender"&gt;&lt;/asp:Label&gt;
                      &lt;/ItemTemplate&gt;
  
                  &lt;asp:CommandField ShowEditButton="True" ShowInsertButton="True" /&gt;
              &lt;/Fields&gt;
          &lt;/asp:DetailsView&gt;
  </code></pre>
  
  <p>=======================================================</p>
  
  <p>Added on 5/3/09</p>
  
  <p>This is what I have so far and I still can not add the drop down list programmatically.</p>
  
  <pre><code>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 &lt; _dt.Rows.Count; i++)
          {
              if (_strSelectedID == _dt.Rows[i][_strDVField].ToString())
              {
                  ddl.SelectedIndex = i;
              }
          }
  
          lbl.Text = ddl.SelectedValue;
      }
  }
  </code></pre>