Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 16th, 2006, 09:59 AM
Friend of Wrox
 
Join Date: Apr 2004
Location: San Francisco, CA, USA.
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default Accessing the Controls of a User Control?

Is it possible to Access the controls inside a User Control from the main page?

I've tried this, and it doesn't seem to work:

(Main Page-codebehind)

Dim tbCCNumber As TextBox = CType(Me.PaymentPanel1.FindControl("tbCCNumber"), TextBox)

In this case, PaymentPanel1 is an instance of a User Control called PaymentPanel, which I've referenced on the main page as follows:

<%@Reference Control="~/PaymentPanel.ascx" %>

Do I need to expose the constituent controls on the user control as properties or something like that?

Thanks.

Aaron


  #2 (permalink)  
Old June 16th, 2006, 10:09 AM
Friend of Wrox
 
Join Date: Apr 2004
Location: San Francisco, CA, USA.
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay I did some research, and found that to access the user control's constituent controls, they must be declared as public.

Now my question is: how do you do this in ASP.NET 2.0? In 1.1 this was easy, because they were visibly declared at the top of the codebehind. Now in 2.0 they are only declared in the aspx page and I can't figure out how to add a Public accessor to the declaration.

Thanks.

Aaron

  #3 (permalink)  
Old June 16th, 2006, 10:34 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You could add a public property to your control, that gives access to your server control, like this:

public TextBox UserNameBox
{
  get
  {
    return txtUserName;
  }
}

This way, the page can access the control, but not completely change it. You can, however, change its own properties like its Text property. If you want to be able to change the entire box, implement a set accessor as well.

Now you can access the properties like this:

MyUserControl.UserNameBox.Text = "SomeText";

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
  #4 (permalink)  
Old June 16th, 2006, 03:15 PM
Friend of Wrox
 
Join Date: Apr 2004
Location: San Francisco, CA, USA.
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Imar,

The only control on the User control that I'll need to change will be a dropdown list. I'll need to be able to execute a RemoveAt method on it. Should I just write my own method on the user control which does that?

Aaron

  #5 (permalink)  
Old June 16th, 2006, 03:30 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

No need for that. The public control property allows you to call that method directly:

MyUserControl.MyDropDown.RemoveAt(0);

should work as well....

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
  #6 (permalink)  
Old June 16th, 2006, 04:33 PM
Friend of Wrox
 
Join Date: Apr 2004
Location: San Francisco, CA, USA.
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Imar,

It didn't seem to like me removing listbox items from the page level. Said my reference wasn't instantiated. Do you think this is because the property I set in the user control was read only? E.g.

    Public ReadOnly Property ExpirationMonth() As DropDownList
        Get
            Return Me.ddlExpirationMonth
        End Get
    End Property


  #7 (permalink)  
Old June 16th, 2006, 07:22 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Aaron,

Not sure what's going wrong in your situation. Are you creating controls dynamically?? Here's what I did (and what works for me):

User Control Markup (Test.ascx)
Code:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="Test.ascx.vb" Inherits="MyControl" %>
<asp:DropDownList ID="DropDownList1" runat="server">
  <asp:ListItem>1</asp:ListItem>
  <asp:ListItem>2</asp:ListItem>
  <asp:ListItem>3</asp:ListItem>
  <asp:ListItem>4</asp:ListItem>
</asp:DropDownList>
User Control Code Behind (Test.ascx.vb)
Code:
Partial Class MyControl
  Inherits System.Web.UI.UserControl

  Public ReadOnly Property MyDropDown() As DropDownList
    Get
      Return Me.DropDownList1
    End Get
  End Property

End Class
Page with the control: (Test.aspx)
Code:
      <uc1:Test ID="Test1" runat="server" />
      <asp:Button ID="Button1" runat="server" Text="Button" /></div>
      where uc1:Test refers to my custom control called MyControl in the file Test.ascx.

Code behind of the page: (Test.aspx.vb)
Code:
Partial Class Test
  Inherits System.Web.UI.Page

  Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.Test1.MyDropDown.Items.RemoveAt(2)
  End Sub
End Class
When I run this page and click the button, one item is removed from the list.

Does this work for you?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing controls in host page from User Control Axe BOOK: Professional Web Parts and Custom Controls ASP.NET ISBN: 0-7645-7860-X 1 March 16th, 2011 06:22 AM
User control accessing problem tgnishant ASP.NET 2.0 Professional 1 May 3rd, 2007 08:52 AM
Accessing Properties in User Controls andyj00 ASP.NET 1.0 and 1.1 Professional 16 August 1st, 2005 07:55 AM
Accessing Properties in User Controls andyj00 Classic ASP Professional 1 May 21st, 2005 02:52 PM
Accessing Web User Controls from code aquaboltar ASP.NET 2.0 Basics 0 October 26th, 2004 04:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.