面板内部动态创建的用户控件 [英] Dynamically created user control inside panel

查看:64
本文介绍了面板内部动态创建的用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我正在尝试一个用于动态创建用户控件的项目.要求是这样的:面板需要重复添加其控件(在通过命令添加时).另外,需要在加载时填充其所有成员.它还应在编辑模式下在所有已加载的面板下面附加一个空的用户控件,并在保存时将其与所有已加载的成员一起跟随.
我已经接受了一个用户控件,并在其中添加了一个复选框和一个文本框.从页面动态加载此用户控件时,我需要从数据库中填充其所有值(取决于每个用户控件将拥有多少条记录).示例-假设我有3行记录,因此我的用户控件应在其中重复3次我的面板.一旦我说添加另一个用户控件,它就应该加载所有3个项目以及另一个空用户控件,并最终保存所有这4个.
我尝试了所有方法,但是无法以编辑模式保存所有记录.我可以拿一个演示样本来帮助我了解当前情况吗?

预先感谢.

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.

推荐答案

您好,

您可以使用列表视图或转发器.

Hello,

You can use a listview or a repeater.

<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>



在后面的代码中,我们将通过页面加载方法将数据源分配给listview,然后再处理listview的ItemDataBound事件,以填充customcontrol的数据.



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

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);
    }
}


这篇关于面板内部动态创建的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