2013-02-20 64 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()