Call .cs method/function from .ascx

Discussion in 'ASP .Net' started by trullock@googlemail.com, Aug 1, 2007.

  1. Guest

    Hi,

    Is there a way to call a codebehind method/function from an attribute
    of a runat=server tag, kinda like this:

    <asp:Literal runat="server" ID="litTest" Text='<%#
    MyMethod("testargs") %>' />

    but obviously that doesn't work :(

    Thanks

    Andrew
     
    , Aug 1, 2007
    #1
    1. Advertising

  2. <> wrote in message
    news:...

    > Is there a way to call a codebehind method/function from an attribute
    > of a runat=server tag, kinda like this:
    >
    > <asp:Literal runat="server" ID="litTest" Text='<%#
    > MyMethod("testargs") %>' />
    >
    > but obviously that doesn't work :(


    1) Make sure MyMethod returns a string

    2) Make sure MyMethod has either protected or public scope

    3) Change <%# to <%=


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
     
    Mark Rae [MVP], Aug 1, 2007
    #2
    1. Advertising

  3. wrote:
    > Hi,
    >
    > Is there a way to call a codebehind method/function from an attribute
    > of a runat=server tag, kinda like this:
    >
    > <asp:Literal runat="server" ID="litTest" Text='<%#
    > MyMethod("testargs") %>' />
    >
    > but obviously that doesn't work :(
    >
    > Thanks
    >
    > Andrew


    I prefer to put the code in the code behind:

    litTest.Text = MyMethod("testargs");

    --
    Göran Andersson
    _____
    http://www.guffa.com
     
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Aug 1, 2007
    #3
  4. Guest

    On 1 Aug, 13:13, "Mark Rae [MVP]" <> wrote:
    > <> wrote in message
    >
    > news:...
    >
    > > Is there a way to call a codebehind method/function from an attribute
    > > of a runat=server tag, kinda like this:

    >
    > > <asp:Literal runat="server" ID="litTest" Text='<%#
    > > MyMethod("testargs") %>' />

    >
    > > but obviously that doesn't work :(

    >
    > 1) Make sure MyMethod returns a string
    >
    > 2) Make sure MyMethod has either protected or public scope
    >
    > 3) Change <%# to <%=
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net


    Hi,

    I tried that but it just literally writes out: <%=
    MyMethod("testargs") %> into the resultant html.

    :(

    Goran, I cant do that for a complicated reason that i wont rabble on
    about here. I just need to be able to do it from the html, not the
    codebehind.

    Any other ideas anyone?

    Thanks
     
    , Aug 1, 2007
    #4
  5. <> wrote in message
    news:...

    Does this work:?

    <asp:placeHolder ID="litTest"
    runat="server"><%=MyMethod("testargs")%></asp:placeHolder>


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
     
    Mark Rae [MVP], Aug 1, 2007
    #5
  6. Teemu Keiski Guest

    This should work, if you just call litTest.DataBind() in code when you want
    it to be "called". <%# refers to a databinding expression when something
    must call DataBind() for the control for databinding to occur.

    --
    Teemu Keiski
    AspInsider, ASP.NET MVP
    http://blogs.aspadvice.com/joteke
    http://teemukeiski.net



    <> wrote in message
    news:...
    > Hi,
    >
    > Is there a way to call a codebehind method/function from an attribute
    > of a runat=server tag, kinda like this:
    >
    > <asp:Literal runat="server" ID="litTest" Text='<%#
    > MyMethod("testargs") %>' />
    >
    > but obviously that doesn't work :(
    >
    > Thanks
    >
    > Andrew
    >
     
    Teemu Keiski, Aug 1, 2007
    #6
  7. Guest

    On 1 Aug, 14:45, "Mark Rae [MVP]" <> wrote:
    > <> wrote in message
    >
    > news:...
    >
    > Does this work:?
    >
    > <asp:placeHolder ID="litTest"
    > runat="server"><%=MyMethod("testargs")%></asp:placeHolder>
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net



    'System.Web.UI.WebControls.Literal' does not allow child controls.

    :(

    Andrew
     
    , Aug 1, 2007
    #7
  8. Guest

    On 1 Aug, 15:49, "Teemu Keiski" <> wrote:
    > This should work, if you just call litTest.DataBind() in code when you want
    > it to be "called". <%# refers to a databinding expression when something
    > must call DataBind() for the control for databinding to occur.
    >
    > --
    > Teemu Keiski
    > AspInsider, ASP.NET MVPhttp://blogs.aspadvice.com/jotekehttp://teemukeiski.net
    >
    > <> wrote in message
    >
    > news:...
    >
    > > Hi,

    >
    > > Is there a way to call a codebehind method/function from an attribute
    > > of a runat=server tag, kinda like this:

    >
    > > <asp:Literal runat="server" ID="litTest" Text='<%#
    > > MyMethod("testargs") %>' />

    >
    > > but obviously that doesn't work :(

    >
    > > Thanks

    >
    > > Andrew


    Hi, Yeah i know i can call databind and use a # in the server tags,
    but i want to avoid any codebehind. (if im calling databind i might as
    well just do literal.text = "value";

    Thanks for the suggestion anyway :)
     
    , Aug 1, 2007
    #8
  9. <> wrote in message
    news:...

    >> Does this work:?
    >>
    >> <asp:placeHolder ID="litTest"
    >> runat="server"><%=MyMethod("testargs")%></asp:placeHolder>
    >>
    >> --
    >> Mark Rae
    >> ASP.NET MVPhttp://www.markrae.net

    >
    > 'System.Web.UI.WebControls.Literal' does not allow child controls.


    Once again - does this work:?

    <asp:placeHolder ID="litTest"
    runat="server"><%=MyMethod("testargs")%></asp:placeHolder>

    Please read carefully...


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
     
    Mark Rae [MVP], Aug 1, 2007
    #9
  10. Teemu Keiski Guest

    You can also do it on the aspx side if you use <script
    runat="server">...</script> block, no need to touch the code-behind ;-)



    --
    Teemu Keiski
    AspInsider, ASP.NET MVP
    http://blogs.aspadvice.com/joteke
    http://teemukeiski.net



    <> wrote in message
    news:...
    > On 1 Aug, 15:49, "Teemu Keiski" <> wrote:
    >> This should work, if you just call litTest.DataBind() in code when you
    >> want
    >> it to be "called". <%# refers to a databinding expression when something
    >> must call DataBind() for the control for databinding to occur.
    >>
    >> --
    >> Teemu Keiski
    >> AspInsider, ASP.NET
    >> MVPhttp://blogs.aspadvice.com/jotekehttp://teemukeiski.net
    >>
    >> <> wrote in message
    >>
    >> news:...
    >>
    >> > Hi,

    >>
    >> > Is there a way to call a codebehind method/function from an attribute
    >> > of a runat=server tag, kinda like this:

    >>
    >> > <asp:Literal runat="server" ID="litTest" Text='<%#
    >> > MyMethod("testargs") %>' />

    >>
    >> > but obviously that doesn't work :(

    >>
    >> > Thanks

    >>
    >> > Andrew

    >
    > Hi, Yeah i know i can call databind and use a # in the server tags,
    > but i want to avoid any codebehind. (if im calling databind i might as
    > well just do literal.text = "value";
    >
    > Thanks for the suggestion anyway :)
    >
     
    Teemu Keiski, Aug 1, 2007
    #10
  11. Guest

    On 1 Aug, 16:13, "Mark Rae [MVP]" <> wrote:
    > <> wrote in message
    >
    > news:...
    >
    > >> Does this work:?

    >
    > >> <asp:placeHolder ID="litTest"
    > >> runat="server"><%=MyMethod("testargs")%></asp:placeHolder>

    >
    > >> --
    > >> Mark Rae
    > >> ASP.NET MVPhttp://www.markrae.net

    >
    > > 'System.Web.UI.WebControls.Literal' does not allow child controls.

    >
    > Once again - does this work:?
    >
    > <asp:placeHolder ID="litTest"
    > runat="server"><%=MyMethod("testargs")%></asp:placeHolder>
    >
    > Please read carefully...
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net


    My bad, sorry.

    Yeah that works, but when i try and do it in an attribute it doesnt :(

    Thanks

    Andrew
     
    , Aug 1, 2007
    #11
  12. Teemu Keiski Guest

    Yup,

    because <%= %> is resolved when the Page is rendered, so it's target cannot
    be server-side property (which basically such attribute represents on server
    control)

    --
    Teemu Keiski
    AspInsider, ASP.NET MVP
    http://blogs.aspadvice.com/joteke
    http://teemukeiski.net

    <> wrote in message
    news:...
    > On 1 Aug, 16:13, "Mark Rae [MVP]" <> wrote:
    >> <> wrote in message
    >>
    >> news:...
    >>
    >> >> Does this work:?

    >>
    >> >> <asp:placeHolder ID="litTest"
    >> >> runat="server"><%=MyMethod("testargs")%></asp:placeHolder>

    >>
    >> >> --
    >> >> Mark Rae
    >> >> ASP.NET MVPhttp://www.markrae.net

    >>
    >> > 'System.Web.UI.WebControls.Literal' does not allow child controls.

    >>
    >> Once again - does this work:?
    >>
    >> <asp:placeHolder ID="litTest"
    >> runat="server"><%=MyMethod("testargs")%></asp:placeHolder>
    >>
    >> Please read carefully...
    >>
    >> --
    >> Mark Rae
    >> ASP.NET MVPhttp://www.markrae.net

    >
    > My bad, sorry.
    >
    > Yeah that works, but when i try and do it in an attribute it doesnt :(
    >
    > Thanks
    >
    > Andrew
    >
     
    Teemu Keiski, Aug 1, 2007
    #12
  13. wrote:
    > On 1 Aug, 16:13, "Mark Rae [MVP]" <> wrote:
    >> <> wrote in message
    >>
    >> news:...
    >>
    >>>> Does this work:?
    >>>> <asp:placeHolder ID="litTest"
    >>>> runat="server"><%=MyMethod("testargs")%></asp:placeHolder>
    >>>> --
    >>>> Mark Rae
    >>>> ASP.NET MVPhttp://www.markrae.net
    >>> 'System.Web.UI.WebControls.Literal' does not allow child controls.

    >> Once again - does this work:?
    >>
    >> <asp:placeHolder ID="litTest"
    >> runat="server"><%=MyMethod("testargs")%></asp:placeHolder>
    >>
    >> Please read carefully...
    >>
    >> --
    >> Mark Rae
    >> ASP.NET MVPhttp://www.markrae.net

    >
    > My bad, sorry.
    >
    > Yeah that works, but when i try and do it in an attribute it doesnt :(
    >
    > Thanks
    >
    > Andrew
    >


    Why do you have to put it in a Literal control? Just do:

    <%=MyMethod("testargs")%>

    --
    Göran Andersson
    _____
    http://www.guffa.com
     
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Aug 1, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Quentin
    Replies:
    1
    Views:
    503
    Quentin
    Jul 29, 2003
  2. Jason Shohet
    Replies:
    1
    Views:
    492
    Kevin Spencer
    Nov 10, 2003
  3. Holger (David) Wagner
    Replies:
    2
    Views:
    1,959
    Holger (David) Wagner
    Jul 3, 2004
  4. Joe
    Replies:
    1
    Views:
    390
    Jeffrey Tan[MSFT]
    Feb 16, 2004
  5. Replies:
    2
    Views:
    548
Loading...

Share This Page