从代码后面添加用户控件 [英] Adding User Controls from code behind

查看:96
本文介绍了从代码后面添加用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有一个用户控件( UserControl1 )和一个添加更多按钮。单击Add More按钮再次添加相同的用户控件。当我点击用户控件第一次,它添加第二个用户控件,但第二次它不添加其他。我认为控件丢失。
我在链接按钮点击时添加这样的控件: -

I have a user control(UserControl1) on my page and one Add more button.Clicking on Add More Button add the same user control again.When i click on user control first time,it adds second User Control,but on second time it doesnot add other.I think the control gets lost. I am adding a control like this on link button click:-

protected void lnkadd_Click(object sender, EventArgs e)
{
    HtmlTableCell tCell = new HtmlTableCell();
    UserControl uc = (UserControl)Page.LoadControl("~/Controls/DirectionalPricingCtrl.ascx");
    tCell.Controls.Add(uc);
    tablerow.Cells.Add(tCell);
}

我认为我做错了,我应该添加用户控件页面生命周期,但如何?
有人可以指导我或提供一些有用的链接吗?
当我每次点击添加按钮然后再添加用户控件时,我应该遵循什么方法稍后检索所有值并将其保存到DB?

I think i am doing something wrong and i should add user controls in respect of page life cycle,but how? Can somebody please guide me or provide some useful links? What approach i should follow when i have to add a user control each time when i click on a Add button and then later retrieve all the values and save it to DB?

推荐答案

我更喜欢javascript / ajax解决方案,但我认为有您的代码没有任何问题。

I prefer javascript / ajax solution but I think there is nothing wrong with your code.

我做了一个小例子。这是与你一样的解决方案。优点是只在点击的情况下加载控件。

I did small example. Here is same solution as you have. Advantage is that control is loaded only in case of click.

public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLink_Click(object sender, EventArgs e)
        {
            var uc = (UserControl)Page.LoadControl("~/WebUserControl1.ascx");
            pnl.Controls.Add(uc);            
        }
    }

以下是在Page_Load事件中加载用户控件的示例并且在单击(btnLink_Click)的情况下,用户控件被添加到面板。它与您的解决方案相同,但即使不需要,也可以加载用户控件(在内存中处理而不是处理)。

Here is example where user control is loaded in Page_Load event and in case of click (btnLink_Click) user control is added to panel. It works same as your solution but user control can be loaded (processed in memory not redered) even if is not needed.

public partial class Default : System.Web.UI.Page
    {
        UserControl uc; 
        protected void Page_Load(object sender, EventArgs e)
        {
           if(IsPostBack) // This condition is not needed but we know that click is always postback
            uc = (UserControl)Page.LoadControl("~/WebUserControl1.ascx");
        }

        protected void btnLink_Click(object sender, EventArgs e)
        {

            pnl.Controls.Add(uc);            
        }
    }

这是我更喜欢的解决方案,它基于可见属性。如果用户控件不可见,则不会将其呈现为输出。当然,如果桌子上有很多单元格作为对照容器而不是面板,那么它不是很实用。

Here is solution which I prefer, it's based on visible property. In case that user control is not visible it's not rendered to output. Sure it's not very practival in case of table with lot of cells as a control container instead of panel.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<%@ Register Src="~/WebUserControl1.ascx" TagName="ucrCtrl" TagPrefix="ctr"  %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="btnLink" runat="server" Text="Add" OnClick="btnLink_Click" />
        <asp:Panel runat="server" ID="pnl">
            <ctr:ucrCtrl runat="server" ID="usrCtrl" Visible="false" />
        </asp:Panel>
    </div>
    </form>
</body>
</html>

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLink_Click(object sender, EventArgs e)
        {
            usrCtrl.Visible = true;     
        }
    }
}

这篇关于从代码后面添加用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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