简体   繁体   English

单击按钮后的回发不会在UserControl中第一次没有反应

[英]Postback after clicking a button does not react not for the first time in a UserControl

I've a webapplication with UserControls. 我有一个带有UserControls的Web应用程序。 When I click on a menu-item or a select-button in a grid, than I see that didn't react the first time. 当我单击网格中的菜单项或选择按钮时,我发现第一次没有反应。 When I clicked for the second time then an event behind the button will fire. 当我第二次单击时,按钮后面的事件将触发。

What do I wrong? 我怎么了 I've given the components a unique ID and the events are in the cs-files. 我给组件一个唯一的ID,事件在cs文件中。

I hope someone can help me. 我希望有一个人可以帮助我。

thanks. 谢谢。


The problems is in the two following examples: 问题出在以下两个示例中:

 <asp:Menu ID="TabMenu" Width="100%" Height="25px" runat="server"         
                    Orientation="Horizontal" CssClass="TabPages" 
                    StaticEnableDefaultPopOutImage="False" 
                     AutoPostBack="true"  
                     OnMenuItemClick="DoMenuItemClick" > 
                <Items> 
                    <asp:MenuItem Text="Domains" Value="0"></asp:MenuItem> 

In this case for the first time, when you clicked on a menu-item, it looks like the page only is reloaded. 在这种情况下,第一次单击菜单项时,看起来只重新加载了页面。 And when clicks for the second time, the event really fires. 当第二次单击时,事件真正触发。 In this example when you clicks on a menu-item, the right View is shown. 在此示例中,当您单击菜单项时,将显示右侧的“视图”。

 <asp:GridView ID="gvwSelection" runat="server" 
        AllowPaging="True" 
        SelectedIndex="1"                 
        AutoGenerateSelectButton="True"
        OnSelectedIndexChanged="gvwSelectie_SelectedIndexChanged"
        OnSelectedIndexChanging="gvwSelectie_SelectedIndexChanging" >
        <PagerStyle ForeColor="#00257e" HorizontalAlign="Right" 
            BackColor="#FFFFFF"></PagerStyle>

    </asp:GridView>

Example 2: The first time you click on a selection button, then the row you touched is not selected. 示例2:第一次单击选择按钮时,那么未选中您触摸的行。 And after the first time everything works correct. 第一次之后,一切正常。

The ID's have a fixed name. ID具有固定名称。 The AutoPostBack is set to True. AutoPostBack设置为True。 There is an event linked. 有一个关联的事件。 The grids has DataBinded. 网格具有DataBinded。 There is site.master a default.aspx and the UserControls are placed in a Placeholder. 有site.master和default.aspx,并且UserControls放置在占位符中。

I hope someone can help me. 我希望有一个人可以帮助我。

I've found and solved the problem. 我已经找到并解决了问题。

I have a dynamic UserControl FAddress and everytime he is wearing another control. 我有一个动态的UserControl FAddress,每次他穿着另一个控件时。 So I added the following row in the method: FAddress.ID = "UserControl1"; 因此,我在方法中添加了以下行: FAddress.ID =“ UserControl1”;

The problem was, that the control was loading everytime. 问题是,控件每次都在加载。 By giving this dynamic control a fixed ID, this item has solved. 通过为该动态控件提供固定的ID,可以解决此问题。

private void LoadPage(string APageName)
{
    FAddress = null;
    PlaceholderAddressTemplate.Controls.Clear();

    if (!string.IsNullOrEmpty(APageName))
    {
        FAddress = (UserControl)LoadControl(string.Format("~/UserControls/{0}.ascx",
                    APageName));

        if (FAddress != null)
        {
            FAddress.ID = "UserControl1";

            PlaceholderAddressTemplate.Controls.Add(FAddress);
            ShowOrHideComponents();
            FAddress.Focus();
        }
        else
            ShowOrHideComponents();
    }
    else
    ShowOrHideComponents();
}

确保在按钮后面将autopostback属性设置为true,这可能会解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM