2011-09-29 41 views
0

我有2個CheckBoxList控件 - chk1和chk2。如果選擇另一個,我需要使用異步回發來清除CheckBoxList的選擇。下面將如果它有選擇,項目CHK2是檢查不明確CHK1:CheckBoxList AJAX異步回發問題

<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會滾動回到頂部。我需要保持滾動狀態(因此需要異步回發)。任何想法或選擇?

回答

0

你可以試試這個代碼..

<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

遺憾不知道這與我發佈的代碼有什麼不同? – billfredtom

0

請使用此代碼嘗試,

  1. 啓用自動回傳兩個複選框列表。
  2. 然後在更新面板中嵌入兩個複選框列表。
  3. 包括內選擇的每個複選框的指數變化的情況下的C#代碼,在這種情況下,它是你的processChecks();

請做如下修改,注意事件名稱「的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>