在页面ON动态添加用户控件按钮单击也是用户控件(用户控件中的ModalPopupExtender)

时间:2012-11-05 19:58:36

标签: asp.net user-controls postback dynamically-generated

我有一个简单的用户控件,我想在每次用户点击按钮时动态创建(按钮也会在数据库中保存数据。)这只是一种讨论论坛,用户回复并显示在消息下方。

当用户点击“回复”按钮时,会弹出ModalPopupExtender窗口,用户输入回复并提交。

当我点击提交弹出数据时,保存在数据库中但OnInit在ButtonClick之前执行,如何在这种情况下创建新控件并添加控件。

enter image description here

enter image description here

控制代码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(按钮单击只将消息保存到数据库中)

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

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

2 个答案:

答案 0 :(得分:1)

使用动态添加的控件时,需要在Page_Init处理程序中重新创建以前添加的所有控件。但是对于新控件,可以在回发控件事件处理程序中添加它。尝试在AddControlInPlaceHolder点击处理程序中调用btnSubmit方法。如果您需要在该方法中使用数据库消息的id int,请考虑使用新值更新Save方法中的消息ID。

答案 1 :(得分:1)

我们需要在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;
    }
}
相关问题