Kerberos
Kerberos

Reputation: 1256

How can i call class from aspx file?

i have a class which is in App_Code/Kerbooo.cs
i want to call that class's method from aspx file (not from code behind)
is it possible? if it is, how can i do?
thank you very much already now.

Upvotes: 1

Views: 7096

Answers (4)

RickNZ
RickNZ

Reputation: 18652

First, import the namespace that your code in App_Code uses:

<%@ Import Namespace="MyNamespace" %>

If your code isn't in a namespace yet, it's a good idea to put it in one.

Next, you can call your code either with <% code; %> or <%= code %>, depending on whether you want to write the results to the output stream or not.

Data binding, as in <%# %>, requires a little extra work, as do expressions in <%$ %>

Upvotes: 1

ram
ram

Reputation: 11626

And your method should be public if your aspx does not inherit from a class in codebehind

Upvotes: 0

Pharabus
Pharabus

Reputation: 6062

You can use <% %> and put your code in between (if you want to write stuff out <%= %> is a short cut for response.write but you need to do this outside of the <% %>

<%
 var bob = new Kerbooo();
..do stuff with class
%>

you can mix and match (this does lead to spaghetti code so be carefull) e.g looping

<table>
 <%
     var bob = new Kerbooo();
     foreach(var thing in bob.GetThings())
{
 %>
<tr><td><%=thing.StuffToWrite%><td></tr>
<%}%>
</table>

Upvotes: 0

Will
Will

Reputation: 1591

If the method is static, then the following should work within the aspx page:

<% Kerbooo.Method1(...) %>

If the method is not static, then you'll need an instance of Kerbooo:

<%
var kerbooo = new Kerbooo();
kerbooo.Method1(...)
%>

Upvotes: 1

Related Questions