2013-02-20 42 views
1

我有下面的代碼看起來應該工作。但是,引用具有controlID「gvSearch」的控件的觸發器實際上是用戶控件頁面內的gridview。從用戶控制頁面觸發controlID

如何訪問該gridview,以便可以將其用作觸發器?

謝謝!

<asp:UpdatePanel ID="pnlSearch" ChildrenAsTriggers="true" runat="server" > 
     <Triggers> 
      <asp:AsyncPostBackTrigger ControlID="btnSearch" /> 
      <asp:AsyncPostBackTrigger ControlID="gvSearch" /> 
     </Triggers> 
</asp:UpdatePanel> 

謝謝!

大綱:

<%@ Page Title="test"> 
<%@ Register src="test1.ascx" tagname="test1" tagprefix="test1uc" %> 
    <UpdatePanel> 
     <Triggers> 
      <asp:AsyncPostBackTrigger ControlID="btnSearch" /> 
      <asp:AsyncPostBackTrigger ControlID="gvSearch" /> 
     </Triggers> 
     <ContentTemplate> 
      <test1uc:test1 ID="test1a" runat="server" /> 
     </ContentTemplate> 
    </UpdatePanel> 

回答

2

你要用作觸發器什麼情況下,SelectedIndexChanged -event?然後應用相應的EventName

<asp:AsyncPostBackTrigger ControlID="gvSearch" EventName="SelectedIndexChanged" /> 

然而,這應該已經是default事件GridView(見remarks部分)。

看一看這個線程:http://forums.asp.net/t/1136932.aspx

更新我想在你的情況,最好的辦法是提供您的用戶控件是從SelectedIndexChanged事件冒泡從UC的GridView自定義事件。然後你可以使用這個事件作爲AsyncPostBackTrigger爲您UpdatePanel,如:

<asp:AsyncPostBackTrigger ControlID="test1a" EventName="GridSearchClicked" /> 
+0

但控制(gridview的在這種情況下)用「gvSearch」的控件ID是內部一個用戶控件,我試圖找出如何從我的.aspx頁面訪問它 - 謝謝 – SkyeBoniwell 2013-02-20 21:17:30

+1

@ 999cm999:一個常見問題,看看我最後提供的鏈接。一般來說,當不同的用戶控件相互依賴時,它不是一件好事,它們不再單獨工作。 – 2013-02-20 21:20:06

+0

在這種情況下,我的頁面中的UpdatePanel包含一個存在於用戶控件中的gridview。 – SkyeBoniwell 2013-02-20 21:23:51

1

撇開事實,需要訪問一個子控制不好的做法,這是我最好的建議...

創建一個Public Sub在你的用戶控件,如下所示:

Private ParentUpdatePanel As System.Web.UI.UpdatePanel 

' Must be called on every Page_Load! 
Public Sub RegisterAsyncTrigger(MyScriptManager As System.Web.UI.ScriptManager, MyUpdatePanel As System.Web.UI.UpdatePanel) 
    MyScriptManager.RegisterAsyncPostBackControl(gvSearch) 
    ParentUpdatePanel = MyUpdatePanel 
End Sub 

在Page_Load事件中,你會調用該函數如下:

Protected Sub Page_Load(Sender As Object, e As EventArgs) 
    ' Call our new function, passing in the current ScriptManager and the UpdatePanel 
    ' The ScriptManager handles the asynchronous postbacks 
    ' The UpdatePanel handles the dynamic updates 
    test1.RegisterAsyncTrigger(ScriptManager.GetCurrent(Me), pnlSearch) 
End Sub 

gvSearch GridView控件,其必須更新面板將包含以下代碼中的事件處理程序:

ParentUpdatePanel.Update() 
相關問題