ASP.Net /C# Question

Hello Experts,

I have user control(ascx control):Code is shown below
---------------------------------------------------------
namespace CSharpWebApplication.UserControls
{
      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 CurrencyValidator.
      /// </summary>
      public class CurrencyValidator : System.Web.UI.UserControl
      {
            protected System.Web.UI.WebControls.TextBox txtCurrency;
            protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
            protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
            protected string CurValue;

            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
            }

            public string CurrencyValue
            {
                  get {return txtCurrency.Text;}
            }

                        

            #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
      }
}

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

i am calling this user control in my aspx page like this:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using CSharpWebApplication.UserControls;

namespace CSharpWebApplication
{
      /// <summary>
      /// Summary description for TestingUserControls.
      /// </summary>
      public class TestingUserControls : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.Button btnSubmit;
            protected System.Web.UI.WebControls.TextBox TextBox1;
            protected CurrencyValidator CurrValue;
            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
            }

            #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.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion

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

                  TextBox1.Text = CurrValue.CurrencyValue;
            
            }
      }
}

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

i am trying to capture the value from the user control on button click, i am getting error object reference not set.

i am new to C#.

Thanks
LVL 8
mani_saiAsked:
Who is Participating?
 
GranModCommented:
PAQ-ing the question and refunding 500 points

Thanks REA_ANDREW !

GranMod
The Experts Exchange
Community Support Moderator of all Ages
0
 
REA_ANDREWCommented:
ok so you have a textbox, and onclick do you want to populate another textbox?
0
 
mani_saiAuthor Commented:
yes, you are right.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
REA_ANDREWCommented:
ok so say you have this on the ASPX page

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Populate"/>

in the CS code we need to write a simple function for Populate

protected void Populate(Object sender, EventArgs e)
{
if(TextBox1.Text == null || TextBox1.Text == "")
{
TextBox2.Text = "Text Box 1 Is Empty";
}
else
{
TextBox2.Text = TextBox1.Text;
}
}

thats it

0
 
mani_saiAuthor Commented:
Sorry, i did not explain properly.

I am getting the value from the user control textbox and populating the textbox on my aspx page.

If you see my code:

  TextBox1.Text = CurrValue.CurrencyValue;

this line i am getting run time error, object refrence not set to an instant of an object.

Thanks
0
 
mani_saiAuthor Commented:
Experts, any one can help me out, thanks
0
 
mani_saiAuthor Commented:
Thanks, i figured it out myself.

It was not accessing the user control properly in my aspx page..

working code is below.

-----------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using CSharpWebApplication.UserControls;

namespace CSharpWebApplication
{
      /// <summary>
      /// Summary description for TestingUserControls.
      /// </summary>
      public class TestingUserControls : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.Button btnSubmit;
            protected System.Web.UI.WebControls.TextBox TextBox1;
            protected  CurrencyValidator CurrValidator;
            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
            }

            #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.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion

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

                  
                  TextBox1.Text = CurrValidator.CurrencyValue;
            
            }
      }
}


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

<%@ Page language="c#" Codebehind="TestingUserControls.aspx.cs" AutoEventWireup="false" Inherits="CSharpWebApplication.TestingUserControls" %>
<%@ Register TagPrefix="UC" TagName="CurrencyValidator" Src="UserControls/CurrencyValidator.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
      <HEAD>
            <title>TestingUserControls</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="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <asp:TextBox id="TextBox1" style="Z-INDEX: 102; LEFT: 8px; POSITION: absolute; TOP: 64px" runat="server"></asp:TextBox>
                  <UC:CurrencyValidator id="CurrValidator"  name="CurrValidator" runat="server"></UC:CurrencyValidator>
                  <asp:Button id="btnSubmit" style="Z-INDEX: 101; LEFT: 208px; POSITION: absolute; TOP: 192px"
                        runat="server" Text="Submit"></asp:Button>
            </form>
      </body>
</HTML>


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


Thanks
0
 
REA_ANDREWCommented:
I Agree.

- PAQ-ing the question and refunding 500 points
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.