Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't access the tag properties in the user control

Status
Not open for further replies.

fly231

Programmer
May 29, 2005
24
GB
Can someone please help me with this.

I can't access the tag properties in the user control.

I did this

Control UserControl1 =LoadControl("signupForm1.ascx");
PlaceHolderSignupPhone.Controls.Add(UserControl1);

It works fine.

But I cant access the tag properties in the user control.
All I have in the signupForm1.ascx file is asp textboxes, checkBoxes and Dopdownlist items.
I just want to access their properties when a onClick event get the value of the TextBox.
I get an error saying
"The type or namespace name 'phoneNo1' could not be found"
Where phoneNo1 is a TextBox ID.

It would be great if anyone can help me with this.

Thanks
 
You'll need to change the User Control Textbox to Public e.g.
Code:
[b]Public[/b] WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

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

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Thanks for the quick response ca8msm.
I tried what you said. But it gives an error.
I'm very new to ASP.net so forgive me if I sound bumb.
HE's my ascx page:



script language="C#" runat="server">
public TextBox phoneNo1;

</script>

<table align="left" cellpadding="5" cellspacing="0" border="0">
<tr>
<td><asp:textbox ID="phoneNo1" runat="server"/></td>
<td><asp:checkbox runat="server" Checked="true" ID="cps1"/></td>
<td><asp:checkbox runat="server" ID="transfer1"/></td>
</tr>
</table>


I get the error saying
"'ASP.signupForm3_ascx' already contains a definition for 'phoneNo1'"

So how else do i make the TextBox public?
It wouls be great if you can tell me how to do it in c# cause I don't know vb.net.

Thanks
 
You can aslo use signupForm1.FindControl.
The Textbox is already declared in the auto generate code.

Maybe try
public System.Web.UI.Webcontrols.Textbox MyTextBox = phoneNo1;

No sure but you may have to do set
MyTextBox = phoneNo1;
in page_load
 
Or try this:
public System.Web.UI.Webcontrols.Textbox MyTextBox
{
get
{
return phoneNo1;
}
}
 
Thanks for the response RTomes,

I tried what you said and I get this error
"The type or namespace name 'Webcontrols' does not exist in the class or namespace 'System.Web.UI' "

Not really sure why ..
 
C# is case sensative.
Cap the C in WebControls
System.Web.UI.WebControls.TextBox
 
Hi,

I tried that as well, I can't get it to work. May be it's just my lack of knowlege about ASP.Net.

I'll give past both the file here so, may be you can tell me what I should do.

Here's the Main ASPX file
Code:
<%@ Page Language="C#" debug="true"  %>
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Web.UI.HtmlControls" %> 
<HTML>
<HEAD>
<title>test</title>		
<script language="C#" runat="server">	
	void Page_Load(Object sender, EventArgs e) {    
		if (!IsPostBack) 
		{  
			Control UserControl1 = LoadControl("includes/signupForm1.ascx");
			PlaceHolderSignup1.Controls.Add(UserControl1);
			Control UserControl2 = LoadControl("includes/signupForm2.ascx");
			PlaceHolderSignup2.Controls.Add(UserControl2);
		}	
    }
	void doSomething(Object sender, EventArgs e)
	{
		Response.Write(UserControl1.MyTextBox.Text);
	}	
</SCRIPT>

</HEAD>
	<body >
	<form method="post" id="formSignup" name="formSignup" runat=server >				
	<table>
		<tr><td><asp:PlaceHolder runat="server" id="PlaceHolderSignupPhone" /></td></tr>
		<tr><td><asp:PlaceHolder runat="server" id="PlaceHolderSignupADSL" /> </td></tr>						
		<tr><td align="right"><asp:Button Text="Submit" OnClick="doSomething" runat="server" id="btnSubmitForm" /> </td></tr>					
	</table>	
    </form>
	</body>
</HTML>
------------------------------------
Here is the signupForm1.ascx , the ASCX file
Code:
<script language="C#" runat="server">
public System.Web.UI.WebControls.TextBox MyTextBox  
{
	get{
		return phoneNo1;
	}
}
</script>		

<table align="left" cellpadding="5" cellspacing="0" border="0">
	<tr>
	<td><asp:textbox ID="phoneNo1" runat="server"/></td>
	<td><asp:checkbox runat="server" Checked="true" ID="cps1"/></td>
	<td><asp:checkbox runat="server" ID="transfer1"/></td>
	</tr>
</table>

