asp.net code displays code itself instead of output in browser - asp.net

The following code displays as it is in browser instead of expected output. I have the table in sql server 2008. I have this code in webmatrix.
<%# Page Language="VB" %>
#{
var db = Database.Open("databasename"); 'database name in sql server is 'databasename'
var selectQueryString = "SELECT column_date, sum(qty1) as quantity1,
sum(qty2) as quantity2, sum(qty3) as quantity3
from tbldaily group by column_date order by column_date";
}
<!DOCTYPE html>
<html>
<body>
<h1>First table test</h1>
<table>
<tr>
<th>at_date</th>
<th>quantity1</th>
<th>quantity2</th>
<th>quantity3</th>
</tr>
#foreach(var row in db.Query(selectQueryString))
{
<tr>
<td>#row.column_date</td>
<td>#row.quantity1</td>
<td>#row.quantity2</td>
<td>#row.quantity3</td>
</tr>
}
</table>
</body>
</html>
I feel there is mistake somewhere in this code. Can you help me to fix this? Please let me know if you want more information regarding my database structure or table that may help to understand my scenario better

Looks like you are mixing Razor syntax with WebPages. Name your file *.cshtml and remove the first line which is for WebPages. The rest of your code is valid C# razor.

I think he needs the opposite of that. Keep it .aspx.
So wherever you have # tags to mark the asp code, use aspx escapes instead
<% For i As Integer = 0 To .... %>
<tr> <td> <%= row.column_date %> </td> ......
<% Next%>

Related

asp.net/vb webpage not redirecting

I've got some legacy websites that have been migrated to Windows Server 2019 and have found out that the code no longer seems to be functioning correctly. This is code for a simple poll/voting system. The problem that I'm currently running into is that when any of the links are clicked, it technically doesn't redirect from the page it's on. for example the PollList.aspx page (www.mysite/admin/PollList.aspx) has a few links on it. one of them is to load up the page PollEdit.aspx so a new poll can be added. the URL it's supposed to redirect to is www.mysite/admin/PollEdit.aspx?ID=0 (new poll only created by admins). however what it redirects to is: www.mysite/admin/PollList.aspx/PollEdit.aspx?ID=0 and stays on the current page.
Can someone show me what's broken? This was migrated from a Windows Server 2008r2 to Windows Server 2019. Let me know if there's any info needed.
below is the related content from the PollList.aspx page. everything else seems to be working just fine:
<form id="Form1" method="post" runat="server">
Edit Voter Permissions
<h1><center>Polls</center></h1>
<asp:repeater id="lstPolls" runat="server">
<HeaderTemplate>
<%= "<table border=0 cellpadding=0 cellspacing=4 align=center width=80% >" %>
<%= "<tr><td></td><td align=center><font size=2><a href=PollEdit.aspx?ID=0>[new poll]</a></font></td></tr>" %>
</HeaderTemplate>
<ItemTemplate>
<%="<tr bgcolor=#B3C9EF>"%>
<%# PrintPollItem(Container.DataItem)%>
<%= "</tr>" %>
</ItemTemplate>
<AlternatingItemTemplate>
<%= "<tr bgcolor=""#DFEBFF"">" %>
<%# PrintPollItem(Container.DataItem)%>
<%= "</tr>" %>
</AlternatingItemTemplate>
<FooterTemplate><%= "</table>" %></FooterTemplate>
</asp:repeater>
</form>
and below is the code behind content for the PrintPollItem() func:
Protected Function PrintPollItem(ByVal myPoll As BLL.Poll) As String
Dim txtTemp As New System.Text.StringBuilder
txtTemp.Append("<td>")
txtTemp.Append("<a href=PollEdit.aspx?ID=" & myPoll.ID.ToString() & ">")
txtTemp.Append(myPoll.Name)
txtTemp.Append("</a>")
txtTemp.Append("</td>")
txtTemp.Append("<td width=200 align=center>")
Select Case myPoll.OpenStatus
Case -1
txtTemp.Append("poll closed: <font size=2>[view results]</font>")
Case 1
txtTemp.Append("poll open: <font size=2>[view results]</font>")
Case Else
txtTemp.Append("poll pending: <a href=PollList.aspx?ID=" & myPoll.ID.ToString() & "&Action=open><font size=2>[open now]</font></a>")
End Select
txtTemp.Append("</td>")
Return txtTemp.ToString()
End Function
href you can give full path or relative path
you can use / or ../ or ~/ Before Assigning Link, That indicates current Files or Folder
Try href=/PollEdit.aspx

Using Request.QueryString in ASP.NET Embedded Code Block

I am attempting to pass a parameter from one file to another via the URL after a button is clicked. These are written with Express.js (index.ejs to items.ejs).
As it stands currently I am setting the URL parameter in a defined Javascript function:
function loadItems(page, subcategory) {
window.history.pushState(null, null, "?subcat=" + subcategory) //param set
$('#mainContent').load(page);
}
where subcategory is the changing variable.
From there I am trying to read this parameter during an ASP.NET function written in embedded code blocks.
<% if(items[i].subcategory === Request.QueryString["subcat"].Value) { %> //get param
<% if (items[i].status === "Supported") { %>
<tr class="success">
<td>Edit</td>
<td id="item name"><%= items[i].name%></td>
<td id="subcat name"><%= items[i].subcategory%></td>
<td id="item status"><%= items[i].status%></td>
<td id="item desc"><%= items[i].description%></td>
</tr>
However I am met with an error which states Request is not defined and a callback to the above if statement. It is my understanding that on the ASP.NET side of things, Request.QueryString is a part of System.Web.HttpContext.Current.
How would I go about including this into my code blocks so that I am able to pull the parameter from the URL? Or, if this is not the way to be looking at this problem, how should I go about it?
My advice would be to use code behind. Embedded code blocks are an old-school throwback from the asp days. But if you must, then you should be able to do something like this:
<%# Page Language="VB" %>
<script run=server>
Protected Function GetSubcat() As String
Return Request.QueryString["subcat"].Value
End Function
</script>
<form id="form1" runat="server">
Subcat value is <% =GetSubcat()%>.
</form>

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

MVC Html Layout C# code formatting

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

Resources