Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I was trying on a project for creating a user control dynamically. The requirement is such that a panel needs to be appended with its controls repeatedly(on adding through command). Also, it needs to be populated on load with all its members. It should also append an empty user control beneath all the loaded panel in edit mode and on saving it should follow alongwith all the loaded members.
I had taken a user control and added a checkbox and a text box inside it. On loading this user control dynamically from a page I need to populate all its value from database(depends on how many record each user control will have).Example - suppose I have 3 rows of records, so my user control should repeat 3 times in my panel. And as soon as i say add another user control, it should load all the 3 items alongwith another empty user control and also save all the 4 on saving it finally.
I tried it all ways but could not save all the records in edit mode. Can i have a demo sample which could help me in understanding current scenario.

Thanks in advance.
Posted
Comments
_Amy 24-Sep-12 10:16am    
Where is your code? What have you tried so far? Where you stuck?
Karthik. A 24-Sep-12 22:45pm    
Post what you have done so far. As a side-note/hint, read about asp:Repeater control.

1 solution

Hello,

You can use a listview or a repeater.

C#
<asp:ListView ID="lvData" runat="server" OnItemDataBound="lvData_ItemDataBound" DataKeyNames="ID">
   <LayoutTemplate>
        <div id="itemPlaceholder" runat="server"></div>
   </LayoutTemplate>
   <ItemTemplate>
       <uc1:mycustomcontrol id="mycustomcontrol1" runat="server" />
   </ItemTemplate>
</asp:ListView>


in the code behind we will asssign datasource to the listview from the page load method and later handle ItemDataBound Event of the listview to populate data for the customcontrol

C#
protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
          IList dataSource=GetData();//getData from the database
          lvData.DataSource=dataSource;
          lvData.DataBind();//bind data
    }
}

protected void lvData_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
       ListViewDataItem dataItem = (ListViewDataItem)e.Item;
       int id = int.Parse(lvData.DataKeys[dataItem.DataItemIndex].Values[0].ToString());
       mycustomcontrol mycustomcontrol1 = e.Item.FindControl("mycustomcontrol1") as mycustomcontrol;
       mycustomcontrol.refresh(id);
    }
}
 
Share this answer
 

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