Solved

VBSCRIPT in Page Load C# ASP.NET

Posted on 2016-09-28
6
52 Views
Last Modified: 2016-10-05
I have this VBscript code in the head section of an asp.net c# form for a webapp:

   <script language="vbscript" type="text/vbscript">

Dim myString, valuecn, cn, stringvalue

valuecn = Request.QueryString("valuecn")

if valuecn = "" then valuecn = "inactivo" End If

If valuecn = "inactive" then

Response.Redirect("inactive.aspx")

End if

</script>

Open in new window


How can we do to execute that in the page load of asp.net c# code behind?

Because if you run the page never redirect to inactive.aspx

Thank you
0
Comment
Question by:Alex E.
  • 3
  • 3
6 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
you need to do it in C# codes, like:
protected void Page_Load(object sender, EventArgs e)
        {
            String myString, valuecn, cn, stringvalue;

            valuecn = Request.QueryString["valuecn"];

            if (valuecn == "") valuecn = "inactivo";

            if (valuecn == "inactive") {
                Response.Redirect("inactive.aspx");
            }
        }

Open in new window

pls aware of line:
 if (valuecn == "") valuecn = "inactivo";

Open in new window

i'm not too sure if inactivo was a typo? should it be inactive instead?
0
 

Author Comment

by:Alex E.
Comment Utility
Yes "inactivo" was a bad typo. But I asked because in this code in regular asp working but when we passed together with the routine I posted in the beginning the next part of code is presenting some troubles:

Imports System.Data
Imports System.IO
Partial Class launch_file2
    Inherits System.Web.UI.Page
    Dim size As Integer
    Dim snfile As String
    Dim fs As String
    Dim objfile As String
    Dim mime_type As String
    Dim fname As String
    Dim GetFile As String
    
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Response.CodePage = 65001
        Response.Charset = "utf-8"

        snfile = Request.QueryString("path")
        fs = Server.CreateObject("scripting.filesystemobject")

        If fs.FileExists(Server.MapPath(snfile)) = True Then
            objfile = fs.GetFile(Server.MapPath(snfile))
            size = objfile.size
            fname = objfile.name
            mime_type = "application/save-as"
            Response.AddHeader("Expires:", 0)
            Response.ContentType = mime_type
            Response.AddHeader("Content-Disposition", "attachment; filename=" & fname)
            objStream = Server.CreateObject("ADODB.Stream")
            objStream.Open()
            objStream.Type = 1
            objStream.LoadFromFile(Server.MapPath(snfile))
            Response.BinaryWrite(objStream.Read)
        Else
            Response.Write("File not found!")
            Response.End()
        End If
    End Sub

Open in new window


The errors show the script are:

The "Response.CodePage = 65001" gives me this error: 'FileExists' is not a member of 'String'.
The size = objfile.size shows this 'size' is not a member of 'String'.
The fname = objfile.name shows this 'size' is not a member of 'String'.
'The objStream = Server.CreateObject("ADODB.Stream") shows objStream' is not declared. It may be inaccessible due to its protection level.

This script what it does is if you call for example dwnloadfile.asp?path=anypath what it does is download the file without executing in browser.

For example in that parts what are the problems? with normal asp you just enter <% %> in the middle code and that´s it but that routine we can´t convert to VB in asp.net.
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
so just to confirm...

1. are you intend to do above in C# and not vb.net ?

2. or you just want to solve the error above in vb.net?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Alex E.
Comment Utility
Just convert that one to vb.net
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
Comment Utility
sorry for late response, you can try this:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Response.Charset = "utf-8"

        snfile = Request.QueryString("path")

        Dim fs As FileInfo = New FileInfo(Server.MapPath(snfile))

        If fs.Exists Then
            size = fs.Length
            fname = fs.Name
            mime_type = "application/save-as"
            Response.AddHeader("Expires:", 0)
            Response.ContentType = mime_type
            Response.AddHeader("Content-Disposition", "attachment; filename=" & fname)
            Response.AddHeader("Content-Length", fs.Length.ToString())
            Response.WriteFile(fname)
            Response.End()
        Else
            Response.Write("File not found!")
            Response.End()
        End If
    End Sub

Open in new window

0
 

Author Comment

by:Alex E.
Comment Utility
Thank you
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now