更新面板无法正常工作

时间:2010-08-14 11:11:27

标签: 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>

3 个答案:

答案 0 :(得分:8)

我写了一个确实有用的快速示例。您不需要UpdatePanel中的按钮。您只需要ListBox,因为它们是唯一正在刷新的控件。设置Trigger的{​​{1}}会导致刷新,而不会出现“闪烁”。

aspx代码:

UpdatePanel

cs(codebehind)代码:

<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>

我已对此进行了测试,但确实有效。我只实现了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(); } } 中的两个来提供一个完整的例子。

答案 1 :(得分:0)

asp:ListBox添加到ContentTemplate的{​​{1}},或添加新的asp:UpdatePanel并将其添加到asp:UpdatePanel。如果控件未置于ContentTemplate内,则不会使用异步回发更新控件。

使用以下代码段,新添加的asp:UpdatePanel将使用异步回发更新新内容:

Page.aspx

listBox

代码隐藏文件中的示例事件处理程序, Page.aspx.cs

<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" />

        <asp:ListBox runat="server" ID="listBox"></asp:ListBox>
    </ContentTemplate>
</asp:UpdatePanel>

答案 2 :(得分:-1)

<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" />

        <asp:ListBox runat="server" ID="listBox"></asp:ListBox>
    </ContentTemplate>
</asp:UpdatePanel>