Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 16th, 2005, 04:04 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: United Kingdom
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default Help with control initialization in user control

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:2 4
   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



__________________
Regards
Mike
  #2 (permalink)  
Old December 16th, 2005, 10:07 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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
  #3 (permalink)  
Old December 17th, 2005, 01:25 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: United Kingdom
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

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

  #4 (permalink)  
Old December 18th, 2005, 03:17 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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
  #5 (permalink)  
Old December 19th, 2005, 01:46 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: United Kingdom
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

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



  #6 (permalink)  
Old December 19th, 2005, 08:54 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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
  #7 (permalink)  
Old December 19th, 2005, 09:31 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: United Kingdom
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

Ah!!!!!Peter,

You are great!!! Thanx for the resolution.....It worked..BANG ON TARGET

Thanks
Mike

  #8 (permalink)  
Old December 19th, 2005, 11:08 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I should have caught that from the first post.
Getting rusty I guess.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Control shoakat ASP.NET 2.0 Professional 0 June 20th, 2007 07:30 AM
Add Windows User control in Web User Control agarwalvidhu C# 0 March 30th, 2006 01:17 AM
Help! Custom Server Control using User Control diehard ASP.NET 1.0 and 1.1 Professional 2 January 4th, 2006 12:33 PM
Minor User Control Initialization Problem Ron Howerton VB.NET 2002/2003 Basics 4 May 10th, 2004 07:38 AM
User Control stu9820 ASP.NET 1.0 and 1.1 Basics 2 February 10th, 2004 01:47 PM



All times are GMT -4. The time now is 07:12 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.