Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
4.75/5 (4 votes)
Hi all,

I am trying to add Controls inside ModalPopupExtender dynamically.

The Controls has been added but my problem is the EventHandler is not calling or it is not properly handling. The AutoPostBack is working fine but the EventHandler not.

Note: I have made the same function in normal page and it is works very well but as soon as I added it to the ModalPopupExtender obj it didn't work with me.

Please give me a suggestion

Thanks
About My Code:
On The Event Handler Of Specific Button i call This Function Which CreateThe PopUpModale:
C#
/*---Function To Display The Message In PopUp Modal---*/
private void DisplayMessage2()
{
    AjaxControlToolkit.ModalPopupExtender modalPop2 = new AjaxControlToolkit.ModalPopupExtender();
    modalPop2.ID = "popUp2";
    modalPop2.PopupControlID = "Panel1";
    modalPop2.DropShadow = true;
    modalPop2.BackgroundCssClass = "modalBackground2";
    modalPop2.CancelControlID = "ImageButtonCancel";
    modalPop2.TargetControlID = "LinkButtonBack";
    this.DivTreeView.Controls.Add(modalPop2);
    modalPop2.Show();
}

And Inside The Panel Call This Function:

C#
/*--- Function To Draw The Html Tags On Run Time---*/
private void DrawHtmlTable(string AccountNumberValue, string AccountNameValue, string AccountTypeValue)
{
    TopPaneDiv4.Attributes.Add("class", "topic_rowSearch");
    HtmlGenericControl TopPaneDiv = new HtmlGenericControl("div");
    TopPaneDiv.Attributes.Add("class", "header_AccountNameFirst_row");
    TopPaneDiv.InnerHtml = AccountNumberValue;
    TopPaneDiv4.Controls.Add(TopPaneDiv);
    HtmlGenericControl TopPaneDiv2 = new HtmlGenericControl("div");
    TopPaneDiv2.Attributes.Add("class", "header_AccountName_row");
    TopPaneDiv2.InnerHtml = AccountNameValue;
    TopPaneDiv4.Controls.Add(TopPaneDiv2);
    HtmlGenericControl TopPaneDiv3 = new HtmlGenericControl("div");
    TopPaneDiv3.Attributes.Add("class", "header_AccountName_row2");
    TopPaneDiv3.InnerHtml = AccountTypeValue;
    TopPaneDiv4.Controls.Add(TopPaneDiv3);
    CreateDynamicButton();
    topics_rows.Controls.Add(TopPaneDiv4);
}
private void CreateDynamicButton()
{
    Button dynamicButton = new Button();
    dynamicButton.Height = 40;
    dynamicButton.Width = 20;
    dynamicButton.Text = "I am Dynamic Button";
    dynamicButton.Text = "I am Dynamic Button";
    dynamicButton.Click += new EventHandler(DynamicButton_Click);
    HtmlGenericControl TopPaneDiv3 = new HtmlGenericControl("div");
    TopPaneDiv3.Attributes.Add("class", "header_Level_float_rowLast2");
    TopPaneDiv3.Controls.Add(dynamicButton);
    TopPaneDiv4.Controls.Add(TopPaneDiv3);
}
private void DynamicButton_Click(object sender, EventArgs e)
{

}
Posted
Updated 28-Feb-11 21:32pm
v4
Comments
Monjurul Habib 28-Feb-11 15:41pm    
Share you code to help you out.
coolestCoder 1-Mar-11 0:15am    
Does it has something to do with UpdatePanel ? I am not sure since you have not shared enough details.
[no name] 1-Mar-11 1:32am    
Can you share your code to get clear idea about it...

Hi,

I am giving an example of controls add to the popup and assign to the Modalpop up extender.

In my aspx page I am simply having

XML
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:ScriptManager runat="server" ID="script1"></asp:ScriptManager>
    <asp:ModalPopupExtender ID="Panel1_ModalPopupExtender" runat="server"
        DynamicServicePath="" Enabled="True" TargetControlID="Button6"   >
    </asp:ModalPopupExtender>
    <asp:Button ID="Button6" runat="server" Text="ShowPopup" />
    <asp:Panel runat="server" ID="Panel2"></asp:Panel>

</asp:Content>


