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
Mark Rae [MVP] - 01 Aug 2007 13:13 GMT
> Is there a way to call a codebehind method/function from an attribute
> of a runat=server tag, kinda like this:
[quoted text clipped - 3 lines]
>
> 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 <%=

Signature
Mark Rae
ASP.NET MVP
http://www.markrae.net
trullock@googlemail.com - 01 Aug 2007 13:34 GMT
> <trull...@googlemail.com> wrote in message
>
[quoted text clipped - 17 lines]
> 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
Mark Rae [MVP] - 01 Aug 2007 14:45 GMT
Does this work:?
<asp:PlaceHolder ID="litTest"
runat="server"><%=MyMethod("testargs")%></asp:PlaceHolder>

Signature
Mark Rae
ASP.NET MVP
http://www.markrae.net
trullock@googlemail.com - 01 Aug 2007 15:53 GMT
> <trull...@googlemail.com> wrote in message
>
[quoted text clipped - 8 lines]
> Mark Rae
> ASP.NET MVPhttp://www.markrae.net
'System.Web.UI.WebControls.Literal' does not allow child controls.
:(
Andrew
Mark Rae [MVP] - 01 Aug 2007 16:13 GMT
>> Does this work:?
>>
[quoted text clipped - 6 lines]
>
> '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...

Signature
Mark Rae
ASP.NET MVP
http://www.markrae.net
trullock@googlemail.com - 01 Aug 2007 16:35 GMT
> <trull...@googlemail.com> wrote in message
>
[quoted text clipped - 21 lines]
> 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 - 01 Aug 2007 17:54 GMT
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)

Signature
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
>> <trull...@googlemail.com> wrote in message
>>
[quoted text clipped - 29 lines]
>
> Andrew
Göran Andersson - 01 Aug 2007 21:08 GMT
>> <trull...@googlemail.com> wrote in message
>>
[quoted text clipped - 25 lines]
>
> Andrew
Why do you have to put it in a Literal control? Just do:
<%=MyMethod("testargs")%>

Signature
Göran Andersson
_____
http://www.guffa.com
Göran Andersson - 01 Aug 2007 13:22 GMT
> Hi,
>
[quoted text clipped - 9 lines]
>
> Andrew
I prefer to put the code in the code behind:
litTest.Text = MyMethod("testargs");

Signature
Göran Andersson
_____
http://www.guffa.com
Teemu Keiski - 01 Aug 2007 15:49 GMT
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.

Signature
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
> Hi,
>
[quoted text clipped - 9 lines]
>
> Andrew
trullock@googlemail.com - 01 Aug 2007 15:54 GMT
> 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
[quoted text clipped - 21 lines]
>
> > 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 - 01 Aug 2007 16:13 GMT
You can also do it on the aspx side if you use <script
runat="server">...</script> block, no need to touch the code-behind ;-)

Signature
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
>> This should work, if you just call litTest.DataBind() in code when you
>> want
[quoted text clipped - 29 lines]
>
> Thanks for the suggestion anyway :)