asp.net/vb webpage not redirecting - asp.net

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

Related

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>

Classic ASP Open directory on server

I want to open a directory, that sits on a server, through a link.
My problem is that it works perfectly in an .htm page but not in an .asp page.
Here is what i use:
Foldername
I have tried using 3 or 5 slashes after the file: but the same result.
A different thing i found without result is:
Link
After trying to figure it out in html i tried the following in asp:
<%
str_url="file://server/folder/folder"
Response.Write("<script>")
Response.Write("window.open('" & str_url & "', 'myWin','height=800,width=1024,status=yes,toolbar=yes,menubar=yes,location=yes,resizable=yes,scrollbars=yes');")
Response.Write("</script>")
%>
But after several attempts to get stuff working i'm still banging my head against the wall. Could this perhaps be a disabled function in IIS7? Or am i missing something?
Thanks in advance
The file:// style links will only work for you on your local machine, you cannot open a folder on a remote server using that method. (well not unless that path is accessable from your local machine, a server share or somesuch)
Some sample code to display a directory:
<table cellspacing="0">
<tr>
<th style="width: 25px"> </th>
<th>Document</th>
<th style="width: 60px">Size</th>
</tr>
<%
Dim iCounter, iFileSize
Dim oFS, oFL
Dim sDirectory
sDirectory = "directory/you/want"
iCounter = 0
Set oFS = Server.CreateObject("Scripting.FileSystemObject")
Set oFL = oFS.GetFolder(Server.MapPath(sDirectory))
For Each oF In oFL.Files
iCounter = iCounter + 1
iFileSize = FormatNumber(CLng(oF.Size) / 1024 / 1024, 2)
%>
<tr>
<td><%=iCounter %>.</td>
<td><%=oF.Name %></td>
<td><%=iFileSize %> MiB</td>
</tr>
<%
Next
%>
</table>

custom controls and query string(asp.net)

How to call a custom control when query string is changed?
My example not work?Why?
<% if(Convert.ToInt32(Request.QueryString["id"])==6){ %>
<answer:answer_n id="give_me_top_five_news" runat="server" />
<%} %>
<% if(Request.QueryString["do"]=="registracija"){
Page.Header.Title = "HHHHH";
%>
<reg:f_reg id="custom_controls_for_registration" runat="server" />
<%} %>
Changed in what fashion? Anytime you load the page with a new URL and query string, the page will be loaded for the first time. You would have to save the info you wanted to store in the Session (for example) and then in the Page_Load event check the query string vs the Session variable to see if they are the same or different.
There may be different ways to do it, but that's what comes to mind off the cuff.

ASP Classic - Include VBScript page in a JavaScript page?

Is there a way to include a VBScript page into an ASP page written using Javascript?
There is an ASP page written in Javascript, but our sidebar menu is written in VBScript. When I include the sidebar asp file into the Javascript the server gives an error.
< %# LANGUAGE="JavaScript" %>
<%
...
< !--#include file="sidebar.asp"-->
...
where sidebar.asp is written using VBScript.
You can try this, but my guess is that the sidebar.asp will be executed before your Javascript code.
< %# LANGUAGE="JavaScript" %>
<%
...
<script language="VBscript" runat=server>
< !--#include file="sidebar.asp"-->
</script>
...
I do this all the time, but I write my ASP/JScript pages a bit differently. Instead of switching the page language to "JavaScript", I leave it at the default "VBScript" and then use a <SCRIPT LANGUAGE="JavaScript" RUNAT="Server"> block for my JScript code. The JavaScript SCRIPT blocks are executed before the normal <% %> tags, so I do all my page processing in the SCRIPT blocks and then simply plug the results into the page with <% %> tags. Here's an example:
mainpage.asp:
<SCRIPT LANGUAGE="JavaScript" RUNAT="Server">
var name;
var address;
var phone;
function main() {
var rec = go_to_database();
name = rec.first_name + " " + rec.last_name;
address = rec.address;
phone = rec.phone;
}
</SCRIPT><% main() %>
<html><head><title><%= name %></title></head><body>
<p>Name: <%= name %><br/>
Address: <%= address %><br/>
Phone Number: <%= phone %></p>
<!--#include file="subpage.asp"-->
</body></html>
subpage.asp:
<p>Blah blah blah, some random VBScript code: <%
Dim whatever
whatever = some_silly_thing()
Response.Write(whatever)
%>.</p>
So, first IIS processes the SSI and includes subpage.asp into mainpage.asp. Then, it evaluates the JScript SCRIPT block, declaring the variables name, address, and phone and defining the function main.
Then it evaluates each <% %> tag in order. <% main() %> call the main function and sets values for name, address, and phone. Then <%= name %>, <%= address %>, and <%= phone %> substitute those values into the page. Finally, the <% %> code from subpage.asp is evaluated and the Response.Write value ends up in the page output.
While the whole page is not written in JScript, the vast majority of the code can be, inside the SCRIPT block. Would that work for you?

Issue with asp:ContentPlaceHolder and code blocks

