• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

Pass a Value from Link to a Sub ?

How can I pass a value from Link to a Sub ?, I have this:

<form runat="server">
<asp:LinkButton ID="Button1" Text="Testing" OnClick="OpenXlsFile" CommandName="1" runat="server" />      
</form>


<script language="VB" runat="server">
Sub OpenXlsFile(sender As Object, e As EventArgs)
      Response.ClearContent()
      Response.ClearHeaders()
      Response.ContentType = "application/vnd.ms-excel"
      Dim fName As String
      fName = "file.xls"
      Response.WriteFile(fName)
      Response.End()
End Sub
0
Mylor
Asked:
Mylor
  • 7
  • 7
1 Solution
 
MylorAuthor Commented:
I need to pass the file name from the link. Thanks.
0
 
muzzy2003Commented:
Use the CommandArgument attribute like this:

<form runat="server">
<asp:LinkButton ID="Button1" Text="Testing" OnClick="OpenXlsFile" CommandName="1" runat="server" CommandArgument="YourFileName.xls"/>      
</form>


<script language="VB" runat="server">
Sub OpenXlsFile(sender As Object, e As EventArgs)
     Response.ClearContent()
     Response.ClearHeaders()
     Response.ContentType = "application/vnd.ms-excel"
     Dim fName As String
     fName = e.CommandArgument
     Response.WriteFile(fName)
     Response.End()
End Sub
0
 
MylorAuthor Commented:
No, it doesn't work, I have the following error:


Compiler Error Message: BC30456: 'CommandName' is not a member of 'System.EventArgs'.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
muzzy2003Commented:
I didn't write e.CommandName, I wrote e.CommandArgument. Did you copy the code correctly?
0
 
MylorAuthor Commented:
Sorry, I have:

Compiler Error Message: BC30456: 'CommandArgument' is not a member of 'System.EventArgs'.
0
 
muzzy2003Commented:
Change the Sub declaration to:

Sub OpenXlsFile(sender As Object, e As CommandEventArgs)
0
 
MylorAuthor Commented:
Now I have this:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30408: Method 'Public Sub OpenXlsFile(sender As Object, e As System.Web.UI.WebControls.CommandEventArgs)' does not have the same signature as delegate 'Delegate Sub EventHandler(sender As Object, e As System.EventArgs)'.
0
 
muzzy2003Commented:
Hmmm. Is AutoEventWireUp set to true in the page declaration?

You could try resetting the declaration, but then changing this line in the code:

    fName = ((CommandEventArgs)e).CommandArgument
0
 
MylorAuthor Commented:
Now:

Compiler Error Message: BC30684: 'CommandEventArgs' is a type and cannot be used as an expression.
0
 
muzzy2003Commented:
Oh, sorry - it's VB.NET isn't it. Slipping into C# syntax. It's not my night tonight. Does this work in VB.NET?

Sub OpenXlsFile(sender As Object, e As EventArgs)
     CommandEventArgs e2 = e
     Response.ClearContent()
     Response.ClearHeaders()
     Response.ContentType = "application/vnd.ms-excel"
     Dim fName As String
     fName = e2.CommandArgument
     Response.WriteFile(fName)
     Response.End()
End Sub
0
 
MylorAuthor Commented:
Yes it's VB.NET, but I still have this:

Compiler Error Message: BC30684: 'CommandEventArgs' is a type and cannot be used as an expression.

Source Error:

Line 109:<script language="VB" runat="server">
Line 110:Sub OpenXlsFile(sender As Object, e As EventArgs)
Line 111:    CommandEventArgs e2 = e
Line 112:      Response.ClearContent()
Line 113:      Response.ClearHeaders()
 
0
 
muzzy2003Commented:
Sorry, think I might have found the problem:

<form runat="server">
<asp:LinkButton ID="Button1" Text="Testing" OnCommand="OpenXlsFile" CommandName="1" runat="server" CommandArgument="YourFileName.xls"/>      
</form>


<script language="VB" runat="server">
Sub OpenXlsFile(sender As Object, e As CommandEventArgs)
     Response.ClearContent()
     Response.ClearHeaders()
     Response.ContentType = "application/vnd.ms-excel"
     Dim fName As String
     fName = e.CommandArgument
     Response.WriteFile(fName)
     Response.End()
End Sub
0
 
muzzy2003Commented:
It needs to be OnCommand and not OnClick to pass a CommandArgument. Sorry, it's late here (00:54).
0
 
MylorAuthor Commented:
You got it !
The problem was the: OnCommand.

Thanks a lot!!!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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