Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / August 2007

Tip: Looking for answers? Try searching our database.

Call .cs method/function from .ascx

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
trullock@googlemail.com - 01 Aug 2007 12:48 GMT
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 :)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.