Saturday, March 24, 2012

Modal Popup shown via server in code behind....

Okay, sothis page says that I can show a modal popup via server in the code behind (I'm using partial rendering), but I am just not seeing how via C#.

I have a button (regular button) that is used as the TargetControlID, and even though I have a click event for the button, it completely ignores it. What is the problem?

For a bit of more info: I have attempted using a separate button and calling the .Show() function within the Click event. (e.g. m_editPopup.Show(); isn't working). It just does the postback without it ever showing.

are you adding the button/modal popup programatically in the code behind, or did you drag them into the designer and link it up that way?. I have no problem making my modal popup appear when I click the target button, im just not sure how exactally you code is set up.


Here I'll give you a portion of my code. Don't worry about my controlID's, it's wonky due to the way we have our modules set up, but it works.

#region Button Setup
m_hiddenAdd.Style.Add("display", "none");
m_hiddenAdd.ID = "hiddenPopupButton"; // Used so we can have the nice postback enabling for data updates.

m_add.Text = "Add";
m_add.ID = "addDemographic";
m_add.Click += new EventHandler(m_add_Click);

m_edit.Text = "Edit";
m_edit.ID = "editDemographic";

m_delete.Text = "Delete";
m_delete.OnClientClick = "alert('Are you sure you want to delete this demographic?'); return false;";

m_delete.ID = "deleteDemographic";

m_buttonDiv.Style.Add("float", "right");

m_buttonDiv.Controls.Add(m_hiddenAdd);
m_buttonDiv.Controls.Add(m_add);
m_buttonDiv.Controls.Add(new LiteralControl(" "));
m_buttonDiv.Controls.Add(m_edit);
m_buttonDiv.Controls.Add(new LiteralControl(" "));
m_buttonDiv.Controls.Add(m_delete);
#endregion

#region Add Popup Setup
m_demoAdd.Style.Add("width", "375px");
m_demoAdd.Style.Add("display", "none");
m_demoAdd.okButtonID = "okayEdit";
m_demoAdd.cancelButtonID = "cancelAdd";
m_demoAdd.ID = "studentAdd";

m_addPopup.BackgroundCssClass = "popUpShadow";
m_addPopup.TargetControlID = m_hiddenAdd.ClientID;
m_addPopup.PopupControlID = m_demoAdd.ClientID;
m_addPopup.OkControlID = this.ClientID + "_" + m_demoAdd.ClientID + "_buttonDiv_" + m_demoAdd.okButtonID;
m_addPopup.CancelControlID = this.ClientID + "_" + m_demoAdd.ClientID + "_buttonDiv_" + m_demoAdd.cancelButtonID;
#endregion

#region Edit Popup Setup
m_demoEdit.Style.Add("width", "375px");
m_demoEdit.Style.Add("display", "none");
m_demoEdit.okButtonID = "okayEdit";
m_demoEdit.cancelButtonID = "cancelEdit";
m_demoEdit.ID = "studentEdit";

m_editPopup.BackgroundCssClass = "popUpShadow";
m_editPopup.TargetControlID = m_edit.ClientID;
m_editPopup.PopupControlID = m_demoEdit.ClientID;
//m_editPopup.OkControlID = this.ClientID + "_" + m_demoEdit.ClientID + "_buttonDiv_" + m_demoEdit.okButtonID;
m_editPopup.CancelControlID = this.ClientID + "_" + m_demoEdit.ClientID + "_buttonDiv_" + m_demoEdit.cancelButtonID;
#endregion

this.Controls.Add(this.generateStudentDemographicTable());
this.Controls.Add(new HtmlBreak());
this.Controls.Add(m_buttonDiv);
this.Controls.Add(new HtmlBreak());
this.Controls.Add(m_demoAdd);
this.Controls.Add(m_addPopup);
this.Controls.Add(m_demoEdit);
this.Controls.Add(m_editPopup);

}

private void m_add_Click(object sender, EventArgs e)
{
m_addPopup.Show();
}

Nothing is EVER done in designer. The "targetcontrolID" is set to a hidden button, because I don't want to show it that way. I want to populate information from my server, and then display it. What you see is what SHOULD be working to just display the information, however it doesn't do so. All objects you see here are private.


As it turns out, it seems that my .Click event is not getting called, and I have absolutely no idea why that is. However, that is what is causing the problem.


try adding a new event handler in C# for the buttons .Click event and link it to your click function.

myButton.Click +=newEventHandler(this.myButton_click);private void myButton_click(object sender, EventArgs e){ modalPopup.Show();}

No, no, no, no. That's not it. I have that all said and done. As you can see through the code above. I registered a class for the event, and I have the .Show() function in there. The problem is the event is just not getting called.


oh sorry, i didnt see the code. Here is a copy of code i just wrote to do what i think you want to do.

using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using AjaxControlToolkit;public partialclass administration_OIS_Default : System.Web.UI.Page{ ModalPopupExtender modPop =new ModalPopupExtender();protected void Page_Load(object sender, EventArgs e) { UpdatePanel update =new UpdatePanel(); Button btnVisible =new Button(); TextBox tempText =new TextBox(); HiddenField hidden =new HiddenField(); hidden.ID ="tesss"; update.UpdateMode = UpdatePanelUpdateMode.Conditional; update.ChildrenAsTriggers =true; update.ContentTemplateContainer.Controls.Add(hidden); btnVisible.Text ="Click to show modPop"; btnVisible.ID ="visibleButton"; btnVisible.Click +=new EventHandler(this.btnVisible_Click); update.ContentTemplateContainer.Controls.Add(btnVisible); tempText.Text ="Testing Testing"; tempText.ID ="textBoxTest"; update.ContentTemplateContainer.Controls.Add(tempText); modPop.TargetControlID = hidden.ID; modPop.PopupControlID = tempText.ID; modPop.DropShadow =true; modPop.X = 500; modPop.Y = 500; update.ContentTemplateContainer.Controls.Add(modPop); form1.Controls.Add(update); }private void btnVisible_Click(object sender, EventArgs e) { modPop.Show(); }}

Its hard to tell because I didnt see variable declarations in the code you posted, but i think you were using a button for the targetID. if you want to have a hidden target id that cannot be seen use a HiddenField type variable instead of putting a button in with visible set to false (once agian not sure if thats what you did or not but hidden field works)

hope this helps


Hard to describe without giving away too much code that we do. However, the code you was was for a .cs file that is a module (all of our modules are UpdatePanels). for a web page. The problem, I find, is that the function I have created for the Click event (m_add_Click) is not getting called when I click the button.

EDIT: Problem solved! Turns out it's because I had the object within an HtmlDiv object (which is a custom made object that we created to emulated a div object. How odd...


Hi

I read all your above posts on adding a modal popup from server side nevertheless I fail to make it work.

I am having problems while adding the modalpopup dynamically to my page on the server side on a (web usercontrol). When I set the "TargetControlId" or the "PopupControlId" likethis -

ModalPopupExtender modPop = new ModalPopupExtender();
TextBox tbMsg = new TextBox();
tbMsg.ID = "tbMsg";
tbMsg.Text = "Thanks!";
modPop.ID = "modulePopup";
modPop.TargetControlID = hiddenField.ID; //made this hidden field in the user control
modPop.PopupControlID = tbMsg.ClientID;
modPop.BackgroundCssClass = "modalBackground";
modPop.X = 200;
modPop.Y = 200;

Nowwhat is happening is that the ID's that go to the modal popupjavascript (ScriptResource etc) I see that it goes in as the name ofthe textbox itself "tbmsg" instead of the client ID ? so I seesomething like document.getElementByID("tbMsg") !!!

What am I supposed to do??

Thanks in advance

Sanchita


You should set the PopulControlID to be the server ID of the popup not the ClientID.

No comments:

Post a Comment