MVC Html Layout C# code formatting - asp.net

I insert into asp.net mvc views C# logic that manages layout like the following:
<% if (Model.People.Count > 0 ) { %>
<% foreach (var person in Model.People) { %>
...
<% }} else { %>
<span class="error">Sorry, no people</span>
<%} %>
I try to minimize <% %> code placing "{" symbol on the same line as it's condition (java-style). Html layout looks more clear to me after that.
Do you apply C# formatting rules to <% %> html injections "}" should be on a new line or manage layout in different way?
Thank you in advance!

Its totally up to you, whatever you find is more readable and maintainable.
The less inline server blocks you have the better though (in terms of preventing run-time code compilation errors).

Related

How to prevent blank page getting added at the end of pdf report due to page break

I am printing the pdf report where i will be looping each student and i am applying page-break after each student
And my applied page break code is as below
<style type="text/css" >
.page-break { display:block; clear:both; page-break-after: always;}
</style>
And my view is
<% #students.each do |student| %>
<%= student.first_name %>
<div class="page-break"></div>
<% end %>
Problem is i am getting empty page at last in pdf report, i am using wicked-pdf gem for pdf generation i think problem is with page-break css can anyone help me
Quick and dirty solution, but should work fine:
<% #students.each do |student| %>
<%= student.first_name %>
<% if student.id != #students.last.id %>
<div class="page-break"></div>
<% end %>
<% end %>
Also if you can show us the html container of the each loop, we can give a better answer depending on this one.
Just dont add the class that gives page break on your last loop, that way "page-break-after: always" wont take effect

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.

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

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

Resources