Gridview rowcommand事件未在动态添加的usercontrol中触发

时间:2015-09-30 03:09:38

标签: asp.net vb.net gridview

我有一个带gridview和rowcommand事件的usercontrol。 在按钮单击页面时使用LoadControl动态添加此用户控件。 gridview的rowcommand没有触发。

以下是在按钮点击时加载用户控件的代码:

Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click

    '<ucTitle:SearchList ID="ucSearchList" runat="server" Visible="false" />
    Dim ucSearchList As TitleSearchList = LoadControl("~/Controls/TitleSearchList.ascx")
    ucSearchList.ISBN = txtSearchISBN.Text
    ucSearchList.LoadTitleSearchList()
    pnlSearchResults.Controls.Add(ucSearchList)

End Sub

这是usercontrol中的代码

公共类TitleSearchList     继承System.Web.UI.UserControl

Public Property ISBN As String

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        LoadTitleSearchList()
    End If
End Sub

Public Sub LoadTitleSearchList()

    Dim _isbn As String = ISBN

    Dim titles As New List(Of Title)

    titles = New List(Of Title) From {
                                        New Title With {.ISBN = _isbn, .TitleName = "Title check"},
                                        New Title With {.ISBN = _isbn, .TitleName = "Title check"},
                                        New Title With {.ISBN = _isbn, .TitleName = "Title check"},
                                        New Title With {.ISBN = _isbn, .TitleName = "Title check"}
                                    }
    gvTitle.DataSource = titles
    gvTitle.DataBind()

End Sub

Public Sub gvTitle_Rowcommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles gvTitle.RowCommand

    If e.CommandName = "TitleDetail" Then

        Response.Redirect("TitleSearch.aspx?isbn=" & e.CommandArgument().ToString())

    End If

End Sub
End Class

1 个答案:

答案 0 :(得分:0)

在UserControl中动态添加的GridViews中的事件变得有点难看。由于动态添加的UserControl必须在回发后重新添加,因此您必须重新绑定GridView DataSource,这会让您失去自动获取为您触发的事件。话虽如此,您仍然可以通过解析表单的__EVENTTARGET来解决这个问题。

添加一个HiddenField,告诉您是否需要重新添加UserControl。在Page_Load事件处理程序中添加:

    If CBool(hdnTitleSearchActive.Value) = True Then

        AddSearchListToPanel()

    End If

AddSearchListToPanel()事件处理程序中调用btnSearch_Click

现在可以清除一些AddSearchListToPanel的实现,但这应该足以让你前进。请注意,在我的示例中触发GridView命令的Button具有ID为lbtTest。 您必须根据您使用的ID进行调整。

Private Sub AddSearchListToPanel()

    Dim ucSearchList As TitleSearchList = LoadControl("~/Controls/TitleSearchList.ascx")
    ucSearchList.ISBN = txtSearchISBN.Text
    ucSearchList.LoadTitleSearchList()
    pnlSearchResults.Controls.Add(ucSearchList)

    hdnTitleSearchActive.Value = True

    Dim strEventTarget As String = HttpContext.Current.Request.Form("__EVENTTARGET")

    If Not strEventTarget Is Nothing AndAlso strEventTarget.Contains("gvTitle$") AndAlso _
           strEventTarget.Contains("$lbtTest") Then

        'Value example = gvTitle$ctl02$lbtTest
        Dim intRowNumber As Integer = (CInt(strEventTarget.Substring(11, 2)) - 1)

        Dim lbtCommandSource As LinkButton = CType(CType(ucSearchList.FindControl("gvTitle"), GridView).Rows(intRowNumber).FindControl("lbtTest"), LinkButton)

        Dim objCommandEventArguments As New CommandEventArgs(lbtCommandSource.CommandName, lbtCommandSource.CommandArgument)

        Dim objGridViewCommandEventArgs As New GridViewCommandEventArgs(lbtCommandSource, objCommandEventArguments)

        ucSearchList.gvTitle_Rowcommand(lbtCommandSource, objGridViewCommandEventArgs)

    End If

End Sub