Call .cs method/function from .ascx

  • Thread starter Thread starter trullock
  • Start date Start date
T

trullock

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
 
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 <%=
 
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");
 
1) Make sure MyMethod returns a string

2) Make sure MyMethod has either protected or public scope

3) Change <%# to <%=

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
 
Does this work:?

<asp:PlaceHolder ID="litTest"
runat="server"><%=MyMethod("testargs")%></asp:PlaceHolder>
 
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.
 
Does this work:?

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


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

:(

Andrew
 
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.

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 :)
 
'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...
 
Once again - does this work:?

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

Please read carefully...

My bad, sorry.

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

Thanks

Andrew
 
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)
 
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")%>
 
Back
Top