I just want to get the values from the textBoxes and checkboxes when an OnClick event occurs.

I would appreciate any help on this.

Thanks
 
Sorry there's a small mistake on the code forgot to change it .
PlaceHolderSignupADSL should be PlaceHolderSignup1 and
PlaceHolderSignupPhone should be PlaceHolderSignup2

Thanks.
 
You need to declare your user control in the ASPX code behind as well, are your doing that?

private signupForm signupForm1;

You access the text box like:
signupForm1.MyTextBox.Text="Hello";
 
Sorry , I do not understand what you are refering to.
The code above is all there is.

"sinupForm1" is the ASCX file I have shown above. It's not a class. sinupForm2 is a similar ASCX file with different textBoxes.

it dispalys everying fine ,
the problem comes when I want to access the textBox in the sinupForm1. I tried various methods but it doesnt seem to work.

Thanks
 
Thank for all the help.

I tried the FindControl method and it worked this time.

Here's the code I used, if this will help anyone.

Code:
<%@ Page Language="C#" debug="true"  %>
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Web.UI.HtmlControls" %> 
<%@ Import namespace="System.Web.UI.WebControls" %>
<HTML>
<HEAD>
<title>test</title>		
<script language="C#" runat="server">
public Control UserControl1;
public Control UserControl2;
public TextBox tempStr;
public string tempText;
	
	void Page_Load(Object sender, EventArgs e) {    
		if (!IsPostBack) 
		{  
			
		}
			UserControl1 = LoadControl("signupForm1.ascx");
			PlaceHolderSignup1.Controls.Add(UserControl1);
			UserControl2 = LoadControl("signupForm2.ascx");
			PlaceHolderSignup2.Controls.Add(UserControl2);
			
				
    }
	void doSomething(Object sender, EventArgs e)
	{
		
		tempStr=((TextBox)this.UserControl1.FindControl("phoneNo1"));		
		Response.Write("tempStr :"+tempStr.Text);
	}	
</SCRIPT>

</HEAD>
	<body >
	<form method="post" id="formSignup" name="formSignup" runat=server >				
	<table>
		<tr><td><asp:PlaceHolder runat="server" id="PlaceHolderSignup1" /></td></tr>
		<tr><td><asp:PlaceHolder runat="server" id="PlaceHolderSignup2" /> </td></tr>						
		<tr><td align="right"><asp:Button Text="Submit" OnClick="doSomething" runat="server" id="btnSubmitForm" /> </td></tr>					
	</table>	
    </form>
	</body>
</HTML>
 
Code:
UserControl UserControl1;
UserControl UserControl2; 

<script language="C#" runat="server">    
    void Page_Load(Object sender, EventArgs e) {    
        if (!IsPostBack) 
        {  
            UserControl1 = LoadControl("includes/signupForm1.ascx");
            PlaceHolderSignup1.Controls.Add(UserControl1);
            UserControl2 = LoadControl("includes/signupForm2.ascx");
            PlaceHolderSignup2.Controls.Add(UserControl2);
        }    
    }
    void doSomething(Object sender, EventArgs e)
    {
        Response.Write(UserControl1.MyTextBox.Text);
    }    
</SCRIPT>





 
Hi RTomes, thanks for the info

I don't know why but, way you said doesn't work.

Get an error
'System.Web.UI.Control' does not contain a definition for 'MyTextBox'

Plus what if you have about 50 text boxes and checkBoxes.
Wouldn't it be easier just to use the find FindControl in a loop. Something like this

Code:
for(int i=1;i<51;i++)
{
 tempTextBox=((TextBox)this.UserControl1.FindControl("phoneNo"+i));
 Response.Write(tempTextBox.Text);
}

Thanks..
 
I believe if you look above that was my very first sugestion.

You can also use signupForm1.FindControl
 
True, Thanks for that.

When you first said it, I tried it but it didn't work because I was loading the user control inside
"if (!IsPostBack)" like below.

Code:
if (!IsPostBack) 
{  
            UserControl1 = LoadControl("includes/signupForm1.ascx");
            PlaceHolderSignup1.Controls.Add(UserControl1);
            UserControl2 = LoadControl("includes/signupForm2.ascx");
            PlaceHolderSignup2.Controls.Add(UserControl2);
}

but later when I put it outside the "if (!IsPostBack)" , it started working.

Do you know why this is? I can't see a reason why it didn't work inside "if (!IsPostBack)" .

Thanks again for all the help.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top