【问题标题】:CheckBoxList AJAX async postback issueCheckBoxList AJAX 异步回发问题
【发布时间】:2011-09-29 13:18:46
【问题描述】:

我有 2 个 CheckBoxList 控件 - chk1 和 chk2。如果选择了另一个,我需要使用异步回发来清除 CheckBoxList 的选择。如果有选择,则以下内容不会清除 chk1,并且检查了项目 chk2:

<asp:ScriptManager ID="sm1" runat="server" ></asp:ScriptManager>
    <asp:UpdatePanel ID="upd" runat="server">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="chk1" />
            <asp:AsyncPostBackTrigger ControlID="chk2" />
        </Triggers>
        <ContentTemplate>
            <asp:Label ID="result" runat="server" />
        </ContentTemplate>
    </asp:UpdatePanel>

    <div style="overflow: auto; height: 150px;">
        <asp:CheckBoxList runat="server" ID="chk1" OnDataBound="assignClickBehaviours" AutoPostBack="true">
            <asp:ListItem Value="1" Text="One"></asp:ListItem>
            <asp:ListItem Value="2" Text="Two"></asp:ListItem>
            <asp:ListItem Value="3" Text="Three"></asp:ListItem>
            <asp:ListItem Value="100" Text="..."></asp:ListItem>
            <asp:ListItem Value="150" Text="One hundred and Fifty"></asp:ListItem>
        </asp:CheckBoxList>
    </div>

    <div style="overflow: auto;">
        <asp:CheckBoxList runat="server" ID="chk2" AutoPostBack="true">
            <asp:ListItem Value="1" Text="One"></asp:ListItem>
            <asp:ListItem Value="2" Text="Two"></asp:ListItem>
            <asp:ListItem Value="3" Text="Three"></asp:ListItem>
        </asp:CheckBoxList>
    </div>

后面的代码:

protected void Page_Load(object sender, EventArgs e)
    {
        processChecks();
    }

    private void processChecks()
    {
        if(chk2.SelectedIndex>-1)
            chk1.ClearSelection();    
    }

如果将整个内容放在更新面板中,它会起作用...但是因为复选框中可以有 150 个项目,如果底部有项目,overflow:auto 上的滚动会弹回顶部被选中。我需要滚动状态保持不变(因此需要异步回发)。有什么想法或替代方案吗?

【问题讨论】:

    标签: c# asp.net asp.net-ajax asp.net-3.5


    【解决方案1】:

    你可以试试这个代码..

    <body>
    <form id="form1" runat="server">
    
      <asp:ScriptManager ID="sm1" runat="server" ></asp:ScriptManager>
    <asp:UpdatePanel ID="upd" runat="server">
        <ContentTemplate>
            <br />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="chk1">
            </asp:AsyncPostBackTrigger>
            <asp:AsyncPostBackTrigger ControlID="chk1">
            </asp:AsyncPostBackTrigger>
        </Triggers>
    
    </asp:UpdatePanel>
    
     <div style="overflow: auto; height: 150px;">
        <asp:CheckBoxList runat="server" ID="chk1"  AutoPostBack="true">
            <asp:ListItem Value="1" Text="One"></asp:ListItem>
            <asp:ListItem Value="2" Text="Two"></asp:ListItem>
            <asp:ListItem Value="3" Text="Three"></asp:ListItem>
            <asp:ListItem Value="100" Text="..."></asp:ListItem>
            <asp:ListItem Value="150" Text="One hundred and Fifty"></asp:ListItem>
        </asp:CheckBoxList>
    </div>
    
    <div style="overflow: auto;">
        <asp:CheckBoxList runat="server" ID="chk2" AutoPostBack="true">
            <asp:ListItem Value="1" Text="One"></asp:ListItem>
            <asp:ListItem Value="2" Text="Two"></asp:ListItem>
            <asp:ListItem Value="3" Text="Three"></asp:ListItem>
        </asp:CheckBoxList>
    </div>
    
    </form></body>
    

    【讨论】:

    • 抱歉,不确定这与我发布的不起作用的代码有何不同?
    【解决方案2】:

    请尝试使用此代码,

    1. 在两个复选框列表上启用自动回发。
    2. 然后将两个复选框列表嵌入到更新面板中。
    3. 在每个复选框的选中索引更改事件中包含 C# 代码,在这种情况下,它是您的 processChecks();

    请进行以下更改,注意 EventName 'SelectedIndexChanged'

    <asp:ScriptManager ID="sm1" runat="server" ></asp:ScriptManager>
            <asp:UpdatePanel ID="upd" runat="server">
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="chk1" EventName="SelectedIndexChanged"/>
                    <asp:AsyncPostBackTrigger ControlID="chk2" EventName="SelectedIndexChanged"/>
                </Triggers>
                <ContentTemplate>
                    <asp:Label ID="result" runat="server" />
                    <div style="overflow: auto; height: 150px;">
                       <asp:CheckBoxList runat="server" ID="chk1"  AutoPostBack="true">
                        <asp:ListItem Value="1" Text="One"></asp:ListItem>
                        <asp:ListItem Value="2" Text="Two"></asp:ListItem>
                        <asp:ListItem Value="3" Text="Three"></asp:ListItem>
                        <asp:ListItem Value="100" Text="..."></asp:ListItem>
                        <asp:ListItem Value="150" Text="One hundred and Fifty"></asp:ListItem>
                       </asp:CheckBoxList>
                    </div>
                    <div style="overflow: auto;">
        <asp:CheckBoxList runat="server" ID="chk2" AutoPostBack="true">
            <asp:ListItem Value="1" Text="One"></asp:ListItem>
            <asp:ListItem Value="2" Text="Two"></asp:ListItem>
            <asp:ListItem Value="3" Text="Three"></asp:ListItem>
        </asp:CheckBoxList>
    </div>
    
     </ContentTemplate>
    </asp:UpdatePanel>
    

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多