Dynamically Create Dropdownlist inside Listview/Repeater/Gridview

itinfo7
itinfo7 used Ask the Experts™
on
Front End:aspx page

<asp:ListView ID="lvAutoMergeToken" runat="server" >
    <LayoutTemplate>
      <div runat="server" id="groupPlaceholder">
            <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
     </div>
    </LayoutTemplate>
    <ItemTemplate>
       <div id="Td1"  runat="server" class="item">
            <asp:TextBox ID="txtToken" runat="server" Text='<%#Eval("defaultMerge")%>' Visible='<%#Eval("defaultVisible")%>'></asp:TextBox>
     </div>
    </ItemTemplate>
</asp:ListView>

<asp:ListView ID="lvDropdowntoken" runat="server" >
    <LayoutTemplate>
      <div runat="server" id="groupPlaceholder">
            <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
     </div>
    </LayoutTemplate>
    <ItemTemplate>
       <div id="Td1"  runat="server" class="item">
          
           <asp:DropDownList ID="ddltoken" runat="server" DataTextField='<%#Eval("defaultMerge")%>' Visible='<%#Eval("defaultVisible")%>'  >
           </asp:DropDownList>
     </div>
    </ItemTemplate>
</asp:ListView>



aspx.cs Page

  DataTable tempDataTable = new DataTable();
                tempDataTable.Columns.Add(new DataColumn("defaultMerge", typeof(string)));
                tempDataTable.Columns.Add(new DataColumn("defaultVisible", typeof(bool)));              
                DataRow dr;               
                ArrayList al = new ArrayList();
                DataTable dropdowntable = new DataTable();            
                dropdowntable.Columns.Add(new DataColumn("defaultMerge", typeof(string)));
                dropdowntable.Columns.Add(new DataColumn("defaultVisible", typeof(bool)));
                DataRow ddr;
                ArrayList dropdownlist = new ArrayList();
                al.Add(DictionaryValue);

                 for (int i = 0; i < al.Count; i++)
                {
                    dr = tempDataTable.NewRow();
                    dr[0] = al[i].ToString();
                    dr[1] = true;
                    tempDataTable.Rows.Add(dr);
                }
                DataView dv = new DataView(tempDataTable);
                lvAutoMergeToken.DataSource = dv;
                lvAutoMergeToken.DataBind();

Open in new window



By this way I am adding dynamic textbix inside the listview.but I really don't know how can I add the Dropdown dynamically.By the same method as depends on data I have to add dropdownlist dynamically everytime it change depends upon data.

sometime may be 1 or 2 or any number.

Please help me to do this.after so much afford I am able to do for textbox but not any idea for dropdownbox..
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
try below code

code behind:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        DropDownList ddl = new DropDownList();
        //add items here
        ddl.AutoPostBack = true;
        ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);

        e.Row.Cells[0].Controls.Add(ddl);
    }
}

it may helps.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial