Link to home
Start Free TrialLog in
Avatar of Mylor
Mylor

asked on

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
Avatar of Mylor
Mylor

ASKER

I need to pass the file name from the link. Thanks.
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
Avatar of Mylor

ASKER

No, it doesn't work, I have the following error:


Compiler Error Message: BC30456: 'CommandName' is not a member of 'System.EventArgs'.
I didn't write e.CommandName, I wrote e.CommandArgument. Did you copy the code correctly?
Avatar of Mylor

ASKER

Sorry, I have:

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

Sub OpenXlsFile(sender As Object, e As CommandEventArgs)
Avatar of Mylor

ASKER

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)'.
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
Avatar of Mylor

ASKER

Now:

Compiler Error Message: BC30684: 'CommandEventArgs' is a type and cannot be used as an expression.
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
Avatar of Mylor

ASKER

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()
 
ASKER CERTIFIED SOLUTION
Avatar of muzzy2003
muzzy2003

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It needs to be OnCommand and not OnClick to pass a CommandArgument. Sorry, it's late here (00:54).
Avatar of Mylor

ASKER

You got it !
The problem was the: OnCommand.

Thanks a lot!!!