473,382 Members | 1,258 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Code behind problems

Hello,

I am new to .Net and I need some help getting access to the controls in my
aspx code. It seems my cs class can not get a good reference to the
controls declared in my aspx file. Here are some code snippets that I have.

CreateUser.aspx
<%@ Page Inherits="CreateUser" Language="C#" Debug="true" %>
.....
<td width=80> <asp:TextBox id="First_Name" type="text" size="15"
runat="server"/></td>

CreateUser.cs
public class CreateUser:Page
{
protected System.Web.UI.WebControls.TextBox fName;
.......

fName = (TextBox)this.Page.FindControl("First_Name");
//
Console.WriteLine ("Enter the SaveUser Method! " +
fName.Text); //Exception thrown here.

So I have the cs class inherit from Page and then the aspx inherits the
CreateUser class. I have also made the reference in the CreateUser to
TextBox protected (and I have tried public), but I always get this error
"System.NullReferenceException: Object reference not set to an instance of
an object." which goes back to my TextBox control I am trying to use.

I have tried this as Page.FindControl("form1").findControl("First_Name" )
and still get the same result.

Thanks for the help,

jlucas

Nov 18 '05 #1
2 999
instead of
protected System.Web.UI.WebControls.TextBox fName;
use
protected System.Web.UI.WebControls.TextBox First_Name;
since that's the ID assigned to it.

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com
"jlucas" <jo**********@yahoo.com> wrote in message
news:ux**************@TK2MSFTNGP11.phx.gbl...
Hello,

I am new to .Net and I need some help getting access to the controls in my
aspx code. It seems my cs class can not get a good reference to the
controls declared in my aspx file. Here are some code snippets that I have.
CreateUser.aspx
<%@ Page Inherits="CreateUser" Language="C#" Debug="true" %>
....
<td width=80> <asp:TextBox id="First_Name" type="text" size="15"
runat="server"/></td>

CreateUser.cs
public class CreateUser:Page
{
protected System.Web.UI.WebControls.TextBox fName;
.......

fName = (TextBox)this.Page.FindControl("First_Name"); //
Console.WriteLine ("Enter the SaveUser Method! " +
fName.Text); //Exception thrown here.

So I have the cs class inherit from Page and then the aspx inherits the
CreateUser class. I have also made the reference in the CreateUser to
TextBox protected (and I have tried public), but I always get this error
"System.NullReferenceException: Object reference not set to an instance of
an object." which goes back to my TextBox control I am trying to use.

I have tried this as Page.FindControl("form1").findControl("First_Name" )
and still get the same result.

Thanks for the help,

jlucas

Nov 18 '05 #2
Curt,

I had that before, but when trying to use the FindControl I switched the
name and I did not think to put it back.

I got it to work, but I am not sure why. I did what you said but at the
same time I pulled my button_click method out of the aspx file and put it in
the cs file with the class. So my new question is, do you have to have all
of the code in the cs file for the code behind to work? I was thinking that
the method the button was tied to should be in the aspx file.

Thanks for your help,

jlucas
"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:#3**************@TK2MSFTNGP11.phx.gbl...
instead of
protected System.Web.UI.WebControls.TextBox fName;
use
protected System.Web.UI.WebControls.TextBox First_Name;
since that's the ID assigned to it.

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com
"jlucas" <jo**********@yahoo.com> wrote in message
news:ux**************@TK2MSFTNGP11.phx.gbl...
Hello,

I am new to .Net and I need some help getting access to the controls in my aspx code. It seems my cs class can not get a good reference to the
controls declared in my aspx file. Here are some code snippets that I

have.

CreateUser.aspx
<%@ Page Inherits="CreateUser" Language="C#" Debug="true" %>
....
<td width=80> <asp:TextBox id="First_Name" type="text" size="15" runat="server"/></td>

CreateUser.cs
public class CreateUser:Page
{
protected System.Web.UI.WebControls.TextBox fName;
.......

fName =

(TextBox)this.Page.FindControl("First_Name");
//
Console.WriteLine ("Enter the SaveUser Method! " + fName.Text); //Exception thrown here.

So I have the cs class inherit from Page and then the aspx inherits the
CreateUser class. I have also made the reference in the CreateUser to
TextBox protected (and I have tried public), but I always get this error
"System.NullReferenceException: Object reference not set to an instance of an object." which goes back to my TextBox control I am trying to use.

I have tried this as Page.FindControl("form1").findControl("First_Name" ) and still get the same result.

Thanks for the help,

jlucas


Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Girish | last post by:
hey all, im building a asp.net project in vs.net. Is there a way to have the codebehind files sit in a different folder outside the website folder altogether? example: The aspx files are in...
1
by: Russ | last post by:
I've been trying to get my head around this for 3 days now and it seems like everything I try does not work for one reason or another. I built a test page using the TabStrip and MultiPage controls....
10
by: Don Munroe | last post by:
This one has me stumped. I have three web applications running on two different servers. The first that works fine is hosted by a .Net hosting company. Everyone that uses it has no problems...
0
by: Jimmy Tharpe | last post by:
Hi, I am having trouble editing the code-behind files of ASP.NET applications in Visual Studio .NET. I often lose syntax highlighting, code formatting, and intelisense. Occasionally, I lose...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
2
by: N. Demos | last post by:
I have a user control with code behind of which two instances are created/declared in my aspx page. The aspx page has code behind also, as I need to access methods of the usercontrols on page...
29
by: John Rivers | last post by:
Hello, What good reason there is for not allowing methods in ASPX pages I can't imagine, but here is how to get around that limitation: (START) <body MS_POSITIONING="FlowLayout"> <form...
1
by: Me | last post by:
Hi, I inherited a web application that has several very similar forms for gathering user data. User's must choose the form that best fits their needs and fill it out. Each form has about 15...
7
by: Alan Silver | last post by:
Hello, I am just looking at VWD and seeing what needs doing to take an existing site I've written by hand and importing it into VWD. I've already discovered that I need to rename my code-behind...
7
by: Alan Silver | last post by:
Hello, I have installed the 2.0 framework, and am looking at converting some of my 1.1 pages to use partial classes. I don't (yet) have VS2005, so I'm doing this by hand, but am having problems....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.