我有一个简单的用户控件,我想在每次用户单击按钮时动态创建(按钮也将数据保存在数据库中。)这只是一种用户回复的论坛,它出现在消息下方。
当用户单击回复按钮时,会弹出一个 ModalPopupExtender 窗口,用户输入其回复并提交。
当我在弹出数据中单击提交时,数据保存在 db 中,但 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);
}