Asp.net 丢失按钮。在UpdatePanel中单击第一次部分回发后的事件

Asp.net 丢失按钮。在UpdatePanel中单击第一次部分回发后的事件,asp.net,asp.net-ajax,user-controls,postback,Asp.net,Asp.net Ajax,User Controls,Postback,我有一个页面,它有一个UserControl实例,而UserControl本身有一个UpdatePanel。UpdatePanel中有几个按钮控件。这些控件的单击事件在UserControl的Init事件中的代码隐藏中连接 每次按下第一个按钮,我都会得到点击事件,没有问题。之后,我只得到一个按钮(SearchButton)的点击事件,其余的都被忽略。我已经包含了下面控件的代码-为了简洁起见,我排除了click事件处理程序方法,但它们都是标准的“void Button\u click(object

我有一个页面,它有一个UserControl实例,而UserControl本身有一个UpdatePanel。UpdatePanel中有几个按钮控件。这些控件的单击事件在UserControl的Init事件中的代码隐藏中连接

每次按下第一个按钮,我都会得到点击事件,没有问题。之后,我只得到一个按钮(SearchButton)的点击事件,其余的都被忽略。我已经包含了下面控件的代码-为了简洁起见,我排除了click事件处理程序方法,但它们都是标准的“void Button\u click(object sender,EventArgs e)”类型。有什么想法吗

<asp:UpdatePanel ID="PickerUpdatePanel" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Panel ID="Container" runat="server">
            <div>
                <asp:TextBox ID="PickerResults" runat="server" style="margin-right: 3px;" SkinID="Plain" />
                <asp:Image
                    ID="LaunchPopup" runat="server" ImageUrl="~/images/icons/user_manage.png" 
                    ImageAlign="Top" BorderColor="#294254" BorderStyle="Dotted" BorderWidth="1px" 
                    Height="20px" Width="20px" style="cursor: pointer;" />
            </div>
            <asp:Panel ID="PickerPanel" runat="server" DefaultButton="OKButton" CssClass="popupDialog" style="height: 227px; width: 400px; padding: 5px; display: none;">
                <asp:Panel runat="server" id="ContactPickerSearchParams" style="margin-bottom: 3px;" DefaultButton="SearchButton">
                    Search: <asp:TextBox ID="SearchTerms" runat="server" style="margin-right: 3px;" Width="266px" SkinID="Plain" />
                    <asp:Button ID="SearchButton" runat="server" Text="Go" Width="60px" SkinID="Plain" />
                </asp:Panel>
                <asp:ListBox ID="SearchResults" runat="server" Height="150px" Width="100%" SelectionMode="Multiple" style="margin-bottom: 3px;" />
                <asp:Button ID="AddButton" runat="server" Text="Add >>" style="margin-right: 3px;" Width="60px" SkinID="Plain" />
                <asp:TextBox ID="ChosenPeople" runat="server" Width="325px" SkinID="Plain" />
                <div style="float: left;">
                    <asp:Button ID="AddNewContact" runat="server" SkinID="Plain" Width="150px" Text="New Contact" />
                </div>
                <div style="text-align: right;">
                    <asp:Button ID="OKButton" runat="server" Text="Ok" SkinID="Plain" Width="100px" />
                </div>
                <input id="SelectedContacts" runat="server" visible="false" />
            </asp:Panel>
            <ajax:PopupControlExtender ID="PickerPopEx" runat="server" PopupControlID="PickerPanel" TargetControlID="LaunchPopup" Position="Bottom" />
        </asp:Panel>
   </ContentTemplate>
   <Triggers>
        <asp:AsyncPostBackTrigger ControlID="AddButton" EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="SearchButton" EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="AddNewContact" EventName="Click" />
    </Triggers>
</asp:UpdatePanel>

public partial class ContactPicker : System.Web.UI.UserControl
{
    protected void Page_Init(object sender, EventArgs e)
    {
        SearchButton.Click += new EventHandler(SearchButton_Click);
        AddButton.Click += new EventHandler(AddButton_Click);
        OKButton.Click += new EventHandler(OKButton_Click);
    }

    // Other code left out
}

搜索:
公共部分类ContactPicker:System.Web.UI.UserControl
{
受保护的无效页_Init(对象发送方,事件参数e)
{
SearchButton.Click+=新建事件处理程序(SearchButton\u Click);
AddButton.Click+=新建事件处理程序(AddButton\u Click);
确定按钮。单击+=新建事件处理程序(确定按钮\u单击);
}
//其他遗漏的代码
}

似乎在按钮定义中添加UseSubmitBehavior=“false”解决了我的问题。我仍然不知道为什么第一次点击按钮会起作用。

最可能的原因是.Net为其控件更改而生成的客户端ID。这些是动态分配的,可以在回发/部分回发之间更改


如果控件被动态添加到面板中,则回发之间的按钮ID可能不同,导致.Net无法将单击事件绑定到代码中的正确事件处理程序。

在我的情况下,我在使用
PostBackUrl
属性的
dgu ItemDataBound
事件处理程序中有一个
LinkButton


当我将
链接按钮
更改为
超链接
时,问题就消失了。

我就是这样。如果您设置动态添加的控件的ID以确保它们每次都是相同的,它会修复它