我有我要动态地创建每次用户点击按钮(按钮也是在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);
}