How to Process Forms in a separate ASP file - vbscript

I have a question.
I am learning classic ASP today for my next project. I am currently a .NET developer and using ASP.NET on that project is not a requirement of my client.
I have below a login page script.
Default.asp
<form method="post" action="ASP/aspLogin.asp">
form code here...
<input type="submit" class="Button Is_Default" value="Login"></input>
</form>
Now, what I have in my test ASP/aspLogin.asp page is as follows:
ASP/aspLogin.asp
<%# Language="VBScript" %>
<%
Dim strUsername
Dim strPassword
strUsername = Request.Form("txtUsername")
strPassword = Request.Form("txtPassword")
If strUsername <> "" And strPassword <> "" Then
Response.Redirect("Index.asp")
End If
%>
When I ran the scripts above, the browser just redirected me to ASP/aspLogin.asp. I would want to redirect the user to his respective home page.
My objective is that I want my ASP/aspLogin.asp file do the processing of my form instead of placing the process above the Default.asp page. May I know if I missed something out here or there are some more things to consider to create the code I need. Resources would be also appreciated.

If you have users logging in and out then I'd use session variables (which I'm sure you've encountered in .net), so something along the lines of.
If strUsername <> "" And strPassword <> "" Then
Session("Username") = strUsername
Response.Redirect("Index.asp")
End If
You would then be able to add logic to index.asp and any other page to display data depending on the value of Session("Username").
This code demonstrates the concept. Obviously you would need to ensure that usernames aren't duplicated. In practice I'd recommend a database query to retrieve the primary key value of the users record which corresponds to both username and password
Edit.
So you're basically trying to emulate .net with Classic ASP. Two things to remember. Classic doesn't have code behinds, and a .net webform can only be posted to itself, (which gets really frustrating for someone moving from Classic to .net)
Probably you're best option is to have your form page post to itself and put your processing code at the top of Default.asp, with logic which only triggers it if there's a form submission, ie
<%# Language="VBScript" %>
<%
Dim strUsername
Dim strPassword
strUsername = Request.Form("txtUsername")
strPassword = Request.Form("txtPassword")
If strUsername <> "" And strPassword <> "" Then
'Your processing code
Response.Redirect("Index.asp")
End If
%>
<html>
<body>
<form method="post">
form code here...
<input type="submit" class="Button Is_Default" value="Login"></input>
</form>
</body></html>
An extension of this method would be to use asplogin.asp as an include. (You would need to remove the <%# Language="VBScript" %> from the top of asplogin.asp, as it would become the middle of a page and you would already have your declaration in default.asp). Default.asp would then look like this.
<%# Language="VBScript" %>
<!--#include file="ASP/aspLogin.asp"-->
<html>
<body>
<form method="post">
form code here...
<input type="submit" class="Button Is_Default" value="Login"></input>
</form>
</body></html>
This is about as close as you can get to having a codebehind in Classic

Related

using row filter to get specific names from table

There is a code for filtering fields from a query (fname, lname and location) into a DataGridView:
Dim DV As New DataView(dbdataset1)
DV.RowFilter = String.Format("fname like '%" & Me.tbSearch.Text.Trim & "%'")
DataGridView.DataSource = dbdataset1
The filter can find lname, location but not fname:
here is the screen shot of the populated fields
As for the DataGridView element I can tell you're on ASP.NET 2.0 or below. Anyway, I got it working on ASP.NET 4.7 like this:
ASPX file:
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
<asp:TextBox ID="tbSearch" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" />
</form>
</body>
</html>
Code behind (VB.Net):
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Private Sub form1_Load(sender As Object, e As EventArgs) Handles form1.Load
End Sub
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim dbdataset1 As New DataTable
Dim dr As DataRow
dbdataset1.Columns.Add("fname")
For i As Integer = 1 To 3
dr = dbdataset1.NewRow()
dr("fname") = "picture" + i.ToString()
dbdataset1.Rows.Add(dr)
Next
For i As Integer = 1 To 3
dr = dbdataset1.NewRow()
dr("fname") = "document" + i.ToString()
dbdataset1.Rows.Add(dr)
Next
Dim DV As New DataView(dbdataset1)
DV.RowFilter = String.Format("fname like '%" & Me.tbSearch.Text.Trim & "%'")
Me.GridView1.DataSource = DV
Me.GridView1.DataBind()
End Sub
End Class
Just replace my GridView1 with your DataGrdiView.
UPDATE:
So the filter was fixed by the user deleting and adding the Texbox control again into the form.

Ajax Pagination in Liferay Search Container

I am trying to Ajax Call on Search Container Pagination. As there are more than one portlet on same page I don't want to each portlet to forcefully call render every time when I do Pagination.
There could be 2 possible solutions but I am facing some problem. Please help me to solve it.
1) Can you please tell me how to edit Liferay Search Container Pagination URL by changing its Portlet Life-cycle from 0 to 2 ? How to create hook ?
2) There is a tag called <liferay-ui:page-iterator/> in which there is a attribute called jsCall. It is used for pagination without page reloading. I am unable to find out how to use it ?
If it can be solved using 2nd option then it would be much helpful.
Thank You.
kanakhara,
Each time I faced that problem I had to implement manually a pagination because search-container is really embedded to be modified by a developer.
Maybe could be a solution to this problem but it's not sure that it exists.
You'll spend less time creating your own pagination for this purpose.
I hope it can help you.
Best wishes
Yes, I know how to use it.
I'll give you an example to do it:
<portlet:renderURL var="manageRelationsURL"
windowState="<%=LiferayWindowState.NORMAL.toString()%>">
<portlet:param name="action" value="manageRelations" />
</portlet:renderURL>
<%PortletURL iteratorURL = renderResponse.createRenderURL();
iteratorURL.setParameter("action", "manageRelations");
%>
<%
PortalPreferences portalPrefs = PortletPreferencesFactoryUtil.getPortalPreferences(request);
String orderByCol = ParamUtil.getString(request, "orderByCol");
String orderByType = ParamUtil.getString(request, "orderByType");
if (Validator.isNotNull(orderByCol) && Validator.isNotNull(orderByType)) {
portalPrefs.setValue("NAME_SPACE", "order-by-col", orderByCol);
portalPrefs.setValue("NAME_SPACE", "order-by-type", orderByType);
} else {
orderByCol = portalPrefs.getValue("NAME_SPACE", "order-by-col", "domainLabel");
orderByType = portalPrefs.getValue("NAME_SPACE", "order-by-type", "asc");
}
%>
<div style="">
<aui:form>
<liferay-ui:search-container delta="20" iteratorURL="<%=iteratorURL%>" emptyResultsMessage="There were not any match." orderByCol="<%= orderByCol %>" orderByType="<%= orderByType %>">
<liferay-ui:search-form
page="/WEB-INF/jsp/localAdministration/relations/search.jsp"
searchContainer="<%= searchContainer %>"
servletContext="<%= this.getServletConfig().getServletContext() %>"
showAddButton="true" />
<liferay-ui:search-container-results>
<%
List<RelationInstance> relationList = UtilsAdministration.getRelationListCache(themeDisplay.getUserId(),0, UtilsAdministration.getRelationListCacheSize(themeDisplay.getUserId()));
Collections.sort(relationList,RelationsComparator.getRelationsOrderByComparator(orderByCol, orderByType));
results = ListUtil.subList(relationList, searchContainer.getStart(),
searchContainer.getEnd());
if(relationList.size()<searchContainer.getEnd()){
results = ListUtil.subList(relationList, searchContainer.getStart(),
relationList.size());
total = relationList.size();
}else{
results = ListUtil.subList(relationList, searchContainer.getStart(),
searchContainer.getEnd());
total = relationList.size();
}
pageContext.setAttribute("results", results);
pageContext.setAttribute("total", total);
%>
</liferay-ui:search-container-results>
<liferay-ui:search-container-row className="RelationInstance" modelVar="aRelationInstance">
<liferay-ui:search-container-column-text name="First" value="<%=aRelationInstance.getFirstLabel()%>" orderable="<%= true %>" orderableProperty="domainLabel"/>
<liferay-ui:search-container-column-text name="Second" value="<%=aRelationInstance.getSecondLabel()%>" orderable="<%= true %>" orderableProperty="relationLabel"/>
<liferay-ui:search-container-column-text name="Third" value="<%=aRelationInstance.getRangeLabel()%>" orderable="<%= true %>" orderableProperty="rangeLabel"/>
<liferay-ui:search-container-column-jsp align="right" name="Acciones" path="/WEB-INF/jsp/localAdministration/relations/actionRelationButton.jsp"/>
</liferay-ui:search-container-row>
<liferay-ui:search-iterator />
</liferay-ui:search-container>
</aui:form>
</div>
In this case I retrieve the list to iterate from EhCache instead of DB.
IteratorURL must contains the same URL that the page you're invoking.
I hope it could be useful for you.

