2015-09-30 45 views
0

我有一個用戶控件gridview和rowcommand事件。 此用戶控件是使用LoadControl在頁面的按鈕點擊時動態添加的。 gridview的rowcommand不會觸發。Gridview的rowcommand事件沒有在動態添加usercontrol觸發

下面是加載按鈕點擊該用戶控件的代碼:

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 

這裏是在用戶控件的代碼

公共類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 

回答

0

在UserControl中動態添加的GridView中的事件會變得有點難看。由於動態添加的用戶控件必須在回發中重新添加,因此必須重新綁定GridView數據源,這會使您無法自動獲取爲您解開的事件。這就是說,你仍然可以通過解析表單的__EVENTTARGET來解決它。

添加一個HiddenField,告訴您是否需要重新添加UserControl。在btnSearch_Click事件處理程序

If CBool(hdnTitleSearchActive.Value) = True Then 

     AddSearchListToPanel() 

    End If 

呼叫AddSearchListToPanel():在你的Page_Load事件添加這一點。

現在AddSearchListToPanel的這個實現可以清理一些,但是這應該足以讓你去。請注意,在我的示例中觸發GridView命令的按鈕的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 
+0

感謝您的回答。在這種情況下,似乎不值得使用用戶控制。 –

相關問題