Access User Control Public Method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stormcandi
    New Member
    • Apr 2007
    • 12

    Access User Control Public Method

    I have a method in a user control which I need for the Page to be able to access. I have set the method to public so the page should be able to access it, but I am still unable to get to it.

    Can anyone assist? I am really new at using user controls.
    Thanks in advance!

    Candi
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    So you're accessing it from your page in this type of fashion?
    Code:
    MyUserControlInstance.MyPublicMethod()
    So long as the user control called MyUserObject exists on your page and has a public sub/function called MyPublicMethod, you should be able to access it. Have you added your user control to the page? You won't be able to access the public method unless the object exists on your page - much the same way you can't access the properties and methods of a textbox if you haven't created it on your page yet.

    I have created the following simple user control...it doesn't contain any HTML, formatting etc...
    User Control Code:
    Code:
    Public Partial Class MyUserControl
      Inherits System.Web.UI.UserControl
      
      Public Sub WriteValue(ByVal DataToWrite As String)
    	Response.Write(DataToWrite)
      End Sub
     
    End Class
    In my main page (in design view), I dragged my user control onto the page and it created the following HTML
    [Code=HTML]<%@ Page Language="vb" AutoEventWireup ="false" CodeBehind="Def ault.aspx.vb" Inherits="WebAp plication1._Def ault" %>
    <%@ Register Src="MyUserCont rol.ascx" TagName="MyUser Control" TagPrefix="uc1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitl ed Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div id="content" runat="server">
    <uc1:MyUserCont rol id="MyUserContr ol1" runat="server">
    </uc1:MyUserContr ol></div>
    </form>
    </body>
    </html>[/Code]
    In the code behind for the main page, I added the following code:
    Code:
    Partial Public Class _Default
    	Inherits System.Web.UI.Page
    	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    		MyUserControl1.WriteValue("Hello World")
    	End Sub
    End Class
    The output is a page that displays "Hello World"

    Hope that helps...

    Comment

    • stormcandi
      New Member
      • Apr 2007
      • 12

      #3
      I have added the control on the page and it will build without errors. So I am good there.

      The part I cannot get to work is in the codebehind where I type the usercontrolname .methodname, but to no avail. I am frustrated because it is probably just one little thing I am overlooking.

      Originally posted by balabaster
      So you're accessing it from your page in this type of fashion?
      Code:
      MyUserControlInstance.MyPublicMethod()
      So long as the user control called MyUserObject exists on your page and has a public sub/function called MyPublicMethod, you should be able to access it. Have you added your user control to the page? You won't be able to access the public method unless the object exists on your page - much the same way you can't access the properties and methods of a textbox if you haven't created it on your page yet.

      I have created the following simple user control...it doesn't contain any HTML, formatting etc...
      User Control Code:
      Code:
      Public Partial Class MyUserControl
        Inherits System.Web.UI.UserControl
        
        Public Sub WriteValue(ByVal DataToWrite As String)
      	Response.Write(DataToWrite)
        End Sub
       
      End Class
      In my main page (in design view), I dragged my user control onto the page and it created the following HTML
      [Code=HTML]<%@ Page Language="vb" AutoEventWireup ="false" CodeBehind="Def ault.aspx.vb" Inherits="WebAp plication1._Def ault" %>
      <%@ Register Src="MyUserCont rol.ascx" TagName="MyUser Control" TagPrefix="uc1" %>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" >
      <head runat="server">
      <title>Untitl ed Page</title>
      </head>
      <body>
      <form id="form1" runat="server">
      <div id="content" runat="server">
      <uc1:MyUserCont rol id="MyUserContr ol1" runat="server">
      </uc1:MyUserContr ol></div>
      </form>
      </body>
      </html>[/Code]
      In the code behind for the main page, I added the following code:
      Code:
      Partial Public Class _Default
      	Inherits System.Web.UI.Page
      	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      		MyUserControl1.WriteValue("Hello World")
      	End Sub
      End Class
      The output is a page that displays "Hello World"

      Hope that helps...

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        Originally posted by stormcandi
        I have added the control on the page and it will build without errors. So I am good there.

        The part I cannot get to work is in the codebehind where I type the usercontrolname .methodname, but to no avail. I am frustrated because it is probably just one little thing I am overlooking.
        I sometimes find that Visual Studio gets hung up on itself and doesn't rebuild the designer.vb code properly. Quite likely it's done this - select the "Show all files" button at the top of your solution explorer to make sure you're viewing all files and then expand the + next to your page name. Open the designer.vb pertaining to your page and check that nothing is amiss in there. For instance, if you've changed the name of the instance of your user control and the designer hasn't updated it properly. This would prevent it being able to find the method as it's not finding the instance of the user control. The other thing is if you've added the control in code, if you've not instantiated it properly, then the intellisense doesn't work.

        When you say you don't have access to the public method - what exactly is the situation you're seeing? Can you describe it? Do you see the intellisense for your instance but your public method is missing? Do you not see any of the intellisense for your user control? Maybe this will give me a better understanding of what exactly is causing the issue.

        Comment

        Working...