Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exception on PostBack in Dynamically loaded UserControl

Status
Not open for further replies.

Akusei

Programmer
Jul 15, 2003
90
US
I am having a heck of a time with this, please someone know something about this!!

I have a web application with 2 files (not including the defaults like web.config). default.aspx and postbacktest.ascx.

I am loading the PostBackTest.ascx into a panel on Default.aspx dynamically via LoadControl. The following are the contents of all files and the error I get when I click the button on PostBackTest.ascx

Default.aspx
Code:
<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="true" Inherits="LoadControlTest.WebForm1" %>
<%@ Reference Control="PostBackTest.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
	<HEAD>
		<title>WebForm1</title>
		<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
		<meta name="CODE_LANGUAGE" Content="C#">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="[URL unfurl="true"]http://schemas.microsoft.com/intellisense/ie5">[/URL]
	</HEAD>
	<body>
		<form id="Form1" method="post" runat="server">
			<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="100%" height="98%" border="1">
				<TR>
					<TD style="WIDTH: 138px"></TD>
					<TD><div style="OVERFLOW: auto; WIDTH: 100%; HEIGHT: 100%">
							<asp:Panel id="PnlMain" runat="server"></asp:Panel>
						</div>
					</TD>
				</TR>
			</TABLE>
		</form>
	</body>
</HTML>

Default.cs
Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace LoadControlTest
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Panel PnlMain;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			this.PnlMain.Controls.Clear();
			Control c = LoadControl("PostBackTest.ascx");
			c.ID = "PostBackTest_ascx";
			this.PnlMain.Controls.Add(c);
		}

		#region Web Form Designer generated code

	}
}

PostBackTest.ascx
Code:
<%@ Control Language="c#" AutoEventWireup="true" ClassName="PostBackTest" Codebehind="PostBackTest.ascx.cs" Inherits="LoadControlTest.PostBackTest" TargetSchema="[URL unfurl="true"]http://schemas.microsoft.com/intellisense/ie5"%>[/URL]
<asp:Button id="Button1" Text="Button" runat="server"></asp:Button>

PostBackTest.cs
Code:
namespace LoadControlTest
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///		Summary description for PostBackTest.
	/// </summary>
	public abstract class PostBackTest : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.Button Button1;

		private void Page_Load(object sender, System.EventArgs e)
		{
		}

		#region Web Form Designer generated code

		private void Button1_Click(object sender, System.EventArgs e)
		{
			Response.Write("TEST");
		}
	}
}

Exception Output: Happens when Button is clicked
Code:
Server Error in '/LoadControlTest' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   System.Web.UI.Control.OnBubbleEvent(Object source, EventArgs args)
   System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e)
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain()

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032

Could someone please shed some light on this!? It was happening in a much larger application, so for simplicity and the sake of my brain, I moved it into a test harness with as little code as possible... low and behold, it happened in this test harness as well.

Best regards,
Akusei
 
It may be that the button is not known as it is Protected e.g.
Code:
protected System.Web.UI.WebControls.Button Button1
Try making it public and see if that has any effect.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Problem solved! I've been beating my head against a wall for 3 days trying to figure this out.... Nothing would work and everything seemed perfect. I had a friend run this exact application on his machine and it worked fine!!! So, I rebooted my PC and now it works fine!

That makes me very angry....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top