1 Replies - 2332 Views - Last Post: 04 September 2013 - 04:19 AM Rate Topic: -----

#1 itzmePoulami  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 12-March 13

Adding dynamic controls to a ASP.NET webpage

Posted 04 September 2013 - 01:08 AM

I want to create dynamic controls on button click.

.aspx page:
 <asp:ListView ID="lvDynamicControls" runat="server" 
            ItemPlaceholderID="itemPlaceholder" 
            onitemdatabound="lvDynamicTextboxes_ItemDataBound">   
                <LayoutTemplate>     
                <table>       
                <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>     
                </table>   
                </LayoutTemplate>  
                <ItemTemplate>    
                <tr> 
                <td><asp:TextBox ID ="txt1" runat ="server"></asp:TextBox></td> 
                <td><asp:CheckBox ID ="chck2" runat ="server" /></td>
                <td> <asp:RadioButton ID ="rbtn2" runat ="server" /></td> 
                <td><asp:DropDownList ID ="drpdwn2" runat ="server" Width="66px" >
                <asp:ListItem Value="Adam" Selected="true"></asp:ListItem>
                    <asp:ListItem Value="Brian"></asp:ListItem>
                    <asp:ListItem Value="Carly"></asp:ListItem>
                    <asp:ListItem Value="Damien"></asp:ListItem>
                    <asp:ListItem Value="Ester"></asp:ListItem>
                </asp:DropDownList></td>  
                </tr>   
               </ItemTemplate>      
            </asp:ListView>
             <asp:Button ID ="btnAdd" runat ="server"  Text ="ADD" onclick="btnAdd_Click"/>




.aspx.cs
 
 private List<string> GetDataSource()
    {
        List<string> dataSource = null;

        if (ViewState["DataSource"] != null)
        {
            dataSource = (List<string>)ViewState["DataSource"];
        }
        else
        {
            dataSource = new List<string>();
            dataSource.Add(string.Empty);
            ViewState["DataSource"] = dataSource;
        }

        return dataSource;
    }

    private void SetDataSource(List<string> dataSource)
    {
        ViewState["DataSource"] = dataSource;
    }

    private void UpdateDataSource()
    {
        List<string> dataSource = new List<string>();

        foreach (ListViewItem item in this.lvDynamicTextboxes.Items)
        {
            if (item is ListViewDataItem)
            {
                TextBox txt = (TextBox)item.FindControl("txt1");
                dataSource.Add(txt.Text);
                RadioButton rbtn = (RadioButton)item.FindControl("rbtn2");
                dataSource.Add(rbtn.Text);
                CheckBox chck = (CheckBox)item.FindControl("chck2");
                dataSource.Add(chck.Text);
                DropDownList drpdwn = (DropDownList)item.FindControl("drpdwn2");
                dataSource.Add(drpdwn.Text);
            }
        }
        this.SetDataSource(dataSource);
    }


    private void IncrementCount()
    {
        List<string> dataSource = this.GetDataSource();
        dataSource.Add(string.Empty);
        this.SetDataSource(dataSource);
    }

    private void BindListView()
    {
        List<string> dataSource = this.GetDataSource();
        this.lvDynamicControls.DataSource = dataSource;
        this.lvDynamicControls.DataBind();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        this.UpdateDataSource();
        this.IncrementCount();
        this.BindListView();
    }


    protected void lvDynamicControls_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item is ListViewDataItem)
        {
            TextBox txt = (TextBox)e.Item.FindControl("txt1");
            txt.Text = ((ListViewDataItem)e.Item).DataItem.ToString();
            RadioButton rbtn = (RadioButton)e.Item.FindControl("rbtn2");
            rbtn.Text = ((ListViewDataItem)e.Item).DataItem.ToString();
            CheckBox chck = (CheckBox)e.Item.FindControl("chck2");
            chck.Text = ((ListViewDataItem)e.Item).DataItem.ToString();
            DropDownList drpdwn = (DropDownList)e.Item.FindControl("drpdwn2");
            drpdwn.Text = ((ListViewDataItem)e.Item).DataItem.ToString();
        }
    }




Output:
attached file 1: initialstate.jpg (this is how the page looks after page load).
Attached Image
attached file 2: afterpostback.jpg (this is how the page looks after i enter "hello!!!" in the textbox and select "Carly" from the dropdownlist).
Attached Image

I know there's a mistake in the code, most probably in the "lvDynamicControls_ItemDataBound()" event. How do I specify if the radio button and the checkbox are checked or not(.checked doesnot work). Please help.

This post has been edited by itzmePoulami: 04 September 2013 - 01:21 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Adding dynamic controls to a ASP.NET webpage

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 442
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: Adding dynamic controls to a ASP.NET webpage

Posted 04 September 2013 - 04:19 AM

What exactly are you trying to do? What is the code supposed to be doing? Also, did you read through the links in your other post about working with dynamically added controls?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1