简体   繁体   中英

Gridview rowcommand event not firing in dynamically added usercontrol

I have a usercontrol with gridview and rowcommand event. This usercontrol is added dynamically using LoadControl on a button click of a page. The gridview's rowcommand doesn't fire.

Here is the code that loads the usercontrol on button click:

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

And here is the code in usercontrol

Public Class TitleSearchList Inherits 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

Events in GridViews that are added dynamically in a UserControl get a little ugly. Since UserControls that are added dynamically have to be re-added on the post-back, you have to rebind the GridView DataSource, which makes you lose out on automatically getting those Events fired for you. That being said, you can still pull it off with some parsing of the __EVENTTARGET of the Form.

Add a HiddenField that tells you whether or not you need to re-add the UserControl. Add this in your Page_Load Event Handler:

    If CBool(hdnTitleSearchActive.Value) = True Then

        AddSearchListToPanel()

    End If

Call AddSearchListToPanel() in your btnSearch_Click Event Handler.

Now this implementation of AddSearchListToPanel can be cleaned up some, but this should be good enough to get you going. Note that the Button triggering the GridView Command in my example has the ID of lbtTest. You will have to adjust based on the ID that you are using.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM