动态地添加用户控件在ON按钮,点击页面whic也是用户控件(ModalPopExtender在用户控

2019-10-17 08:53发布

我有我要动态地创建每次用户点击按钮(按钮也是在DB保存数据。)这只是一种讨论论坛,用户回复,看来消息下面的一个简单的用户控制。

当回复按钮,用户点击一个ModalPopupExtender窗口弹出,用户输入其答复并提交。

当我点击中得到保存在数据库弹出数据提交,但ButtonClick之前的OnInit执行,如何创建新的控制,并在此方案中添加控件。

控制代码ASPX

<div style="border: .25em solid #000; margin: 10px; padding: 10px">
    <div>
        <asp:Label ID="lblMessage" runat="server" Width="200px"></asp:Label>
    </div>
    <div>
        <asp:Button ID="btnReply" runat="server" Text="Reply" />
    </div>
</div>
<table>
    <tr>
        <td>
            <asp:ModalPopupExtender ID="mpeReply" runat="server" TargetControlID="btnReply" PopupControlID="pnlReply"
                BackgroundCssClass="ModalPopupBG1">
            </asp:ModalPopupExtender>
            <asp:Panel ID="pnlReply" runat="server">
                <div class="popupbody">
                    <table width="90%">
                        <tr>
                            <td>
                                <asp:TextBox ID="txtReply" runat="server" Width="400px" CssClass="input1" TextMode="MultiLine"
                                    ValidationGroup="ReplyPopup"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="ReplyPopup"
                                    OnClick="btnSubmit_Click" />
                                <asp:Button ID="btnClose" runat="server" Text="Cancel" CausesValidation="false" />
                            </td>
                        </tr>
                    </table>
                </div>
            </asp:Panel>
        </td>
    </tr>
</table>

用户控制的.cs(按一下按钮只保存消息为DB)

protected void btnSubmit_Click(object sender, EventArgs e)
{
    message.Save();
}

页面的.cs

protected void Page_Init(object sender, EventArgs e)
{
    LoadControl();
}

protected void Page_Load(object sender, EventArgs e)
{
}

private void LoadControl()
{
    divMessageControl.Controls.Clear();

    MessageCollection msgs = MessageCollection.LoadAll();
    foreach (Message msg in msgs)
    {
        AddControlInPlaceHolder(msg);
    }
}

private void AddControlInPlaceHolder(Message msg)
{
    Control myUserControl = (Control)Page.LoadControl("~/Controls/MessageControl.ascx");

    Label lblMessage = myUserControl.FindControl("lblMessage") as Label;
    if (lblMessage != null)
    {
        lblMessage.Text = msg.MsgTxt;
    }

    divMessageControl.Controls.Add(myUserControl);
}

Answer 1:

当您使用动态添加控件工作,你需要在Page_Init处理程序来重新创建所有以前添加控件。 但是,对于新的控制它的确定它添加只是回传控制的事件处理程序。 尝试调用AddControlInPlaceHolder法权btnSubmit单击处理程序。 如果你需要有数据库信息的id int值的方法,考虑保存方法与新的值更新消息ID。



Answer 2:

我们需要重新创建的Page_Load控制。 完整的例子:

public partial class _Default : Page
{
    public int n;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (ViewState["Controls"] != null)
            n = (int)ViewState["Controls"];
        if (!Page.IsPostBack)
            n = 0;
        for (int i = 0; i <= n; i++)
        {
            TestUserControl uc = LoadControl("~/TestUserControl.ascx") as TestUserControl;
            uc.ID = "TestControl" + i.ToString();
            PlaceHolder1.Controls.Add(uc);
        }

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        TestUserControl uc = LoadControl("~/TestUserControl.ascx") as TestUserControl;
        uc.ID = "TestControl" + n++.ToString();
        PlaceHolder1.Controls.Add(uc);
        ViewState["Controls"] = n;
    }
}


文章来源: Dynamically add user control in page ON button click whic is also in user control (ModalPopExtender in user control)