ASP.NET在代码隐藏中向按钮添加命令参数

时间:2013-10-21 18:15:41

标签: asp.net vb.net

我试图在后面的代码中添加一个添加到asp:按钮的参数,但是收到错误:

无法转换类型为#System; EventArgs'的对象输入' System.Web.UI.WebControls.CommandEventArgs'

此按钮是普通表单的一部分,而不是gridview等。

我可能做错了什么?问候。

<form><asp:Button ID="btnPrint" runat="server" Text="Print" CommandArgument="" /></form>

Imports System.Web

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim String AS String = "MyValue"
    btnPrint.CommandArgument = String

End Sub    

Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As CommandEventArgs) Handles btnPrint.Click


     Dim ID as String = e.CommandArgument.toString

     'Have also tried
      'Dim btn As Button = sender
      'Dim ID As String = btn.CommandArgument

      Response.Redirect("Print.aspx?tid=" & ID)

End Sub

2 个答案:

答案 0 :(得分:2)

您只需更新btnPrint_Click即可处理Command事件而不是“点击”:

Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As CommandEventArgs) Handles btnPrint.Command

答案 1 :(得分:0)

我的ASP按钮点击事件如下所示:

Private Sub SearchASPxButton_Click(sender As Object, e As EventArgs) Handles SearchASPxButton.Click

注意第二个参数是System.EventArgs类型。您是否更改了自动生成的点击事件的类型?或者您自己手动创建了这个?