[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 480
  • Last Modified:

Passing a querystring parameter and Invoking a method on an ASPX page

I have an ASP page which has a Link on it that passes a parameter via a querystring to an ASP.NET page called downloadFile.aspx.vb.  I have included the code of the downloadFile.aspx.vb page below.  My question is this.  Is there a way for the downloadFile page to retrieve the querystring and and then invoke a method that is included on that page using that parameter?  Essentially I want the downloadFile.asapx.vb page to do all the work.

Let me warn, I'm asking this because I know almost nothing about coding and I will require some help coding this.
0
TPBPIT
Asked:
TPBPIT
  • 7
  • 4
1 Solution
 
TPBPITAuthor Commented:
Opps, forgot to add the code.  I also have it in C# and can go either way.
Imports System 
Imports System.Collections 
Imports System.Configuration 
Imports System.Data 
Imports System.Web 
Imports System.Web.Security 
Imports System.Web.UI 
Imports System.Web.UI.HtmlControls 
Imports System.Web.UI.WebControls 
Imports System.Web.UI.WebControls.WebParts 
Imports System.IO 

Public Partial Class downloadFile 
    Inherits System.Web.UI.Page 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) 
        
    End Sub 
    
    Protected Sub lbnGetURLFile_Command(ByVal sender As Object, ByVal e As CommandEventArgs) 
        
        Dim sFileName As String = System.IO.Path.GetRandomFileName() 
        Dim sGenName As String = (e.CommandArgument & "_") + DateTime.Now.ToString() & ".nrl" 
        
        'YOu could omit these lines here as you may not want to save the textfile to the server 
        
        'I have just left them here to demonstrate that you could create the text file 
        
        Using SW As New System.IO.StreamWriter(Server.MapPath(sFileName)) 
            SW.WriteLine("BRDMS") 
            SW.WriteLine(("!nrtdms:0:!session:BRDMS:!database:br:!document:" & e.CommandArgument & ",") + e.CommandName & ":") 
            SW.WriteLine("[Version]") 
            SW.WriteLine("Latest=N") 
            SW.Close() 
        End Using 
        
        Dim fs As System.IO.FileStream = Nothing 
        fs = System.IO.File.Open(Server.MapPath(sFileName), System.IO.FileMode.Open) 
        Dim btFile As Byte() = New Byte(fs.Length - 1) {} 
        fs.Read(btFile, 0, Convert.ToInt32(fs.Length)) 
        
        fs.Close() 
        
        System.IO.File.Delete(Server.MapPath(sFileName)) 
        
        Response.AddHeader("Content-disposition", "attachment; filename=" & sGenName) 
        Response.ContentType = "application/octet-stream" 
        Response.BinaryWrite(btFile) 
            
            
            
        Response.[End]() 
    End Sub 
    
    
    
End Class

Open in new window

0
 
Miguel OzSoftware EngineerCommented:
Please include your code?

I think what you need is Request.QueryString, please check this link:

http://dotnetperls.com/querystring-net
0
 
TPBPITAuthor Commented:
That's a good start, but the other part is crucial.  How do I invoke the method using the parameter that is received?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Miguel OzSoftware EngineerCommented:
protected void Page_Load(object sender, EventArgs e)
    {
        string v = Request.QueryString["FileName"];
        if (v != null)
        {
            HandleDownloadFile(v);
        }
    }
protected void HandleDownloadFile(string filename)
    {
        // code this to your need.
    }

I do not think you need lbnGetURLFile_Command.

Note: very simple example of query string here:
http://aspnet101.com/tutorials.aspx?id=29
0
 
TPBPITAuthor Commented:
Could I just call the lbnGetURLFile_Command method?
0
 
Miguel OzSoftware EngineerCommented:
Yes you can:
protected void Page_Load(object sender, EventArgs e)
    {
        string v = Request.QueryString["FileName"];
        if (v != null)
        {
                 CommandEventArgs args2 = new CommandEventArgs("MyCmd", v);
                  lbnGetURLFile_Command(null, arg2);
        }
    }

Note: I prefer to use methods that are called for both the event handler and whatever code requires it so that the parameters are meaningful and code is easy to understand/maintain
0
 
TPBPITAuthor Commented:
Ok, I have changed the code; however, when I click the link I get an error which is below.  I have also attached a screenshot of the whole error returned by the browser.

The type of page you have requested is not served because it has been explicitly forbidden.  The extension '.vb' may be incorrect.   Please review the URL below and make sure that it is spelled correctly.
web-browser-error.jpg
0
 
TPBPITAuthor Commented:
Ok, apparently I can't call the downloadFile.aspx.vb file extension from the asp site.  Is there a way to compile this file so that it is an .aspx file that actually works?  Does that make sense?
0
 
