Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8783327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:48:15+00:00 2026-06-13T20:48:15+00:00

I have a simple user control which I want to create dynamically every time

  • 0

I have a simple user control which I want to create dynamically every time user click button (button also saves data in db.) This is just a kind of Discussion forum where user reply and it appears beneath message.

When user click on Reply button a ModalPopupExtender window pops up and user enter its reply and submit.

When I click on submit in popup data get saved in db but OnInit execute before ButtonClick, how to create new control and add in controls in this scenario.

enter image description here

enter image description here

Control Code 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>

User control .cs (button click just save message into db)

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:48:17+00:00Added an answer on June 13, 2026 at 8:48 pm

    When you working with dynamically added controls you need to re-create in Page_Init handler all previously added controls. But for new control it’s ok to add it just in postback control event handler. Try to call AddControlInPlaceHolder method right in btnSubmit click handler. If you need to have database message’s id int that method, consider to update message id in Save method with new value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a simple jQuery.ajax function which loads a user control from the
I have just started to create a user control realising that I want a
This is driving me crazy. I have a very simple user control: public int?
I have a simple User model which is associated to many Town objects using
I have a WinForms control on which I want to display two things: An
I have a class which acts as a simple crawler and I want to
I've created a simple user control which is manually created with something like MyUserControl
i want to control home button in android for my application only...actually i have
I have a simple UserControl containing a ComboBox which is empty at first. The
I have a simple user production report where daily quotas are tracked. The sql

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.