ASPxGridView 結合CheckBox實現多選(在後臺事件代碼控制)

本篇介紹:ASPxGridView 結合CheckBox實現多選 ,實質跟gridview是一樣的:遍歷ASPxGridView 找到每行的CheckBox,讓後做處理。不過ASPxGridView 在遍歷的時候有很多地方和gridview是不同的,對於和我一樣對ASPxGridView 不熟悉的人來說,要花很多的時間去找和適的屬性。
    其實ASPxGridView 官方也提供了實現全選的例子不過那個似乎是不能綁定數據庫的。

    遍歷ASPxGridView 時findcontrol要注意要用 (CheckBox)ASPxGridView1.FindRowCellTemplateControl(i, (GridViewDataColumn)ASPxGridView1.Columns["SelectAll"], "CheckBox1")來取得某行的CheckBox
還有記得要把head的CheckBox的設置AutoPostBack="True" ,負責當點擊CheckBox時相當於head的click事件。

下面是詳細代碼:
後臺代碼:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using DevExpress.Web.ASPxGridView;


public partial class 結合checkbox_Default : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {

    }
    
protected void selectAll_CheckedChanged(object sender, EventArgs e)
    {
        
int startid = ASPxGridView1.PageIndex * ASPxGridView1.SettingsPager.PageSize;
        
int endid;

        
if (ASPxGridView1.PageIndex == ASPxGridView1.PageCount - 1)
        {
            endid 
= ASPxGridView1.VisibleRowCount;
        }
        
else
        {
            endid 
= (ASPxGridView1.PageIndex + 1* ASPxGridView1.SettingsPager.PageSize;
        }

        CheckBox cbHead 
= (CheckBox)sender;
        
if (cbHead.Checked == true)
        {
            CheckBox cb;
            
for (int i = startid; i < endid; i++)
            {
                cb 
= (CheckBox)ASPxGridView1.FindRowCellTemplateControl(i, (GridViewDataColumn)ASPxGridView1.Columns["SelectAll"], "CheckBox1");
                cb.Checked 
= true;
            }
        }
        
else
        {
            CheckBox cb;
            
for (int i = startid; i < endid; i++)
            {
                cb 
= (CheckBox)ASPxGridView1.FindRowCellTemplateControl(i, (GridViewDataColumn)ASPxGridView1.Columns["SelectAll"], "CheckBox1");
                cb.Checked 
= false;
            }
        }
    }
}

 


前臺代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="結合checkbox_Default" %>

<%@ Register assembly="DevExpress.Web.ASPxGridView.v7.3, Version=7.3.7.0, Culture=neutral, PublicKeyToken=6d2d307aee1dfbe0" namespace="DevExpress.Web.ASPxGridView" tagprefix="dxwgv" %>
<%@ Register assembly="DevExpress.Web.ASPxEditors.v7.3, Version=7.3.7.0, Culture=neutral, PublicKeyToken=6d2d307aee1dfbe0" namespace="DevExpress.Web.ASPxEditors" tagprefix="dxe" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
        
<dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" 
            AutoGenerateColumns
="False" DataSourceID="SqlDataSource1" 
            KeyFieldName
="ProductID">
            
<Columns>
                
<dxwgv:GridViewDataTextColumn FieldName="ProductID" ReadOnly="True" 
                    VisibleIndex
="0">
                    
<EditFormSettings Visible="False" />
                
</dxwgv:GridViewDataTextColumn>
                
<dxwgv:GridViewDataTextColumn FieldName="ProductName" VisibleIndex="1">
                
</dxwgv:GridViewDataTextColumn>
                
<dxwgv:GridViewDataCheckColumn FieldName="Discontinued" VisibleIndex="2">
                
</dxwgv:GridViewDataCheckColumn>
                
<dxwgv:GridViewDataTextColumn FieldName="SelectAll" VisibleIndex="3">
                    
<DataItemTemplate>
                        
<asp:CheckBox ID="CheckBox1" runat="server" 
                            Checked
='<%# Eval("Discontinued") %>' />
                    
</DataItemTemplate>
                    
<HeaderTemplate>
                        
<asp:CheckBox ID="selectAll" runat="server" AutoPostBack="True" 
                            oncheckedchanged
="selectAll_CheckedChanged" />
                        SelectAll
                    
</HeaderTemplate>
                
</dxwgv:GridViewDataTextColumn>
            
</Columns>
        
</dxwgv:ASPxGridView>
    
    
</div>
    
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString
="<%$ ConnectionStrings:NorthwindConnectionString %>" 
        SelectCommand
="SELECT [ProductID], [ProductName], [Discontinued] FROM [Products] ORDER BY [ProductID]">
    
</asp:SqlDataSource>
    
</form>
</body>
</html>

 

數據庫有的是Nothwind表是Products

發佈了11 篇原創文章 · 獲贊 6 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章