按钮单击动态加载用户控件

时间:2013-01-08 02:03:45

标签: asp.net html5 c#-4.0

我有一个标签和一个文本框,我已在用户控件中添加。

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AddMultiLoc.ascx.cs" Inherits="CRM_Streamline_Forms.UserControls.AddMultiLoc" %>
<table>
<tr>
    <td style="width:25%">
        <asp:Label ID="lblLocName_UC_G0138" runat="server" Text="Location Name:" />
    </td>
    <td style="width:25%">
        <asp:TextBox ID="txtLocName_UC_G0138" runat="server" Width="200px" />
    </td>
    <td style="width:25%">
        <asp:Label ID="lblLocID_UC_G0138" runat="server" Text="Location ID:" />
    </td>
    <td style="width:25%">
        <asp:TextBox ID="txtLocID_UC_G0138" runat="server" Width="200px" />
    </td>
</tr>

我的某个aspx页面中有一个链接按钮,点击该按钮时会填充此用户控件。

<asp:LinkButton ID="lnkAddLoc_AGBI2_G0138" runat="server" Text="+ Add Another Location" onclick="lnkAddLoc_AGBI2_G0138_Click" />

代码落后,我已经为按钮点击编写了这段代码:

protected void lnkAddLoc_AGBI2_G0138_Click(object sender, EventArgs e)
    {
        AddMultiLoc con = (AddMultiLoc)LoadControl("~/UserControls/AddMultiLoc.ascx");
        pnlMultiInvoiceInfo1_AGBI2_G0138.Controls.Add(con);
        Panel p = new Panel();
        Control uc = (Control)Page.LoadControl("~/UserControls/AddMultiLoc.ascx");
                    p.Controls.Add(uc);
                    p.Width = 200;
                    p.Height = 100;
                    pnlMultiInvoiceInfo1_AGBI2_G0138.Controls.Add(p);
    }

首次将用户控件填充为在aspx页面中调用它,但第二次点击链接按钮时,第二次没有填充用户控件。我是编码的新手,请帮忙:(

2 个答案:

答案 0 :(得分:0)

我认为问题是当控件加载到ViewState时,你会在回发时丢失控件。

下面是一个示例,您可以将控件加载到Session中,这不是很好的练习,但它可以用于您的目的:

<强> AddMultiLocPage.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:LinkButton ID="lnkAddLoc_AGBI2_G0138" runat="server" Text="+ Add Another Location" OnClick="lnkAddLoc_AGBI2_G0138_Click" />
        </div>
        <asp:PlaceHolder runat="server" ID="Placeholder1"></asp:PlaceHolder>
    </form>
</body>
</html>

<强> AddMultiLocPage.aspx.cs

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

    private List<AddMultiLoc> ViewStateControls
    {
        get { return (List<AddMultiLoc>) Session["ViewStateControls"]; }
        set { Session["ViewStateControls"] = (List<AddMultiLoc>)value; }
    }

    private void LoadControlsFromSession()
    {
        if (ViewStateControls != null)
        {
            Placeholder1.Controls.Clear();

            foreach (var c in ViewStateControls)
            {
                Placeholder1.Controls.Add(c);
            }
        }
    }

    protected void lnkAddLoc_AGBI2_G0138_Click(object sender, EventArgs e)
    {
        var con = (AddMultiLoc)LoadControl("~/AddMultiLoc.ascx");
        con.ID = Guid.NewGuid().ToString();

        List<AddMultiLoc> tmpList = ViewStateControls;
        if(tmpList == null) tmpList = new List<AddMultiLoc>();
        tmpList.Add(con);
        ViewStateControls = tmpList;

        LoadControlsFromSession();
    }
}

UserControl Markup

<table>
    <tr>
        <td style="width: 25%">
            <asp:Label ID="lblLocName_UC_G0138" runat="server" Text="Location Name:" />
        </td>
        <td style="width: 25%">
            <asp:TextBox ID="txtLocName_UC_G0138" runat="server" Width="200px" />
        </td>
        <td style="width: 25%">
            <asp:Label ID="lblLocID_UC_G0138" runat="server" Text="Location ID:" />
        </td>
        <td style="width: 25%">
            <asp:TextBox ID="txtLocID_UC_G0138" runat="server" Width="200px" />
        </td>
    </tr>
</table>

答案 1 :(得分:-2)

无需在会话中存储控件,解决您的问题的方法是:

ASPX代码:

< asp:LinkButton ID="lnkAddLoc_AGBI2_G0138" runat="server" Text="+ Add Another Location" onclick="lnkAddLoc_AGBI2_G0138_Click" />
    <asp:Panel ID="Panel1" runat="server">

代码背后:

protected void lnkAddLoc_AGBI2_G0138_Click(object sender, EventArgs e)
    {
        Control uc = (Control)Page.LoadControl("~/AddMultiLoc.ascx");
        Panel1.Controls.Add(uc);
    }

每当您点击链接按钮lnkAddLoc_AGBI2_G0138时,它都会向Panel1添加新的用户控件实例。