Calling a Public Function from the ASPX page

Last post 04-03-2009 6:57 PM by belcherman. 12 replies.

Sort Posts:

  • Calling a Public Function from the ASPX page

    04-07-2008, 8:46 PM
    • Member
      4 point Member
    • quizwedge
    • Member since 12-18-2007, 7:10 PM
    • Posts 15

    I know I did this in Visual Studio 2003 (ASP.NET 1.1), but can't seem to do it in Visual Studio 2005 (ASP.NET 3.5?). I've got a page that's part of a master page.  The project is an AJAX project.  From the aspx page, I'm trying to call myFunction(ID) with the following: <%# myFunction(ID) %>.  On the codebehind page, I have the following as part of the partial public class:

     

        Public Function myFunction(ByVal ID As Integer) As String
            Return ID.ToString
        End Function
     

    My function actually does more, but I figured I'd keep things simplified.  Visual Studio 2005 says "Name 'myFunction' is not declared".  What am I doing wrong?  This worked great in Visual Studio 2003.

     

    Thanks,

    Dan 

    Filed under:
  • Re: Calling a Public Function from the ASPX page

    04-07-2008, 9:59 PM
    • Participant
      1,273 point Participant
    • RJA
    • Member since 07-09-2003, 12:26 PM
    • Pittston, Pa
    • Posts 206

     Check to see where you are calling myFunction from - the content page or master page and where's it defined, in the content page or master page?  If myFunction is defined in the master page you may need to use the Me.Master property to access it and call it.

    Rachel Appel
    MVP - ASPInsider
  • Re: Calling a Public Function from the ASPX page

    04-08-2008, 3:45 AM
    • Member
      4 point Member
    • quizwedge
    • Member since 12-18-2007, 7:10 PM
    • Posts 15

     It's in the content page as part of the page's class.

  • Re: Calling a Public Function from the ASPX page

    04-08-2008, 8:10 PM
    • Member
      4 point Member
    • quizwedge
    • Member since 12-18-2007, 7:10 PM
    • Posts 15

    Just to clarify, the function is in the content page codebehind.  The function is called from the content page as well.  Thanks.

  • Re: Calling a Public Function from the ASPX page

    04-08-2008, 8:43 PM
    Answer
    • Participant
      1,273 point Participant
    • RJA
    • Member since 07-09-2003, 12:26 PM
    • Pittston, Pa
    • Posts 206

     The code you've shown should work - it looks good.  I've created a Master Page w/a content page and a myFunction as you've described to test it out and it works great.  Is the ID parameter set somewhere else or used in a data binding expression?  Perhaps if you can post the rest of the functions code if possible that might point toward what the problem is.

    Rachel Appel
    MVP - ASPInsider
  • Re: Calling a Public Function from the ASPX page

    04-08-2008, 8:57 PM
    • Member
      4 point Member
    • quizwedge
    • Member since 12-18-2007, 7:10 PM
    • Posts 15

    Weird... here's the entire code of a sample page that's erroring... just in case I missed something (I only changed the main project name to MyProject for security reasons).  The solution has two projects, UserInterface and BusinessLogic.  The BusinessLogic project is just a bunch of classes.

    ----ASPX page 

    <%@ Page Language="vb" AutoEventWireup="false" MasterPageFile="~/MyProject.Master" CodeBehind="zTestForForum.aspx.vb" Inherits="UserInterface.zTestForForum"
        title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <%#f_MyFunction(1)%>
    </asp:Content>

     ----CodeBehind

    Public Partial Class zTestForForum
        Inherits System.Web.UI.Page

        Public Function f_MyFunction(ByVal ID As Integer) As String
            Return ID.ToString
        End Function

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        End Sub

    End Class

     

    Thanks,

    Dan 

  • Re: Calling a Public Function from the ASPX page

    04-09-2008, 6:05 AM

    quizwedge:

    Weird... here's the entire code of a sample page that's erroring... just in case I missed something (I only changed the main project name to MyProject for security reasons).  The solution has two projects, UserInterface and BusinessLogic.  The BusinessLogic project is just a bunch of classes.

    ----ASPX page 

    <%@ Page Language="vb" AutoEventWireup="false" MasterPageFile="~/MyProject.Master" CodeBehind="zTestForForum.aspx.vb" Inherits="UserInterface.zTestForForum"
        title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <%#f_MyFunction(1)%>
    </asp:Content>

     ----CodeBehind

    Public Partial Class zTestForForum
        Inherits System.Web.UI.Page

        Public Function f_MyFunction(ByVal ID As Integer) As String
            Return ID.ToString
        End Function

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        End Sub

    End Class

     

    Thanks,

    Dan 

     

    Hi,

    The expression inside <%# %> will be executed only when DataBind method is called. The expression inside <%= %> will be executed when it appears in the page.

    In your case, you can use the <%=f_MyFunction(1) %>.

     

    I hope this helps.

    Thomas Sun
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
  • Re: Calling a Public Function from the ASPX page

    04-09-2008, 6:50 PM
    • Member
      4 point Member
    • quizwedge
    • Member since 12-18-2007, 7:10 PM
    • Posts 15

     I tried switching from # to = and still got the Name 'f_MyFunction' is not declared.

  • Re: Calling a Public Function from the ASPX page

    04-09-2008, 10:39 PM
    Answer

    Hi,

    Thanks for your response.

    Does the codebehind class have a namespace? If yes, we need to add the namespace in the Inherits section of Page directive. In your case, I cannot see the namespace in the codebehind, but you use a namespace in the Inherits section of this Page directive. For more information about Page directive, see http://msdn2.microsoft.com/en-us/library/ydy4x04a(VS.80).aspx

     

    I hope this helps.

     

    Thomas Sun
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
  • Re: Calling a Public Function from the ASPX page

    04-10-2008, 6:21 PM
    • Member
      4 point Member
    • quizwedge
    • Member since 12-18-2007, 7:10 PM
    • Posts 15

    okay, that's bizarre... now it's working fine.  Oh, well, I guess I just shouldn't question it. :)  At least I know that what I did is correct.

    Thanks,

    Dan 

  • Re: Calling a Public Function from the ASPX page

    04-17-2008, 2:48 PM
    • Member
      2 point Member
    • rathergolf
    • Member since 04-17-2008, 6:44 PM
    • Posts 3

    I'm running into this same thing a LOT!  I have a web site project that was created in VS05 and runs great. I've tried to move some of the pages to a VS08 project and I get the "Function Not Delcared" error. I also periodically get an error that a variable hasn't been declared in the code behind the page for a simple label that exists in the page.  What is the deal with VS???  Has anyone discovered why this happens??

  • Re: Calling a Public Function from the ASPX page

    07-08-2008, 12:16 PM

    Make sure that you are calling Page.DataBind().

    cheers 

  • Re: Calling a Public Function from the ASPX page

    04-03-2009, 6:57 PM
    • Member
      29 point Member
    • belcherman
    • Member since 11-03-2006, 9:18 AM
    • Posts 61
    \n"; LiteralWelcome.Text += "\n"; LiteralWelcome.Text += "\n"; LiteralWelcome.Text += "\n"; LiteralWelcome.Text += "\n"; LiteralWelcome.Text += "\n"; } } And form admin.aspx I am trying to call this function. I am not sure how to do that or if you can. I know this SiteName(sender,e); will not work because the function is in the header.ascx code behind. Is it possible to do this. Call a function in a included user controls code behind? Header.ascx is a webusercontrol in admin.aspx
    Old post but I have the same situation. I am trying to call a function in my code behind form a aspx page. The thing is that this page is using inline coding module as opposed to code behind. I have a header.ascx file that has the function: void SiteName(Object sender, EventArgs e) { if (string.Compare(G_site.name, string.Empty) != 0) { LiteralWelcome.Text = ""; LiteralWelcome.Text += "
    Welcome!
    " + G_site.name + "
    \n"; Response.Write("
    " + G_site.name + "
    \n"); if (string.Compare(G_site.site_id, string.Empty) != 0) { LiteralWelcome.Text += "(" + G_site.site_id + ")"; } else if (string.Compare(G_site.consortia_id, string.Empty) != 0) { LiteralWelcome.Text += "(" + G_site.consortia_id + ")"; } LiteralWelcome.Text += "

Page 1 of 1 (13 items)