从用户控制页面触发controlID

时间:2013-02-20 21:10:57

标签: asp.net .net vb.net

我有下面的代码看起来应该有效。但是,使用controlID“gvSearch”引用控件的触发器实际上是用户控件页面内的gridview。

如何访问该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 个答案:

答案 0 :(得分:2)

您希望将哪个事件用作触发器SelectedIndexChanged - 事件?然后应用适当的EventName

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

但是,这应该是GridView的{​​{3}}事件(请参阅default部分)。

看一下这个主题:remarks

更新我认为在您的情况下,最佳方法是在您的UserControl中提供一个自定义事件,该事件来自UC中SelectedIndexChanged的{​​{1}}事件。然后,您可以将此事件用作GridView的{​​{1}},例如:

AsyncPostBackTrigger

答案 1 :(得分: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()
相关问题