?
Solved

ASP.Net /C# Question

Posted on 2006-05-15
9
Medium Priority
?
346 Views
Last Modified: 2013-11-25
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
0
Comment
Question by:mani_sai
  • 4
  • 3
8 Comments
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16682227
ok so you have a textbox, and onclick do you want to populate another textbox?
0
 
LVL 8

Author Comment

by:mani_sai
ID: 16682319
yes, you are right.
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16682355
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Author Comment

by:mani_sai
ID: 16682373
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
 
LVL 8

Author Comment

by:mani_sai
ID: 16682835
Experts, any one can help me out, thanks
0
 
LVL 8

Author Comment

by:mani_sai
ID: 16682885
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
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16682965
I Agree.

- PAQ-ing the question and refunding 500 points
0
 

Accepted Solution

by:
GranMod earned 0 total points
ID: 16683037
PAQ-ing the question and refunding 500 points

Thanks REA_ANDREW !

GranMod
The Experts Exchange
Community Support Moderator of all Ages
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question