Miguel OzSoftware EngineerCommented:
Make the call without the extension:
http://yoursite/downloadFile.aspx?Filename=123
0
 
shrinivasmwCommented:
Try sending querystring from one codebehind file lets say page1 to page2

1. add querystring value in page1 like putting in URL like  http://....../page2.aspx?query=Name
OR from codebehind file , use Response.Write("page2.aspx?query=Name");
2. get querystring value in page2 PAGE_LOAD method
3. depending on querystring value, in IF statement check value and call the method.
4. The error page IMAGE file you attached is because .net page you can call from URL is .aspx extension not   *.aspx.cs or *.aspx.vb file.  

So try in url, put like this -
Refer any sample code for querystring online.

http://www.java2s.com/Code/ASP/Request/SendandgetquerystringC.htm



<%@ Page language="c#" src="QueryStringSender.aspx.cs" AutoEventWireup="false" Inherits="QueryStringSender" %>
<HTML>
  <body>
    <form id="Form1" method="post" runat="server">
      <asp:ListBox id="lstItems" style="Z-INDEX: 101; LEFT: 21px; POSITION: absolute; TOP: 26px" runat="server" Width="204px" Height="188px"></asp:ListBox>
      <asp:CheckBox id="chkDetails" style="Z-INDEX: 102; LEFT: 24px; POSITION: absolute; TOP: 224px" runat="server" Width="216px" Height="16px" Text="Show Full Details"></asp:CheckBox>
      <asp:Button id="cmdGo" style="Z-INDEX: 103; LEFT: 24px; POSITION: absolute; TOP: 272px" runat="server" Width="152px" Text="View Information"></asp:Button>
      <asp:Label id="lblError" style="Z-INDEX: 104; LEFT: 304px; POSITION: absolute; TOP: 64px" runat="server" Width="312px" Height="96px"></asp:Label>
    </form>
  </body>
</HTML>


<%--
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

  public class QueryStringSender : System.Web.UI.Page
  {
    protected System.Web.UI.WebControls.ListBox lstItems;
    protected System.Web.UI.WebControls.CheckBox chkDetails;
    protected System.Web.UI.WebControls.Button cmdGo;
    protected System.Web.UI.WebControls.Label lblError;
  
    private void Page_Load(object sender, System.EventArgs e)
    {
      // Add sample values.
      lstItems.Items.Add("A");
      lstItems.Items.Add("B");

    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      //
      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      //
      InitializeComponent();
      base.OnInit(e);
    }
    
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
      this.cmdGo.Click += new System.EventHandler(this.cmdGo_Click);
      this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void cmdGo_Click(object sender, System.EventArgs e)
    {
      if (lstItems.SelectedIndex == -1)
      {
        lblError.Text = "You must select an item.";
      }
      else
      {
        string url = "QueryStringRecipient.aspx?";
        url += "Item=" + lstItems.SelectedItem.Text + "&";
        url += "Mode=" + chkDetails.Checked.ToString();
        Response.Redirect(url);
      }

    }
  }


--%>








<%-- QueryStringRecipient.aspx
<%@ Page language="c#" src="QueryStringRecipient.aspx.cs" AutoEventWireup="false" Inherits="QueryStringRecipient" %>
<HTML>
  <body>
    <form id="Form1" method="post" runat="server">
      <asp:Label id="lblInfo" style="Z-INDEX: 101; LEFT: 26px; POSITION: absolute; TOP: 25px" runat="server" Width="412px" Height="130px" BorderStyle="Groove" BorderWidth="2px" Font-Bold="True" Font-Names="Verdana" Font-Size="Medium" BackColor="LightYellow"></asp:Label>&nbsp;
    </form>
  </body>
</HTML>


//File: QueryStringRecipient.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

  public class QueryStringRecipient : System.Web.UI.Page
  {
    protected System.Web.UI.WebControls.Label lblInfo;
  
    private void Page_Load(object sender, System.EventArgs e)
    {
      lblInfo.Text = "Item: " + Request.QueryString["Item"];
      lblInfo.Text += "<br>Show Full Record: ";
      lblInfo.Text += Request.QueryString["Mode"];

    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      //
      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      //
      InitializeComponent();
      base.OnInit(e);
    }
    
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
      this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion
  }


--%>
           
       Reference : 
http://www.java2s.com/Code/ASP/Request/SendandgetquerystringC.htm

Open in new window

0
 
TPBPITAuthor Commented:
Sorry for the delay in response, I'm on holiday.

mas_oz2003 - That didn't work as it only opens the page as text and doesn't launch the method

shrinivasmw - I'm not sure how to use what you posted.  Looks like there probably needs to be 4 files, but I'm not sure what goes in which file.
0
 
TPBPITAuthor Commented:
I was able to rewrite the .aspx page to take a parameter from a query string.  Was actually easier once I understod what I was trying to accomplish.

Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now