Member_2_3703398
asked on
+= new System.EventHandler Not Firing
Hi,
I have created a Master web page with a ContentPlaceHolder with some Panels and Views and a PlaceHolder contained within the ContentPlaceHolder.
What should happen is that the user will click a button, in this instance a delete button which then calls an external class contained in the App_Code directory. The called class then dynamically creates some controls in a panel and displays the panel with a message and OK & Cancel buttons.
It is the code/events for the OK & Cancel buttons that never fire. This is driving me nuts so a helping hand will be greatly appreciated.
Thank you.
I have created a Master web page with a ContentPlaceHolder with some Panels and Views and a PlaceHolder contained within the ContentPlaceHolder.
What should happen is that the user will click a button, in this instance a delete button which then calls an external class contained in the App_Code directory. The called class then dynamically creates some controls in a panel and displays the panel with a message and OK & Cancel buttons.
It is the code/events for the OK & Cancel buttons that never fire. This is driving me nuts so a helping hand will be greatly appreciated.
Thank you.
using System;
using System.Data;
using System.Configuration;
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;
/// <summary>
/// Display Panel with Custom Controls
/// </summary>
public class pnlBox
{
public void showPanelBox()
{
//setup table
Table myTable = new Table();
myTable.BorderStyle = BorderStyle.None;
myTable.CellPadding = 4;
myTable.CellSpacing = 2;
myTable.Width = Unit.Parse("100%");
TableRow myTR1 = new TableRow();
TableCell myTD1 = new TableCell();
TableCell myTD2 = new TableCell();
TableRow myTR2 = new TableRow();
TableCell myTD3 = new TableCell();
TableCell myTD4 = new TableCell();
TableCell myTD5 = new TableCell();
TableRow myTR3 = new TableRow();
TableCell myTD6 = new TableCell();
TableCell myTD7 = new TableCell();
TableCell myTD8 = new TableCell();
myTable.Controls.Add(myTR1);
myTR1.Controls.Add(myTD1);
myTR1.Controls.Add(myTD2);
myTD2.ColumnSpan = 2;
myTable.Controls.Add(myTR2);
myTR2.Controls.Add(myTD3);
myTR2.Controls.Add(myTD4);
myTR2.Controls.Add(myTD5);
myTable.Controls.Add(myTR3);
myTR3.Controls.Add(myTD6);
myTR3.Controls.Add(myTD7);
myTR3.Controls.Add(myTD8);
//setup image
Image myImage = new Image();
myImage.ImageUrl = "images/symbols/question.gif";
myTD1.Controls.Add(myImage);
//setup display text
Label myLabel = new Label();
myLabel.CssClass = "messageFont";
myLabel.Text = "You are about to delete a record.<br />Do you wish to continue?";
myTD2.Controls.Add(myLabel);
//setup blank line
myTR2.Height = 10;
//setup OK button
Button okButton = new Button();
okButton.Text = "OK";
okButton.CssClass = "buttonFont";
okButton.Width = 120;
okButton.CausesValidation = false;
okButton.Click += new System.EventHandler(okButton_Click);
myTD7.Controls.Add(okButton);
//setup cancel button
Button cancelButton = new Button();
cancelButton.Text = "Cancel";
cancelButton.CssClass = "buttonFont";
cancelButton.Width = 120;
cancelButton.CausesValidation = false;
cancelButton.Click += new System.EventHandler(cancelButton_Click);
myTD8.Controls.Add(cancelButton);
//setup panel
Panel myPanel = new Panel();
myPanel.CssClass = "panelDeleteConfirm";
myPanel.Controls.Add(myTable);
//point to current page
Page myPage = HttpContext.Current.Handler as Page;
PlaceHolder myPlaceHolder = (PlaceHolder)myPage.Master.FindControl("PlaceHolderPopUp");
myPlaceHolder.Controls.Add(myPanel);
}
public void okButton_Click(object sender, EventArgs e)
{
throw new System.Exception("It worked! - OK");
}
public void cancelButton_Click(object sender, EventArgs e)
{
throw new System.Exception("It worked! - Cancel");
}
}
please post the code where you create your panels... dynamic controls must be created/recreated in the page's Init event, otherwise you'll run into this kind of problem with events not firing and viewstates getting lost and so on...
by create panels i mean code which calls the showPanelBox method
ASKER
Hi,
This is the code which is called from customers.aspx.cs:
protected void ButtonDelDeleteAddress_Cli ck(object sender, EventArgs e)
{
//display delete prompt for items pending save
pnlBox myBox = new pnlBox();
myBox.showPanelBox();
}
I have included the full code snippet for you as well. I am happy to send you a zip file but I do not think that this is permitted as an upload.
Thanks.
This is the code which is called from customers.aspx.cs:
protected void ButtonDelDeleteAddress_Cli
{
//display delete prompt for items pending save
pnlBox myBox = new pnlBox();
myBox.showPanelBox();
}
I have included the full code snippet for you as well. I am happy to send you a zip file but I do not think that this is permitted as an upload.
Thanks.
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 System.Drawing;
public partial class customers : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//get object reference for updating page heading
Label myLabel = (Label)Master.FindControl("LabelStatus");
myLabel.Text = "Customer Records";
//disable main menu
Menu myMasterMenu = (Menu)Master.FindControl("MenuMain");
myMasterMenu.Enabled = false;
//set the default view to detail view
myTableCellDetails.BackColor = System.Drawing.ColorTranslator.FromHtml("#e6e6e6");
LinkButtonDetails.ControlStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#ffa500");
MultiViewCustomers.ActiveViewIndex = 0;
}
else
{
//do something else
}
}
protected void LinkButtonDetails_Click(object sender, EventArgs e)
{
//setup account details menu tabs
myTableCellDetails.BackColor = System.Drawing.ColorTranslator.FromHtml("#e6e6e6");
LinkButtonDetails.ControlStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#ffa500");
myTableCellContacts.BackColor = System.Drawing.ColorTranslator.FromHtml("#ffffff");
LinkButtonContacts.ControlStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#cc6633");
myTableCellNotes.BackColor = System.Drawing.ColorTranslator.FromHtml("#ffffff");
LinkButtonNotes.ControlStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#cc6633");
MultiViewCustomers.ActiveViewIndex = 0;
}
protected void LinkButtonContacts_Click(object sender, EventArgs e)
{
//setup account contacts menu tabs
myTableCellDetails.BackColor = System.Drawing.ColorTranslator.FromHtml("#ffffff");
LinkButtonDetails.ControlStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#cc6633");
myTableCellContacts.BackColor = System.Drawing.ColorTranslator.FromHtml("#e6e6e6");
LinkButtonContacts.ControlStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#ffa500");
myTableCellNotes.BackColor = System.Drawing.ColorTranslator.FromHtml("#ffffff");
LinkButtonNotes.ControlStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#cc6633");
MultiViewCustomers.ActiveViewIndex = 1;
}
protected void LinkButtonNotes_Click(object sender, EventArgs e)
{
//setup account notes menu tabs
myTableCellDetails.BackColor = System.Drawing.ColorTranslator.FromHtml("#ffffff");
LinkButtonDetails.ControlStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#cc6633");
myTableCellContacts.BackColor = System.Drawing.ColorTranslator.FromHtml("#ffffff");
LinkButtonContacts.ControlStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#cc6633");
myTableCellNotes.BackColor = System.Drawing.ColorTranslator.FromHtml("#e6e6e6");
LinkButtonNotes.ControlStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#ffa500");
MultiViewCustomers.ActiveViewIndex = 2;
}
// ********************************************************************************************************************
// Invoice Details Screen Code
// ********************************************************************************************************************
protected void ButtonInvoiceDetailsDeliveryAddresses_Click(object sender, EventArgs e)
{
//disable main panel and enable delivery address panel
PanelMain.Enabled = false;
PanelDeliveryAddresses.Enabled = true;
PanelDeliveryAddresses.Visible = true;
}
protected void ButtonInvoiceDetailsClose_Click(object sender, EventArgs e)
{
//close / discard account details screen
ButtonInvoiceDetailsDeliveryAddresses.Enabled = false;
if (ButtonInvoiceDetailsClose.Text == "Close")
{
CheckEditedItems();
}
else
{
InvoiceDetailsScreenConfig();
}
}
protected void ButtonInvoiceDetailsSave_Click(object sender, EventArgs e)
{
//check if new account has been selected
if (TextBoxInternalCode.Enabled == false)
{
ButtonInvoiceDetailsDeliveryAddresses.Enabled = true;
}
InvoiceDetailsScreenConfig();
}
protected void InvoiceDetailsScreenConfig()
{
//re-configure buttons
ButtonInvoiceDetailsClose.Text = (ButtonInvoiceDetailsClose.Text == "Discard") ? "Close" : "Discard";
ButtonInvoiceDetailsSave.Text = (ButtonInvoiceDetailsSave.Text == "Save Details") ? "Edit Details" : "Save Details";
ButtonInvoiceDetailsSave.CausesValidation = (ButtonInvoiceDetailsSave.CausesValidation == true) ? false : true;
ButtonInvoiceDetailsSuspend.Enabled = (ButtonInvoiceDetailsSuspend.Enabled == false) ? true : false;
ButtonInvoiceDetailsNew.Enabled = (ButtonInvoiceDetailsNew.Enabled == false) ? true : false;
//enable required fields for editing
TextBoxCreditLimit.ReadOnly = (TextBoxCreditLimit.ReadOnly == false) ? true : false;
PanelInvoiceDetails.Enabled = (PanelInvoiceDetails.Enabled == true) ? false : true;
TextBoxCustomerName.ReadOnly = (TextBoxCustomerName.ReadOnly == false) ? true : false;
//enable internal code input text box
if (TextBoxInternalCode.Enabled == true)
{
//reset new account fields access
DropDownListInternalCode.Visible = true;
DropDownListInternalCode.Enabled = true;
TextBoxInternalCode.Enabled = false;
TextBoxInternalCode.Visible = false;
}
}
protected void ButtonInvoiceDetailsSuspend_Click(object sender, EventArgs e)
{
//TODO suspend cusotmer account
}
protected void ButtonInvoiceDetailsNew_Click(object sender, EventArgs e)
{
//enable panel to allow field editing
InvoiceDetailsScreenConfig();
//change fields access
DropDownListInternalCode.Visible = false;
DropDownListInternalCode.Enabled = false;
TextBoxInternalCode.Enabled = true;
TextBoxInternalCode.Visible = true;
//clear text fields
TextBoxCustomerName.Text = "";
TextBoxCreditLimit.Text = "0.00";
TextBoxAccountBalance.Text = "0.00";
TextBoxAccountStatus.Text = "Active";
TextBoxAddressLine1.Text = "";
TextBoxAddressLine2.Text = "";
TextBoxAddressLine3.Text = "";
TextBoxTownCity.Text = "";
TextBoxPostCode.Text = "";
TextBoxVatNo.Text = "";
TextBoxInvoiceContactName.Text = "";
TextBoxTelephoneNo.Text = "";
TextBoxFaxNo.Text = "";
TextBoxEmail.Text = "";
TextBoxPlantNo.Text = "";
TextBoxVendorNo.Text = "";
}
// ********************************************************************************************************************
// Contacts Details Screen Code
// ********************************************************************************************************************
protected void ButtonContactsSave_Click(object sender, EventArgs e)
{
//re-conigure fields & buttons for editing
ContactDetailsScreenConfig();
}
protected void ButtonContactsClose_Click(object sender, EventArgs e)
{
if (ButtonContactsClose.Text == "Close")
{
//exit from account contacts
CheckEditedItems();
}
else
{
//re-conigure fields & buttons for editing
ContactDetailsScreenConfig();
}
}
protected void ContactDetailsScreenConfig()
{
//enable contact fields
DropDownListContactsName.Visible = (DropDownListContactsName.Visible == false) ? true : false;
DropDownListContactsName.Enabled = (DropDownListContactsName.Enabled == false) ? true : false;
TextBoxContactName.Enabled = (TextBoxContactName.Enabled == true) ? false : true;
TextBoxContactName.Visible = (TextBoxContactName.Visible == true) ? false : true;
PanelContactDetails.Enabled = (PanelContactDetails.Enabled == true) ? false : true;
//re-configure button access
ButtonContactsSave.Text = (ButtonContactsSave.Text == "Save Contact") ? "Edit Contact" : "Save Contact";
ButtonContactsDelete.Enabled = (ButtonContactsDelete.Enabled == false) ? true : false;
ButtonContactsAdd.Enabled = (ButtonContactsAdd.Enabled == false) ? true : false;
ButtonContactsClose.Text = (ButtonContactsClose.Text == "Discard") ? "Close" : "Discard";
ButtonContactsFirst.Enabled = (ButtonContactsFirst.Enabled == false) ? true : false;
ButtonContactsLast.Enabled = (ButtonContactsLast.Enabled == false) ? true : false;
ButtonContactsNext.Enabled = (ButtonContactsNext.Enabled == false) ? true : false;
ButtonContactsPrev.Enabled = (ButtonContactsPrev.Enabled == false) ? true : false;
}
protected void ButtonContactsDelete_Click(object sender, EventArgs e)
{
//TODO delete contact
}
protected void ButtonContactsAdd_Click(object sender, EventArgs e)
{
//enable panel to allow field editing
ContactDetailsScreenConfig();
}
protected void ButtonContactsPrev_Click(object sender, EventArgs e)
{
//TODO navigate previous contact
}
protected void ButtonContactsFirst_Click(object sender, EventArgs e)
{
//TODO navigate first contact
}
protected void ButtonContactsLast_Click(object sender, EventArgs e)
{
//TODO navigate last contact
}
protected void ButtonContactsNext_Click(object sender, EventArgs e)
{
//TODO navigate next contact
}
// ********************************************************************************************************************
// Account Notes Screen Code
// ********************************************************************************************************************
protected void ButtonNotesSave_Click(object sender, EventArgs e)
{
//enable notes field & reconfigure buttons
TextBoxAccountNotes.Enabled = true;
ButtonNotesSave.Text = "Save Notes";
ButtonNotesClose.Text = "Discard";
}
protected void ButtonNotesClose_Click(object sender, EventArgs e)
{
//close / discard account details screen
if (ButtonNotesClose.Text == "Close")
{
CheckEditedItems();
}
else
{
TextBoxAccountNotes.Enabled = false;
ButtonNotesSave.Text = "Edit Notes";
ButtonNotesClose.Text = "Close";
}
}
// ********************************************************************************************************************
// Delivery Address Details Screen Code
// ********************************************************************************************************************
protected void ButtonDelSaveAddress_Click(object sender, EventArgs e)
{
//re-conigure fields & buttons for editing
DeliveryDetailsScreenConfig();
}
protected void ButtonDelDeleteAddress_Click(object sender, EventArgs e)
{
//display delete prompt for items pending save
pnlBox myBox = new pnlBox();
myBox.showPanelBox();
}
protected void ButtonDelAddAddress_Click(object sender, EventArgs e)
{
//re-conigure fields & buttons for editing
DeliveryDetailsScreenConfig();
}
protected void ButtonDelClose_Click(object sender, EventArgs e)
{
if (ButtonDelClose.Text == "Close")
{
//disable delivery address panel and enable main panel
PanelDeliveryAddresses.Visible = false;
PanelDeliveryAddresses.Enabled = false;
PanelMain.Enabled = true;
}
else
{
//re-conigure fields & buttons for editing
DeliveryDetailsScreenConfig();
}
}
protected void DeliveryDetailsScreenConfig()
{
//re-conigure fields for editing
TextBoxDelAddressLine1.Enabled = (TextBoxDelAddressLine1.Enabled == false) ? true : false;
TextBoxDelAddressLine2.Enabled = (TextBoxDelAddressLine2.Enabled == false) ? true : false;
TextBoxDelAddressLine3.Enabled = (TextBoxDelAddressLine3.Enabled == false) ? true : false;
TextBoxDelTownCity.Enabled = (TextBoxDelTownCity.Enabled == false) ? true : false;
DropDownListDelCounty.Enabled = (DropDownListDelCounty.Enabled == false) ? true : false;
TextBox1DelPostCode.Enabled = (TextBox1DelPostCode.Enabled == false) ? true : false;
DropDownListDelCountry.Enabled = (DropDownListDelCountry.Enabled == false) ? true : false;
TextBoxDelTelephoneNo.Enabled = (TextBoxDelTelephoneNo.Enabled == false) ? true : false;
TextBoxDelFaxNo.Enabled = (TextBoxDelFaxNo.Enabled == false) ? true : false;
TextBoxDelEmail.Enabled = (TextBoxDelEmail.Enabled == false) ? true : false;
TextBoxDelMobile.Enabled = (TextBoxDelMobile.Enabled == false) ? true : false;
//re-configure buttons
ButtonDelSaveAddress.CausesValidation = (ButtonDelSaveAddress.CausesValidation == true) ? false : true;
ButtonDelSaveAddress.Text = (ButtonDelSaveAddress.Text == "Save Address") ? "Edit Address" : "Save Address";
ButtonDelDeleteAddress.Enabled = (ButtonDelDeleteAddress.Enabled == false) ? true : false;
ButtonDelAddAddress.Enabled = (ButtonDelAddAddress.Enabled == false) ? true : false;
ButtonDelClose.Text = (ButtonDelClose.Text == "Discard") ? "Close" : "Discard";
ButtonDelFirst.Enabled = (ButtonDelFirst.Enabled == false) ? true : false;
ButtonDelLast.Enabled = (ButtonDelLast.Enabled == false) ? true : false;
ButtonDelNext.Enabled = (ButtonDelNext.Enabled == false) ? true : false;
ButtonDelPrev.Enabled = (ButtonDelPrev.Enabled == false) ? true : false;
}
protected void ButtonDelPrev_Click(object sender, EventArgs e)
{
//TODO navigate previous delivery address
}
protected void ButtonDelFirst_Click(object sender, EventArgs e)
{
//TODO navigate fisrt delivery address
}
protected void ButtonDelLast_Click(object sender, EventArgs e)
{
//TODO navigate last delivery address
}
protected void ButtonDelNext_Click(object sender, EventArgs e)
{
//TODO navigate next delivery address
}
// ********************************************************************************************************************
// Misc Classes Code
// ********************************************************************************************************************
protected void CheckEditedItems()
{
if ((ButtonContactsSave.Text == "Save Contact") | (ButtonInvoiceDetailsSave.Text == "Save Details") | (ButtonNotesSave.Text == "Save Notes"))
{
//display save/close prompt for items pending save
msgBox myMessageBox = new msgBox();
myMessageBox.showModal("modalclosesave.aspx", "default.aspx");
}
else
{
//no items found pending save
Response.Redirect("default.aspx");
}
}
}
The events wont fire if the controls are not re-created on postback.
See http://www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx for more info.
See http://www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx for more info.
ASKER
Hi,
Thank you for the singingeels link. I have read through it but I am still unable to implement the suggestions which I think maybe due to my level of experience or maybe to the fact that I am using a Master Page with a ContenetPlaceHolder and the class I am calling is an external CS file in the App_Code. Effectively going through 3 levels of code files.
I will try a little longer but if I have no luck, I am going to use a ModalDialogBox and fill a hidden text field with a value that I will act upon.
Thank you.
Thank you for the singingeels link. I have read through it but I am still unable to implement the suggestions which I think maybe due to my level of experience or maybe to the fact that I am using a Master Page with a ContenetPlaceHolder and the class I am calling is an external CS file in the App_Code. Effectively going through 3 levels of code files.
I will try a little longer but if I have no luck, I am going to use a ModalDialogBox and fill a hidden text field with a value that I will act upon.
Thank you.
you should rather use a user web control instead of just a class.
you can have it fire its own events when the ok/cancel buttons are clicked. you can handle those in the parent page.
the problem still remains though - as crazyman said, you need to recreate those controls in the Init event of the page.
you can have it fire its own events when the ok/cancel buttons are clicked. you can handle those in the parent page.
the problem still remains though - as crazyman said, you need to recreate those controls in the Init event of the page.
Do you even need dynamic controls here, do the controls ever actually change depending on data etc..,
why not make it a user control, then just put the controls on the designer and show/hide as needed, no need for dynamic controls here really unless ive missed something.
why not make it a user control, then just put the controls on the designer and show/hide as needed, no need for dynamic controls here really unless ive missed something.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
Thank you both for all your help. I have implemented a Modal DialogBox class as I want ot be able to re-use the class from other ASPX pages rather than cut and paste controls on every page (see code snippet).
Thank you crazyman for your shortcut code. As you have both been so helpful and have really confirmed to me that my apporach was not right for my given example I will award you both points.
Thank you both for all your help. I have implemented a Modal DialogBox class as I want ot be able to re-use the class from other ASPX pages rather than cut and paste controls on every page (see code snippet).
Thank you crazyman for your shortcut code. As you have both been so helpful and have really confirmed to me that my apporach was not right for my given example I will award you both points.
public void ModalShow(string messagePage, string redirectPage)
{
//create javascript to launch modal dialog
string popupScript = "<script language='javascript' type='text/javascript'>" + "var strReturn; strReturn=window.showModalDialog('" + messagePage + "',null,'status:no;dialogWidth:400px;dialogHeight:130px;dialogHide:true;help:no;scroll:no'); if (strReturn != null) document.getElementById('LabelModalMessages').value=strReturn;" + "</script>";
Page myPage = HttpContext.Current.Handler as Page;
myPage.ClientScript.RegisterStartupScript(GetType(), "popupScript", popupScript);
}
ASKER
Thank you.