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 页面中调用它,但第二次单击链接按钮时,它没有第二次填充用户控件。我是编码新手,请帮助:(

4

2 回答 2

0

我认为的问题是,当控件加载到 ViewState 时,您在回发过程中丢失了控件。

这是一个示例,您可以在其中将控件加载到会话中,这不是很好的做法,但它可以满足您的目的:

添加MultiLocPage.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();
    }
}

用户控件标记

<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>
于 2013-01-08T02:51:11.033 回答
-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。

于 2013-01-08T05:41:18.783 回答