When a content placeholder contains any code blocks it reports that the control collection is empty.
For instance:
MasterPage.aspx
<asp:ContentPlaceHolder ID="Content1" runat="server" />
<asp:ContentPlaceHolder ID="Content2" runat="server" />
<div>Content1: <%= Content1.Controls.Count %></div>
<div>Content2: <%= Content2.Controls.Count %></div>
APage.aspx
<asp:Content ContentPlaceHolderID="Content1" runat="server">
Plain text content.
</asp:Content>
<asp:Content ContentPlaceHolderID="Content2" runat="server">
<%= "Code block content." %>
</asp:Content>
This will render the following:
Plain text content. Code block content.
Content1: 1
Content2: 0
Why is the master page's ContentPlaceHolder.Controls collection empty?
I want to check whether the ContentPlaceHolder has been populated (see also this question) but can't if it contains any <%= blocks.
Does anyone know a way around this?
As promised, I said I would take a look. Sorry I never uploaded last night, long day and needed to hit the hay!
So, I was checking out the ContentPlaceHolder.Controls collection differences between how they are populated. I noticed that when the code block is used, it flips to read only. At any other point, it will simply be empty or populated.
I therefore decided to throw in an extension method to check it for us:
ContentPlaceHolderExtensions.cs
public static class ContentPlaceHolderExtensions
{
public static bool ContainsControlsOrCodeBlock(this ContentPlaceHolder placeHolder)
{
if (placeHolder.Controls.Count > 0)
return true;
return placeHolder.Controls.IsReadOnly;
}
}
And then check this in the master page:
Site.Master
<asp:ContentPlaceHolder ID="Content1" runat="server" />
<asp:ContentPlaceHolder ID="Content2" runat="server" />
<asp:ContentPlaceHolder ID="Content3" runat="server" />
<div>Content1: <%= Content1.Controls.Count %></div>
<div>Content2: <%= Content2.Controls.Count %></div>
<div>Content3: <%= Content3.Controls.Count %></div>
<div>Content1 (Ex. Meth.): <%= Content1.ContainsControlsOrCodeBlock() %></div>
<div>Content2 (Ex. Meth.): <%= Content2.ContainsControlsOrCodeBlock() %></div>
<div>Content3 (Ex. Meth.): <%= Content3.ContainsControlsOrCodeBlock() %></div>
As proof-of-concept, I then added a content page:
Index.aspx
<asp:Content ContentPlaceHolderID="Content1" runat="server">
Plain Text Content
</asp:Content>
<asp:Content ContentPlaceHolderID="Content2" runat="server">
<%= "Code block content" %>
</asp:Content>
And all rendered as expected (I believe)..
TBH, while it is not perfect.. I don't think we can get much more elegance in this situation. I am not sure how other control collections are set up in these different scenarios, so I only bolted on to the ContentPlaceHolder control.. Other templated controls may or may not work the same.
Thoughts?
You can download the project from here:
http://code.google.com/p/robcthegeek/source/browse/#svn/trunk/stackoverflow/964724
Too much for a comment, here's the full code that I finally got working (adapted from #Rob Cooper's answer):
public static bool HasContent( this ContentPlaceHolder placeHolder )
{
if ( placeHolder.Controls.Count > 0 )
{
LiteralControl textBlock;
ContentPlaceHolder subContent;
foreach ( var ctrl in placeHolder.Controls )
if ( (textBlock = ctrl as LiteralControl) != null )
{ //lit ctrls will hold any blocks of text
if ( textBlock.Text != null && textBlock.Text.Trim() != "" )
return true;
}
else if ( (subContent = ctrl as ContentPlaceHolder) != null )
{ //sub content controls should call this recursively
if ( subContent.HasContent() )
return true;
}
else return true; //any other control counts as content
//controls found, but all are empty
return false;
}
//if any code blocks are used the render mode will be different and no controls will
//be in the collection, however it will be read only
return placeHolder.Controls.IsReadOnly;
}
This includes two extra checks - firstly for empty literal controls (which occur if the page includes the <asp:Content tags with any whitespace between them) and then for sub-ContentPlaceHolder which will occur for any nested master pages.
The controls collection is empty because when <%= %> script tags are present, literal controls are not added to the control tree. However, server controls will still get added. So:
<asp:Content ID="Content2" ContentPlaceHolderID="Content2" Runat="Server">
<%= "Code block content." %>
<asp:GridView runat="server" ID="gvTest" />
</asp:Content>
<div>Content2: <%= Content2.Controls.Count %></div>
will return
Content 2: 1
Rick Strahl has a great article that explains this behavior:
To make code like this work, ASP.NET
needs to override the rendering of the
particular container in which any
script code is hosted. It does this by
using SetRenderMethodDelegate on the
container and creating a custom
rendering method ...
Rather than building up the control
tree literal controls, ASP.NET only
adds server controls to the control
tree when <% %> tags are present for a
container. To handle the literal
content and the script markup, ASP.NET
generates a custom rendering method.
This method then explicitly writes out
any static HTML content and any script
expressions using an HTML TextWriter.
Any script code (<% %>) is generated
as raw code of the method itself.
Unfortunately I can't think of any elegant solution to this conundrum.

Resources