Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

Why does this code work in server-side code, but not in a class?

$
0
0

Using ASP.NET 3.5 created in VS 2008

I have code that builds a message box, that was written by a former co-worker. I use it on all my web forms where I want a message box. it works perfectly when the code is on each form.

I've tried to put the server-side code into a class, and call it from the class. When the code is called, the web form otherwise works correctly, and no error messages appear, but neither does the message box.

Here's the html code:

<body style="background-color: #ccffff">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />

Textboxes, labels, other stuff goes here

<!-- ######################################## POPUP MESSAGE BOX ######################################################## -->
<!--### Include LinkButton as Target Control and Leave out Text to Make it Invisible ###-->
<asp:LinkButton ID="lnkPopup" runat="server" />
<asp:ModalPopupExtender ID="mpeModalWindow" TargetControlID="lnkPopup" PopupControlID="pnlPopup"
runat="server" Drag="True" DropShadow="True" X="-1" Y="-1" Enabled="True" BackgroundCssClass="modalBackground"
PopupDragHandleControlID="pnlPopup" RepositionMode="RepositionOnWindowScroll" />
<!--### X and Y Are Screen Coordinates to Position the Popup Extender and Can Be Set Programmtically in Behind Code ###-->
<!--### The PANEL Will Render the Image (Icon), Text, and Buttons in The ModalPopUp Extender ###-->
<asp:Panel ID="pnlPopup" BackColor="#5BBFB0" Width="300" runat="server" Style="display: none"
CssClass="modalPopup" BorderStyle="Ridge" BorderWidth="5">
<!--### Ajax Update Panel ###-->
<asp:UpdatePanel ID="updatePanel2" runat="server">
<ContentTemplate>
<div class="divHeader" style="line-height: 30px">
<!--### The Message Box Display Header ###-->
<asp:Image ID="Images" runat="server" ImageUrl="" Height="30px" Width="30px" ImageAlign="Left" />
<asp:Label ID="lbl_Header" runat="server" Width="240px" ForeColor="#FFFF66" Font-Bold="True"
Font-Size="Large" CssClass="boxHeader" />
</div>
<br />
<div class="boxHeader">
<!--### Message Box Message Area ###-->
<asp:Label ID="lbl_BoxMessage" runat="server" Width="260" />
</div>
<br />
<div class="boxHeader">
<!--### Message Box Button Area ###-->
<asp:Button ID="btn_OK" Text="OK" runat="server" OnClick="btnOK_Cancel_Click" BorderStyle="Outset"
BackColor="#CCCCCC" ForeColor="#993300" Width="65px" CausesValidation="False" />
<asp:Button ID="btn_Cancel" Text="Cancel" OnClick="btnOK_Cancel_Click" runat="server"
BorderStyle="Outset" BackColor="#CCCCCC" ForeColor="#993300" Width="65" />
</div>
<br />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
<!-- ############################################################################################################################# -->
</form>
</body>

Here is the server side code:

protected void BuildMsgBox(string title, string msg, string icon)
{
//*** Set the Appearance of the Modal Popup Control ***

Image errorImg = (Image)pnlPopup.FindControl("Images");
Label lblMsgBox = (Label)pnlPopup.FindControl("lbl_BoxMessage");
Label lblHeader = (Label)pnlPopup.FindControl("lbl_Header");
Button cancelButton = (Button)pnlPopup.FindControl("btn_Cancel");
Button OKButton = (Button)pnlPopup.FindControl("btn_OK");

lblHeader.Text = title;
lblMsgBox.Text = msg;
errorImg.ImageUrl = icon;
cancelButton.Visible = false;
mpeModalWindow.Show();

}

protected void btnOK_Cancel_Click(object sender, EventArgs e)
{

Button theBtn = (Button)sender;


if (theBtn.ID == "btn_OK")
{
mpeModalWindow.Hide();
}

}

Here I'm calling it from within a button on_click event:

snip...

if (dateCompare > 0)
{
title = "Invalid selection!";
msg = "The ending month must be later than the starting month.";
icon = "IconImages/warning.ico";
BuildMsgBox(title, msg, icon);
}

Now here is the code put into methods in a class:

using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for MessageBox
/// </summary>
public static class MessageBox
{
public static void BuildMsgBox(string title, string msg, string icon)

{
Page page = HttpContext.Current.Handler as Page; //Reference to the calling form


Panel pnlPopup = HttpContext.Current.Handler as Panel;

AjaxControlToolkit.ModalPopupExtender mpeModalWindow = HttpContext.Current.Handler as AjaxControlToolkit.ModalPopupExtender;


//*** Set the Appearance of the Modal Popup Control ***

Image errorImg = (Image)pnlPopup.FindControl("Images");
Label lblMsgBox = (Label)pnlPopup.FindControl("lbl_BoxMessage");
Label lblHeader = (Label)pnlPopup.FindControl("lbl_Header");
Button cancelButton = (Button)pnlPopup.FindControl("btn_Cancel");
Button OKButton = (Button)pnlPopup.FindControl("btn_OK");

lblHeader.Text = title;
lblMsgBox.Text = msg;
errorImg.ImageUrl = icon;
cancelButton.Visible = false;

mpeModalWindow.Show();

}

public static void btnOK_Cancel_Click(object sender, EventArgs e)
{
Page page = HttpContext.Current.Handler as Page; //Reference to the calling form
AjaxControlToolkit.ModalPopupExtender mpeModalWindow = HttpContext.Current.Handler as AjaxControlToolkit.ModalPopupExtender;
Button theBtn = HttpContext.Current.Handler as Button;

if (theBtn.ID == "btn_OK")
{
mpeModalWindow.Hide();
}

}
}

And here it is being called from within the same button on_click event as above:

snip...

if (dateCompare > 0)
{
title = "Invalid selection!";
msg = "The ending month must be later than the starting month.";
icon = "IconImages/warning.ico";

MessageBox.BuildMsgBox(title, msg, icon);


Viewing all articles
Browse latest Browse all 5678

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>