首页 > 解决方案 > 我如何将链接按钮 oncommand 事件挂钩到另一种方法,比如 gridview OnPageIndexChanging 事件

问题描述

我有一个LinkButton处理Command事件的地方。我也有一个GridView我正在处理PageIndexChanging事件的地方。我想从' 处理程序中调用LinkButton'Command处理程序。我该怎么做呢?GridViewPageIndexChanging

//Code-behind of my LinkButton Command handler

Protected Sub Linkprodcat1_Command(sender As Object, e As CommandEventArgs)    
    If e.CommandName = "select" Then
        //do something
    End if 
End Sub


//Code-behind of my GridView PageIndexChanging handler
//Here I want to call the the LinkButton's Command handler method 

Protected Sub GridView1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)
    If Me.pageindexselctoption.Value = "product1" Then
        Call Linkprodcat1_Command(sender, e) //this does not work
    End If
End Sub

标签: asp.netvb.net

解决方案


Call Linkprodcat1_Command(sender, e)行不通,因为e这里是 type GridViewPageEventArgsLinkprodcat1_Command期望其第二个参数为 by 类型CommandEventArgs

这是我的做法:

Protected Sub Linkprodcat1_Command(sender As Object, e As CommandEventArgs)
    If e.CommandName = "select" Then
        SelectCommand()
    End If
End Sub

Protected Sub GridView1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)
    If Me.pageindexselctoption.Value = "product1" Then
        SelectCommand()
    End If
End Sub

Protected Sub SelectCommand()
    'Do something
End Sub

推荐阅读