首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CheckBoxList AJAX异步回发问题

问CheckBoxList AJAX异步回发问题
EN

Stack Overflow用户
提问于 2011-09-29 21:18:46
回答 2查看 5.7K关注 0票数 0

我有两个CheckBoxList控件-- chk1和chk2。如果选择了另一个CheckBoxList,则需要使用异步回发来清除对该use的选择。如果有选择,并且选中了项目chk2,则不会清除chk1:

代码语言:javascript
复制
<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>

代码隐藏:

代码语言:javascript
复制
protected void Page_Load(object sender, EventArgs e)
    {
        processChecks();
    }

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

如果把整个东西放到更新面板里,它就能工作了.但由于复选框中可能有150个项目,如果选中了底部的项目,则overflow:auto上的滚动将弹回顶部。我需要滚动状态保持不动(因此需要异步回发)。有什么想法或选择吗?

EN

回答 2

Stack Overflow用户

发布于 2011-09-29 21:41:26

你可以试试这段代码..

代码语言:javascript
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2011-10-17 12:22:29

请尝试使用此代码,

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

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

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7597914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档