【问题标题】:Dynamically add user control in page ON button click whic is also in user control (ModalPopExtender in user control)在页面 ON 按钮单击中动态添加用户控件,该按钮也在用户控件中(用户控件中的 ModalPopupExtender)
【发布时间】:2012-10-25 17:15:58
【问题描述】:

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

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

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

控制代码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();
}

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-23
        • 2019-04-24
        • 2017-08-03
        • 1970-01-01
        • 2018-10-31
        • 1970-01-01
        相关资源
        最近更新 更多