|
Subject:
|
Help with control initialization in user control
|
|
Posted By:
|
mike_remember
|
Post Date:
|
12/16/2005 3:04:19 AM
|
Hi Friends
I need some help. I will explain it. I have a page and a user control. There is a hidden control named "Hidden1" in the usercontrol. Now I have created a test method in the usercontrol:
------------------------------------------------------- public void SetHdnVal() { Hidden1.Value="Mike"; } -------------------------------------------------------
Now I call this method from the page load of my main page; webform1.aspx
//CREATED OBJECT INSTANCE WebUserControl1 usr_ctrl=new WebUserControl1();
private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here usr_ctrl.SetHdnVal(); }
BUT NOW I GET THE ERROR BELOW:
----------------------------------------------------------------------
Server Error in '/trial' 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:
Line 22: public void SetHdnVal() Line 23: { Line 24: Hidden1.Value="Mike"; Line 25: Line 26: }
Source File: c:\inetpub\wwwroot\trial\webusercontrol1.ascx.cs Line: 24
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.] trial.WebUserControl1.SetHdnVal() in c:\inetpub\wwwroot\trial\webusercontrol1.ascx.cs:24 trial.test.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\trial\test.aspx.cs:23 System.Web.UI.Control.OnLoad(EventArgs e) +67 System.Web.UI.Control.LoadRecursive() +29 System.Web.UI.Page.ProcessRequestMain() +724
-------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:1.0.3705.0; ASP.NET Version:1.0.3705.0
----------------------------------------------------------------------
Please help asap.
Thanks Michael
|
|
Reply By:
|
planoie
|
Reply Date:
|
12/16/2005 9:07:41 AM
|
This indicates that the control "Hidden1" is not instantiated. Without seeing your control markup or all the code behind I'll venture a guess that you have something like this in your markup:
<input type="hidden" runat="server" id="Hidden1" />
But you are missing this in the codebehind:
protected System.Web.UI.WebControl.HtmlInputHidden Hidden1;
-Peter
|
|
Reply By:
|
mike_remember
|
Reply Date:
|
12/17/2005 12:25:11 AM
|
Hi Peter
Thanks for the quick response. This matter is really urgent, so I will paste below my page and user control code...... Anyways the reason I was trying to do this was to create a user control for datagrid paging apart from the usual datagrid paging... So here's the code
User Control:
HTML CODE ----------
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="WebUserControl1.ascx.cs" Inherits="trial.WebUserControl1" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<INPUT type="hidden" id="Hidden1" name="Hidden1" runat="server">
Codebehind -----------
namespace trial { 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 WebUserControl1. /// </summary> public class WebUserControl1 : System.Web.UI.UserControl { protected System.Web.UI.HtmlControls.HtmlInputHidden Hidden1;
private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here }
public void SetHdnVal() { Hidden1.Value="Mike"; }
#region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load);
} #endregion } }
Now the Main page, i.e. test.aspx (HTML) -----------------------------------
<%@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false" Inherits="trial.test" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html> <head> <title>test</title> <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name=vs_defaultClientScript content="JavaScript"> <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5"> </head> <body MS_POSITIONING="GridLayout"> <form id="test" method="post" runat="server">
</form> </body> </html>
CODEBEHIND -----------
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 trial { /// <summary> /// Summary description for test. /// </summary> public class test : System.Web.UI.Page { WebUserControl1 usr_ctrl=new WebUserControl1();
private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here usr_ctrl.SetHdnVal(); }
#region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } }
Hopefully you will be able to help me.
Thanks Mike
|
|
Reply By:
|
planoie
|
Reply Date:
|
12/18/2005 2:17:46 PM
|
Looks like your code contains the right thing now, but I assume that it's still not working. Can you tell us what the error is now? My initial suggestion should have cleared the first error you reported.
-Peter
|
|
Reply By:
|
mike_remember
|
Reply Date:
|
12/19/2005 12:46:42 AM
|
Hi Peter
Thanx for the reply. But I am afraid I am still getting the error as mentioned in the previous post. I guess the problem is that the user control page's controls have not been initialized. This may be bcoz I am calling the user control's function on the page load of my main page. When I debugged the program, I found that the breakpoint didn't enter the page_load event of the user control. Any ideas now what to do.......................
Regards MIke
|
|
Reply By:
|
planoie
|
Reply Date:
|
12/19/2005 7:54:52 AM
|
The hidden control is initialized automatically by the ASP.NET page parser. Basically, this:
<INPUT type="hidden" id="Hidden1" name="Hidden1" runat="server">
is what creates a new instance of the control.
In your page code you don't have a markup instance of the USER control because you are attempting to create one in the code behind. So you instantiate an instance of the control's code behind class. HOWEVER! Because of that (note what I said above) there is no instance of the hidden input control because the markup part of the control (the ASCX file) is never parsed. There is no technical reason why you can't create an instance of the code-behind class, it just doesn't work that way. Instead you need to use the Page.LoadControl() method:
WebUserControl1 usr_ctrl; usr_ctrl = (WebUserControl1)LoadControl("WebUserControl1.ascx");
This makes the process actual load your user control markup file, not just the class defined in the codebehind file.
-Peter
|
|
Reply By:
|
mike_remember
|
Reply Date:
|
12/19/2005 8:31:20 AM
|
Ah!!!!!Peter,
You are great!!! Thanx for the resolution.....It worked..BANG ON TARGET
Thanks Mike
|
|
Reply By:
|
planoie
|
Reply Date:
|
12/19/2005 10:08:41 AM
|
I should have caught that from the first post. Getting rusty I guess.
|