By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,214 Members | 2,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,214 IT Pros & Developers. It's quick & easy.

Can't access to web control textbox property

P: n/a
Hi,
I built a web control wich has text box and public property that set or get
the text value of that text box.

when i try to access that property i got error:
"Object reference not set to an instance of ..."

the compilation has no detected problem and i do not understand what is the
problem.

..ascx (web control)
-------------------
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="WebUserControl1.ascx.cs" Inherits="WebGrid.WebUserControl1"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
Name:<asp:TextBox id="txtName" runat="server"></asp:TextBox>

public class WebUserControl1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.TextBox txtName;
private string num;
public string txt
{
get
{
return txtName.Text;
}
set
{
txtName.Text=value;
}
}}
-----------------------------------------------------
..aspx (web form that uses the web control
.................................................. ...
<%@ Register TagPrefix="uc1" TagName="WebUserControl1"
Src="WebUserControl1.ascx" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="WebGrid.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</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">
<uc1:WebUserControl1 id="WebUserControl11"
runat="server"></uc1:WebUserControl1>
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 16px; POSITION:
absolute; TOP: 64px" runat="server"
Text="Button"></asp:Button>

</form>
</body>
</HTML>
private void Button1_Click(object sender, System.EventArgs e)
{
WebUserControl1 ctl=new WebUserControl1();
Response.Write(ctl.txt);
}

Jun 6 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Jun 6, 9:07 am, Nimrod Rotner <Nimrod
Rot...@discussions.microsoft.comwrote:
Hi,
I built a web control wich has text box and public property that set or get
the text value of that text box.

when i try to access that property i got error:
"Object reference not set to an instance of ..."

the compilation has no detected problem and i do not understand what is the
problem.

.ascx (web control)
-------------------
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="WebUserControl1.ascx.cs" Inherits="WebGrid.WebUserControl1"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
Name:<asp:TextBox id="txtName" runat="server"></asp:TextBox>

public class WebUserControl1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.TextBox txtName;
private string num;
public string txt
{
get
{
return txtName.Text;
}
set
{
txtName.Text=value;
}
}}
-----------------------------------------------------
.aspx (web form that uses the web control
.................................................. ..
<%@ Register TagPrefix="uc1" TagName="WebUserControl1"
Src="WebUserControl1.ascx" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="WebGrid.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</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">
<uc1:WebUserControl1 id="WebUserControl11"
runat="server"></uc1:WebUserControl1>
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 16px; POSITION:
absolute; TOP: 64px" runat="server"
Text="Button"></asp:Button>

</form>
</body>
</HTML>

private void Button1_Click(object sender, System.EventArgs e)
{
WebUserControl1 ctl=new WebUserControl1();
Response.Write(ctl.txt);
}
try to call WebUserControl11.txt

Jun 6 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.