2017-06-15 84 views
1

問題是當我檢查複選框它很好,但是當我檢查下一頁上的複選框。以前的頁面複選框會被取消選中。 複選框選中/未選中事件觸發CheckedChanged事件。但是當我在列表視圖的下一頁的列表視圖中選中複選框時,取消選中列表視圖上一頁的複選框。在分頁列表視圖CheckBox_CheckedChanged事件不起作用

ListView.aspx代碼

<table class=" example1 table table-bordered table-striped"> 
<thead> 
     <tr> 
<th>Sr no.</th> 
        <th>Parent Category</th> 
        <th>Title</th> 
        <th>Description</th> 
        <th>Image</th> 
        <th>Show on Homepage</th> 
        <th>Edit</th> 
        <th>Delete</th> 
</tr> 
</thead> 
     <tbody> 
     <asp:ListView ID="ListCourse" runat="server" OnItemCommand="ListCourse_ItemCommand" DataKeyNames="CID"> 
<LayoutTemplate> 
        <tr id="ItemPlaceholder" runat="server"> 
        </tr> 
        </LayoutTemplate> 
        <ItemTemplate> 
         <tr class="gradeA"> 
          <td> 
           <asp:Label ID="lblSrno" runat="server" Text='<%# Container.DataItemIndex+1 %>'></asp:Label> 
</td> 
<td> 
                <asp:Label ID="lbl" runat="server" Text='<%# GetCourse(Convert.ToInt32(Eval("CatID"))) %>'></asp:Label> 
               </td> 
               <td> 
                <asp:Label ID="Lbltitle" runat="server" Text='<%# Eval("Title") %>'></asp:Label> 
                <asp:Label ID="lablc" runat="server" Visible="false" Text='<%# Eval("CID") %>'></asp:Label> 
               </td> 
               <td> 
                <asp:Label ID="lblDescrption" runat="server" Text='<%# (Eval("Description").ToString().Length <=200)?Eval("Description").ToString(): Eval("Description").ToString().Substring(0, 200) + "..."%>'></b></asp:Label> 
               </td> 
<td> 
                <img class="img_show " src="/Gallery/<%# Eval("Image")%>"> 
               </td> 
               <td> 
                <asp:CheckBox ID="CheckCourse" runat="server" Style="margin-left: 50px;" OnCheckedChanged="CheckCourse_CheckedChanged" AutoPostBack="true" /> 
               </td> 
               <td> 
                <asp:LinkButton ID="LinkEdit" runat="server" PostBackUrl='<%# "Add_New_Course.aspx?ID="+ Eval("CID")%>'>Edit</asp:LinkButton> 
               </td> 
               <td> 
                <asp:LinkButton ID="LinkDelete" runat="server" CommandName="DeleteCourse" CommandArgument='<%# Eval("CID") %>' OnClientClick='return confirm("Do you want to delete record ??")'> Delete</asp:LinkButton> 
               </td> 
              </tr> 
             </ItemTemplate> 
            </asp:ListView> 
           </tbody> 
          </table> 

代碼隱藏的CheckedChanged

protected void CheckCourse_CheckedChanged(object sender, EventArgs e) 
     { 
      CheckBox checkhome = (CheckBox)sender; 
      ListViewItem item = (ListViewItem)checkhome.NamingContainer; 
      ListViewDataItem dataItem = (ListViewDataItem)item; 
      string code = ListCourse.DataKeys[dataItem.DisplayIndex].Value.ToString(); 
      int CID = Convert.ToInt32(code); 
      Course_Master objnew = DB.Course_Master.Single(p => p.CID == CID); 
      bool IsHome = CheckOnHome(CID); 
      if (IsHome == true) 
      { 
       if (checkhome.Checked == false) 
       { 
        objnew.ShowOnHomePage = false; 
       } 
      } 
      else 
      { 
       if (checkhome.Checked == true) 
       { 
        objnew.ShowOnHomePage = true; 
       } 
      } 
      DB.SaveChanges(); 
     } 

回答

0

謝謝你們的幫助。我使用一些簡單的過程來解決它,方法是從ListView中刪除複選框並添加文本,而不是複選框是否被選中(是/否)。正如我認爲這將是解決我的問題最簡單的方法。

0

它不火,因爲當回發火災的服務器不知道之前的複選框狀態,所以它不知道它是否被更改過。

嘗試設置默認值設置爲false,它應該工作

<asp:CheckBox ID="CheckCourse" runat="server" Checked="false" Style="margin-left: 50px;" OnCheckedChanged="CheckCourse_CheckedChanged" AutoPostBack="true" /> 
+1

謝謝@Matt ..但它不工作.. :(..當我檢查列表視圖1頁上的複選框,它取消選中列表視圖2頁上的複選框,反之亦然。否則所有工作正常...請幫助.. –

+0

@JayPatel嘗試添加一個OnItemDataBound到列表視圖..在onitemdatabound裏面你設置checked = false或者true爲每個項目..你有沒有在該列表視圖上的頁面? – Matt

+0

@ Matt ...我已經添加OnItemDataBound to listview ..和Set Checked = true或false根據數據庫中的數據。 我在Listview中尋呼。錯誤僅在那裏..當我在Listview頁面上檢查一個項目時1其他頁面項目沒有選中.. don 'y知道爲什麼會發生這種情況.. –

0

您需要從列表中選中某個項目的保存IDS。當你移動到ListView的頁面2時,它失去了以前的狀態。 將數據存儲在視圖狀態並從那裏加載。要讀取並綁定它,您需要處理ListView的PagePropertiesChanging和ItemDataBound事件處理程序。

下面是關於Maintaining the state of checkboxes in ListView

請給+1,如果它幫助了一個很好的解釋。乾杯!

+0

感謝您的幫助。 –

+0

不客氣,請不要忘記投票答案,如果它幫助。乾杯!:) –