Unable to use Response.Write in VBScript

I am supporting an old Classic ASP application and I've read that the code we write within <% %> is VBScript and <% Response.Write "VBScript" %> is working fine , but when I write in the following format, Response.Write is not working
<SCRIPT LANGUAGE="VBScript">
Response.Write "VBScript"
</SCRIPT>
also Response.Write is not getting executed in an button click event
<SCRIPT LANGUAGE="VBScript" >
function B3_OnClick()
FORM1.T3.style.backgroundColor = "white"
FORM1.T4.style.backgroundColor = "white"
FORM1.T3.readOnly ="false"
FORM1.T4.readOnly ="false"
FORM1.style.backgroundColor = "white"
Response.Write("Hello World")
End function
</SCRIPT>
Can Anyone explain me why? Is there any substitute for Response.Write in that case..? Thanks in advance
This is because you are missing the runat="Server" attribute in the <script> tag.
<SCRIPT LANGUAGE="VBScript" runat="Server">
Response.Write "VBScript"
</SCRIPT>
Remember VBScript accessed through Classic ASP happens before the response is sent to the client. For an action such as clicking a button on the client to affect the server-side code it has to make a round-trip to the server. If you want to have server-side code affect client-side code you can inject code before returning a server response to the client.
<SCRIPT LANGUAGE="VBScript" >
function B3_OnClick()
FORM1.T3.style.backgroundColor = "white"
FORM1.T4.style.backgroundColor = "white"
FORM1.T3.readOnly ="false"
FORM1.T4.readOnly ="false"
FORM1.style.backgroundColor = "white"
MsgBox "<% Response.Write("Hello World") %>"
End function
</SCRIPT>
Useful Links
Answer to Access client variable within server-tags in vbscript
Answer to what's the difference between <% %> and in classic asp?

