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
<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
Use the CommandArgument attribute like this:
<form runat="server">
<asp:LinkButton ID="Button1" Text="Testing" OnClick="OpenXlsFile" CommandName="1" runat="server" CommandArgument="YourFileN ame.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
<form runat="server">
<asp:LinkButton ID="Button1" Text="Testing" OnClick="OpenXlsFile" CommandName="1" runat="server" CommandArgument="YourFileN
</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
ASKER
No, it doesn't work, I have the following error:
Compiler Error Message: BC30456: 'CommandName' is not a member of 'System.EventArgs'.
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?
ASKER
Sorry, I have:
Compiler Error Message: BC30456: 'CommandArgument' is not a member of 'System.EventArgs'.
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)
Sub OpenXlsFile(sender As Object, e As CommandEventArgs)
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. CommandEve ntArgs)' does not have the same signature as delegate 'Delegate Sub EventHandler(sender As Object, e As System.EventArgs)'.
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.
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).Comm andArgumen t
You could try resetting the declaration, but then changing this line in the code:
fName = ((CommandEventArgs)e).Comm
ASKER
Now:
Compiler Error Message: BC30684: 'CommandEventArgs' is a type and cannot be used as an expression.
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
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
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()
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It needs to be OnCommand and not OnClick to pass a CommandArgument. Sorry, it's late here (00:54).
ASKER
You got it !
The problem was the: OnCommand.
Thanks a lot!!!
The problem was the: OnCommand.
Thanks a lot!!!
ASKER