Call .cs method/function from .ascx

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
 
M

Mark Rae [MVP]

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 <%=
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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");
 
T

trullock

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
 
M

Mark Rae [MVP]

Does this work:?

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

Teemu Keiski

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

trullock

Does this work:?

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


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

:(

Andrew
 
T

trullock

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 :)
 
M

Mark Rae [MVP]

'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...
 
T

trullock

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
 
T

Teemu Keiski

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)
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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")%>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top