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
MylorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.