How can i call class from aspx file? - asp.net

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.

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(...)
%>

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 <%$ %>

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>

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

Related

What is the difference between RenderPartialExtensions.RenderPartial and DisplayExtensions.DisplayFor

I want to have an "dynamic" table ascx:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<% foreach (var element in Model.Elements) { %>
<%= Html.DisplayFor(m => element) %>
<% } %>
with model:
public class TableViewModel<ElementType> {
public List<ElementType> Elements {get;set;}
}
first strangely enough I cannot write something like this in ascx directives:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl>" %>
then the second problem - I cannot use DisplayFor function extension, because compiler cannot deduce HtmlHelper<> generic param, because the ascx model is dynamic. That is why I had to replace the:
<%= Html.DisplayFor(m => element) %>
with the:
<% RenderPartialExtensions.RenderPartial(Html, "Table", element); %>
So my questions are am I doing something wrong, or if I'm not what is the difference between DisplayFor and RenderPartial in terms of performance?
Thanks in advance.
May be you could try to use just Display?
RenderPartial just renders partial view. DisplayFor uses UIHint or DisplayTemplate from Shared views for rendering.

script tags in html

I'm having difficulties google what's the difference between the following script tags:
<%# ... %>
<% = ... %>
<% ... %>
Can someone help?
These tags also may be ASP.NET tags.
Here are links with the information about each tag:
<%# ... %> Data-Binding Expression Syntax
<%= ... %> Displaying from ASP.NET
<% ... %> Embedded Code Blocks in ASP.NET Web Pages
i don't know about the 1st one
but 2 and 3 was the jsp tag,
2 one is the expression tag like when you want to get value from jsp variable then you can use this tag
for e.g.
String arg = "Pratik"
now you want use this in jsp page anywhere
Hello <%= name %> ////// it will print on web page as Hello Pratik
the 3rd one is the script tag
when you want write block of jsp,java code you can write within this tag
for ex.
<%
String name="";
name = "abc";
out.println(name);
%>

How to make If statements in Databound ListView

I have a ListView with many advanced controls and html tags. ListView is bound to collection of profiles when first profile in collection is current profile. current profile has few differences from other profiles ie. flash embed, js and some other stuff. I can access inside of my ListView Container.DataIndex property which gives me 0 as first item in index but i'm unable to use inline If statements like so
<% If Container.DataIndex = 0 Then %>
do stuff
<% EndIf %>
this is because i must place pound to access databound item but neither this
<%# If Container.DataIndex = 0 Then %>
do stuff
<% EndIf %>
How can i make inline If ?
Try this:
<% if (DataBinder.Eval(Container, "DataItemIndex")) { %>
do stuff
<% } else { %>
do other stuff
<%} %>
Here is a small summary of the inline aspx tags:
http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-%283c25242c-3c253d2c-3c252c-3c252c-etc%29.aspx
But i would recommend to use ListView.ItemDataBound. It is less error-prone and more readable in codebehind.
Did you mean IIF? IIF - Returns one of two objects, depending on the evaluation of an expression.

Html helper extension works if I do <%= .. %> but not if I do <% Html.MyExt(); %>

Why does my html helper extension work if I do:
<%= Html.MyExt() %>
all mvc helpers work like:
<% Html.TextBox(""); %>
My extension builds a StringBuilder, then returns a string.
This helper must return string (ASP.NET MVC 1.0) or MvcHtmlString (ASP.NET MVC 2.0) which is written to the response stream (using Response.Write):
<%= Html.MyExt() %>
This helper returns nothing (void) it simply executes the extension method:
<% Html.TextBox(""); %>
<%= command %> runs the command and prints the returned string. The command must return a string.
<% command; %> just runs the command. Anything returned by the command will be ignored.
Most HTML helpers I've seen use the first format.

What is the difference between <% %> and <%= %>?

I tried to find the difference on Google.
BUT
I 'm not able to search with '<% %>' , maybe the reason is <% is a HTML TAG
Now i'm thinking there's no diffrence betwwen <% and <%= .
<% %> executes the code between the 2 brackets.
<%= %> returns the value between the 2 brackets.
Example:
<% Response.Write("Hello.") %>
vs
<%= "Hello" %>
<% %> and <%= %> are normally server side scripts, the difference is first one does not print out the value to the page, unless you explicitly use print function, but second one will do automatically
Are you talking about the ASP ? If yes then <% %> is to hold the server side code and this is <%= %> equivalent to the Response.Write().
They're generally referred to as beestings. These particular ones are used by ASP.Net or ASP Classic. <% %> signifies server side code and <%=<Something%> is shorthand for <% Response.Write(<Something>) %>
If you want to show current date in a page you can do either of the following to write the date to the document. In the first sample using <% %> you have to explicitly use Response.Write.
<% Response.Write(DateTime.Now.ToString()) %>
and in the following one no need to explicitly write Response.Write
<%= DateTime.Now.ToString() %>

Resources