The popup control, ok button, cancel button and all added and assigned at runtime from code behind.
C#
protected void Page_Load(object sender, EventArgs e)
{

    Panel Panel1 = new Panel();
    Panel1.ID = "Panel1";

    TextBox text1 = new TextBox();

    Button button1 = new Button();
    button1.Text = "OK";
    Button button2 = new Button();
    button2.Text = "Cancel";
    Button button3 = new Button();
    button3.Text = "Submit";

    Panel2.Controls.Add(Panel1);
    Panel1.Controls.Add(text1);
    Panel1.Controls.Add(button1);
    Panel1.Controls.Add(button2);
    Panel2.Controls.Add(button3);


    Panel1_ModalPopupExtender.PopupControlID = Panel1.ID;
    Panel1_ModalPopupExtender.OkControlID = button1.ID;
    Panel1_ModalPopupExtender.CancelControlID = button2.ID;

    button3.Click += new EventHandler(SubmitButtonClick);

}


protected void SubmitButtonClick(object sender, EventArgs e)
{
    //on submit button click this function will be called
}


These controls can be added and assigned in any event, but the popupcontrolid for the extender should be assigned on page load event.


In your case...

The dynamic button is add on a button click event. Button click event is firing after page load event. After page loaded event handler can't be assigned. So you need to have the button on page load event. Also if you assign the button to a div control which created after page load (the Div is only render, it will not be available for instantiation on post back), then the button instance will not be available on submit. So handler will not work.


You can do this way.

Have the dynamic button as a global to the form. Then in page load..
C#
Button dynamicButton;
protected void Page_Load(object sender, EventArgs e)
{
    dynamicButton = new Button();
    dynamicButton.Style.Add("Visibility","Hidden");
    dynamicButton.ClientIDMode = ClientIDMode.AutoID;
    dynamicButton.Height = 40;
    dynamicButton.Width = 200;
    dynamicButton.Text = "I am Dynamic Button";
    dynamicButton.Click += new EventHandler(DynamicButton_Click);
    Panel2.Controls.Add(dynamicButton);

}

This panel2 has to be next to the topic_rows where you are adding the TopPaneDiv4 . So visibly it shows next to the TopPaneDiv4.

Next have your Create Dynamic function should be like ...

C#
private void CreateDynamicButton()
{
    dynamicButton.Style.Add("Visibility", "Visible");
    HtmlGenericControl TopPaneDiv3 = new HtmlGenericControl("div");
    TopPaneDiv3.Attributes.Add("class", "header_Level_float_rowLast2");
    TopPaneDiv4.Controls.Add(TopPaneDiv3);
}


So you are not dynamically adding the button at button click. But dynamically adding it on page load and make it hide and visible as per the need.

Hope this helps.
 
Share this answer
 
v2
Comments
moral_2011 1-Mar-11 2:03am    
First Thanks Alot For Your Reply,

I Have Shared My Code May Be It is help,

Thanks
Albin Abel 1-Mar-11 3:57am    
Are you saying your code helps me??
You can't assign me a homework. Any how I have few questions.

1) When this DrawHtmlTable function called. I mean in which event.
2) Why toppane3 created two times and added two times. One time in DrawHtml function and again in create dynamic button function

Also my advice is use server controls instead of html controls when you are creating controls from code behind. Html controls are to use on the client side with intensive javascript interaction.
Albin Abel 1-Mar-11 4:34am    
No worries. I am reviewing your code. Get back to you in minutes
Albin Abel 1-Mar-11 5:35am    
I improved the answer as per your questions. Look at the last segment of my answer
moral_2011 1-Mar-11 5:57am    
Thank you so much
Hi,

The solution is very simple just use the concept of delegates.
 
Share this answer
 
Comments
Albin Abel 1-Mar-11 5:44am    
He is creating the button after the page loaded. How the instance will be preserved to call a handler on postback?. Noting to do with delegates. Adding an event handler is nothing but assigning a delegate only.

He has to create the button on page load to have the server control instance recerated on post back or he has to have a custom static button which lives still the server shuts down
conetik 9-Dec-11 6:47am    
hi ashok vyas is here
1.when i make a div and link dynamically runtime from useing javascript on load event then control make out side of aspx content page i.e it is on master page i want to make in content page
2.how can i make panel runtime dynamically useing javascript
plz rply on my email ashok.vyas786@gmail.com
1.when i make a div and link dynamically runtime from useing javascript on load event then control make out side of aspx content page i.e it is on master page i want to make in content page
2.how can i make panel runtime dynamically useing javascript
plz rply on my email ashok.vyas786@gmail.com
 
Share this answer
 
Comments
conetik 9-Dec-11 6:47am    
1.when i make a div and link dynamically runtime from useing javascript on load event then control make out side of aspx content page i.e it is on master page i want to make in content page
2.how can i make panel runtime dynamically useing javascript
plz rply on my email ashok.vyas786@gmail.com

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900