更新面板无法正常工作

Nov*_*Net 0 asp.net updatepanel ajax.net

我有两个列表框执行添加删除项目功能,由四个按钮控制和o每个按钮点击那里碰巧回发但我不希望它是闪烁我正在使用这样的更新面板但它这个问题让我解释了这个问题

<asp:UpdatePanel ID="button" runat="server" UpdateMode="Always">
    <ContentTemplate>
        <asp:Button ID="ButtonAdd" runat="server" Text=">" OnClick="ButtonAdd_Click"  Width="50px"/><br />
        <asp:Button ID="ButtonRemove" runat="server" Text="<" OnClick="ButtonRemove_Click" Width="50px"/><br />
        <asp:Button ID="ButtonAddAll" runat="server" Text =">>>" OnClick="ButtonAddAll_Click" Width="50px"/><br />    
        <asp:Button ID="ButtonRemoveAll" runat="server" Text ="<<<" OnClick="ButtonRemoveAll_Click" Width="50px"/>
    </ContentTemplate>
</asp:UpdatePanel>
Run Code Online (Sandbox Code Playgroud)

Kel*_*sey 8

我写了一个确实有用的快速示例.你不需要你的按钮UpdatePanel.您只需要ListBox因为它们是唯一的刷新控件.设置TriggerUpdatePanel将导致刷新发生没有' 闪烁 '.

aspx代码:

<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
    <asp:Button ID="ButtonAdd" runat="server" Text=">" OnClick="ButtonAdd_Click"  Width="50px"/><br /> 
    <asp:Button ID="ButtonRemove" runat="server" Text="<" OnClick="ButtonRemove_Click" Width="50px"/><br /> 
    <asp:Button ID="ButtonAddAll" runat="server" Text =">>>" OnClick="ButtonAddAll_Click" Width="50px"/><br />     
    <asp:Button ID="ButtonRemoveAll" runat="server" Text ="<<<" OnClick="ButtonRemoveAll_Click" Width="50px"/>     
    <asp:UpdatePanel ID="button" runat="server" UpdateMode="Always">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="ButtonAdd" EventName="Click" />
            <asp:AsyncPostBackTrigger ControlID="ButtonRemove" EventName="Click" />
            <asp:AsyncPostBackTrigger ControlID="ButtonAddAll" EventName="Click" />
            <asp:AsyncPostBackTrigger ControlID="ButtonRemoveAll" EventName="Click" />
        </Triggers>
        <ContentTemplate>
            <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
            &nbsp;&nbsp;
            <asp:ListBox ID="ListBox2" runat="server"></asp:ListBox>
        </ContentTemplate> 
    </asp:UpdatePanel>
</div>
Run Code Online (Sandbox Code Playgroud)

cs(代码隐藏)代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ListBox1.Items.Add(new ListItem("Test1", "1"));
        ListBox1.Items.Add(new ListItem("Test2", "2"));
        ListBox1.Items.Add(new ListItem("Test3", "3"));
        ListBox1.Items.Add(new ListItem("Test4", "4"));
        ListBox1.Items.Add(new ListItem("Test5", "5"));
    }
}

protected void ButtonRemove_Click(object sender, EventArgs e)
{
    if (ListBox2.SelectedItem != null)
    {
        ListBox1.Items.Add(ListBox2.SelectedItem);
        ListBox2.Items.RemoveAt(ListBox2.SelectedIndex);
        ListBox2.ClearSelection();
        ListBox1.ClearSelection();
    }
}

protected void ButtonAdd_Click(object sender, EventArgs e)
{
    if (ListBox1.SelectedItem != null)
    {
        ListBox2.Items.Add(ListBox1.SelectedItem);
        ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
        ListBox1.ClearSelection();
        ListBox2.ClearSelection();
    }
}
Run Code Online (Sandbox Code Playgroud)

我测试了这个,它确实有效.我只实现了两个Buttons来提供一个完整的例子.