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!

Referencing user control within user control from web form

Status
Not open for further replies.

sarahnice

Technical User
Nov 18, 2002
101
IE
Hi all,

I have an aspx page (main.aspx), which contains a user control menu.ascx, which in turn contains another user control poll.ascx.

The code behind for the poll.ascx file works fine to a point. I want to call a function in this file depending on which of two buttons has fired the postback. My form is on the main.aspx page. In the code for the main.aspx file, I want to determine the button which caused the postback (not the problem here) and then call a function in the poll.ascx code behind file.

I am trying to do something like this:

Code:
Protected WithEvents menuctrl as menuControl
...
menuctrl.poll.button1click()

The class name of the menu user control is menuControl.
In the code behind of the menuControl I simply have a reference to the poll.ascx class.

I keep getting the following error in the main.aspx file:

Code:
Type 'menuControl' is not defined.

Any ideas?
 
If you have placed your user control on your aspx page e.g.
Code:
<uc1:WebUserControl1 id="MyUserControl" runat="server"></uc1:WebUserControl1>
Simply add a public reference to it e.g.
Code:
Public WithEvents MyUserControl As WebUserControl1
Then, you should be able to access any public functions from the user control e.g.
Code:
MyUserControl.MyFunction()


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I've tried that but keep getting the type not defined error. If I change the type to UserControl, ie

Code:
Public WithEvents MyUserControl As UserControl

then I can get it compile but can't access any of the functions in that control because they are not a member of 'System.Web.UI.UserControl'

Do I need to declare namespaces and then import those namespace in the aspx code behind file?
 
You shouldn't change the type to "As UserControl" because it isn't a UserControl in the sense that you User Control has inherited the UserControl type. If you have placed your user control in a namespace, you should just add the namespace before the name of the user control e.g.
Code:
Public WithEvents MyUserControl As MyNameSpace.WebUserControl1


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I haven't declared any namespaces for any of the code behind files.

I just wanted to know if I should have.
 
I find that it's good practice to use namespaces but you don't have to use them.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Also, make sure you have registered the user control on the aspx page e.g.
Code:
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebUserControl1.ascx" %>


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I have registered the user control and it shows up fine on the aspx page. The problem arises if I try to access any of the functions in the ascx.vb file from the aspx.vb code behind file.

I have declared the class for the user control as public and the functions i'm trying to access is also public.

This is starting to drive me up the wall.
 
OK, can you paste your code for

1) The user control
2) The aspx page (ie. the HTML)
3) The aspx code


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
All of this so far really doesn't do anything. I'm just trying to get the problem of referencing a user control to work.

main.aspx:
Code:
<%@ Page language="VB" debug="true" Inherits="MainControl" Src="~/codeBehind/main.aspx.vb" %>
<%@ Register TagPrefix="Components" TagName="Right" Src="includes/right.ascx" %>

<html>
<head>
  <link href="css/right_tab_frame.css" rel="stylesheet" type="text/css">
</head>
<body>
 <form name="mainForm" runat="server">
  <table width="100%"  border="0" cellspacing="0" cellpadding="0">
   <tr>
    <td>
     <Components:Right id="RightFrame" runat="server" />
    </td>
   </tr>
  </table>
 </form>
</body>
</html>

main.aspx.vb:
Code:
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls

Public Class MainControl : Inherits Page
 Protected WithEvents RightFrame as Right 
 Sub Page_Load(sender as Object, e as EventArgs)
  If IsPostBack then
   RightFrame.castVote()
  End If
 End Sub
End Class

right.ascx:
Code:
<%@ Control Language="vb" Debug="true" Inherits="RightComponents.rightUserControl" Src="~/codeBehind/content/right.ascx.vb" %>

<table width="200" border="0" cellpadding="0" cellspacing="0">
 <tr>
  <td>
  </td>
 </tr>
</table>

right.ascx.vb:
Code:
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls

Public Class rightUserControl : Inherits UserControl

 Protected WithEvents RightComponents_OpinionPoll as myUserControl
 Public Sub test()
  Dim aaa as String
  aaa = "test"
 End Sub
End Class
 
The majority of your code looks to be correct but you don't seem to be calling the public sub called test from anywhere (you appear to be calling something called castVote which I haven't seen a reference to).

Here's a simple example of a webcontrol that you could try to implement and extend to your needs:

On a new aspx page add:
Code:
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebUserControl1.ascx" %>
and in the code behind for that page add:
Code:
    Protected WithEvents MyUserControl As WebUserControl1

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        MyUserControl.MyFunction()
    End Sub
Then, add a user control named WebUserControl1 and add this to it's code-behind:
Code:
    Public Function MyFunction()
        Response.Write("Hello, I Fired!!")
    End Function

If you can get this simple example working (I would suggest doing this first) then you can extend the user control to display the relevant section that you need for your poll.




____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I've tried the sample you gave me but I get the following error:

Code:
Compiler Error Message: BC30001: Statement is not valid in a namespace.
 
Sorry, here is the error i'm getting

Code:
Compiler Error Message: BC30002: Type 'WebUserControl1' is not defined.
 
Is the User Control in the same folder as the aspx page?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Sorry, I missed out the following step from my sample which was to add the following to the aspx page:
Code:
<uc1:WebUserControl1 id="MyUserControl" runat="server"></uc1:WebUserControl1>
Add that if you haven't already.



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Had already added that line but still the same error.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top