Image Uploading to a folder in classic ASP

I am trying to upload a image into a folder using classic asp. I am able to upload image into a folder but not getting the values of other input fields.
<%# Language="VBScript" %>
<!--<span class="code-comment"> #include file="vbsUpload.asp" </span>-->
<form method=post
enctype="multipart/form-data"
action=<%=request.servervariables("script_name")%>>
Your File:<BR><input type="file" name="YourFile"><BR>
<input type="text" name="myname" id="name12">
<BR>
<input type="submit" name="submit" value="Upload">
</form>
<% Dim objUpload, lngLoop, cia
If Request.TotalBytes > 0 Then
Set objUpload = New vbsUpload
For lngLoop = 0 to objUpload.Files.Count - 1
'If accessing this page annonymously,
'the internet guest account must have
'write permission to the path below.
objUpload.Files.Item(lngLoop).Save "c:\inetpub\wwwroot\puntoseguro\admin\image\"
Response.Write "File Uploaded"
Next
cia = Upload.Form("myname")
End if
%>
When I submit the form, I am getting the following error,
File Uploaded
Microsoft VBScript runtime error '800a01a8'
Object required: ''
/admin/test.asp, line 26
line 26 - cia = Upload.Form("myname")
I see two problems with your code.
1) You are declaring the text box like this:
<input type="text" name="myname" id="name12">
But you are trying to get it's value using the ID (name12), but you you need to get it using it's "name" (myname).
2) Also, you should be using objUpload.Form instead of Upload.Form as Upload doesn't appear to exist, hence the error.
You can fix both problems by replacing this line...
cia = Upload.Form("name12")
...with...
cia = objUpload.Form("myname")
(or change the name to match the ID)

A potentially dangerous Request.Form value was detected from the client ext.net

I have a user control in ext.net and a hidden field inside it which is "__DataForAddingToOutlook". This field is filled using xml writer so it has xml.
We have a functionality to add data to outlook and sync to outlook for which I am using direct methods. As soon as I call direct method I get the error
"A potentially dangerous Request.Form value was detected from the client (cms_cpMain_winAddContactsToOutlook___DataForAddingToOutlook="
The field is filled like-
Private Sub ExportToControl()
Dim oXMLWriter As XmlTextWriter = Nothing
Dim oStream As MemoryStream = Nothing
oStream = New MemoryStream()
oXMLWriter = New XmlTextWriter(oStream, Encoding.UTF8)
ReadData(oXMLWriter)
'
Dim stream_reader As New StreamReader(oStream)
oStream.Seek(0, SeekOrigin.Begin)
__DataForAddingToOutlook.Value = stream_reader.ReadToEnd()
End Sub
Can anyone help.
Try to set ValidateRequest="false" in Page tag:
<%# Page Title="" ValidateRequest="false" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

Resources