如何在GridView中添加CheckBox控件列,添加删除确认对话框

 

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

public   partial   class  GridViewCheckBox : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
            GridViewDataBind();
    }


    
private void GridViewDataBind()
    
{
        
string connStr = ConfigurationManager.ConnectionStrings["northwind"].ConnectionString;
        SqlConnection conn 
= new SqlConnection(connStr);
        SqlDataAdapter da 
= new SqlDataAdapter("select firstname, lastname, city from employees", conn);
        conn.Open();
        DataSet ds 
= new DataSet();
        
try
        
{
            da.Fill(ds, 
"testTable");
            dgCheckBox.DataSource 
= ds.Tables["testTable"].DefaultView;
            dgCheckBox.DataBind();
        }

        
catch (Exception error)
        
{
            Response.Write(error.ToString());
        }

        
finally 
        
{
            conn.Close();
        }

    }

    
protected void cmdSelectAll_Click(object sender, EventArgs e)
    
{
        System.Web.UI.WebControls.CheckBox chkEport;
        
if (cmdSelectAll.Text == "全选")
        
{
            
foreach (GridViewRow gridviewrow in dgCheckBox.Rows)
            
{
                chkEport 
= (CheckBox)gridviewrow.FindControl("chkEport");
                chkEport.Checked 
= true;
            }

            cmdSelectAll.Text 
= "取消";
        }

        
else
        
{
            
foreach (GridViewRow gridviewrow in dgCheckBox.Rows)
            
{
                chkEport 
= (CheckBox)gridviewrow.FindControl("chkEport");
                chkEport.Checked 
= false;
            }

            cmdSelectAll.Text 
= "全选";
        }

    }

    
protected void cmdFindSelected_Click(object sender, EventArgs e)
    
{
        System.Web.UI.WebControls.CheckBox chkEport;
        
string strID;
        System.Text.StringBuilder strMsg 
= new System.Text.StringBuilder("选中项的City字段值分别为:<hr color=red>");
        
foreach (GridViewRow gridviewrow in dgCheckBox.Rows)
        
{
            chkEport 
= (CheckBox)gridviewrow.FindControl("chkEport");
            
if (chkEport.Checked)
            
{
                strID 
= ((Label)(gridviewrow.FindControl("lblCity"))).Text;
                strMsg.Append(strID 
+ "<br><hr color=red>");
            }

            message.Text 
= strMsg.ToString();
        }

    }

    
protected void dgCheckBox_RowCreated(object sender, GridViewRowEventArgs e)
    
{
        
if (e.Row.RowType == DataControlRowType.DataRow)
        
{
            TableCell myCell;
            myCell 
= e.Row.Cells[4];
            LinkButton btnDel 
= (LinkButton)myCell.Controls[0];
            btnDel.Attributes.Add(
"onclick""return confirm('您真的要删除此行吗?');");
            btnDel.Text 
= "删除";
        }

    }

    
protected void dgCheckBox_RowDeleting(object sender, GridViewDeleteEventArgs e)
    
{
        Response.Write(
"删除");
    }

}

GridViewCheckBox.aspx如下:

         < table style = " width: 566px " >
            
< tr >
                
< td style = " width: 112px "   >
                    
< asp:GridView ID = " dgCheckBox "  runat = " server "  GridLines = " vertical "  AutoGenerateColumns = " false "  PagerSettings - Mode = " numeric "  Width = " 326px "  OnRowCreated = " dgCheckBox_RowCreated "   OnRowDeleting = " dgCheckBox_RowDeleting " >
                    
< SelectedRowStyle Font - Bold = " True "  ForeColor = " White "  BackColor = " #008A8C " />
                    
< AlternatingRowStyle BackColor = " AppWorkspace " />
                    
< RowStyle ForeColor = " ActiveBorder "  BackColor = " ActiveCaption "   />
                    
< HeaderStyle Font - Bold = " True "  ForeColor = " ActiveCaptionText "  BackColor = " AliceBlue "   />
                    
< FooterStyle ForeColor = " AppWorkspace "  BackColor = " Aqua "   />
                    
< Columns >
                    
< asp:TemplateField >
                    
< ItemTemplate >
                    
< asp:CheckBox ID = " chkEport "  runat = " server "   />
                    
</ ItemTemplate >
                    
< EditItemTemplate >
                    
< asp:CheckBox ID = " chkEportON "  runat = " server "   />
                    
</ EditItemTemplate >
                    
</ asp:TemplateField >
                    
< asp:BoundField DataField = " lastname "  HeaderText = " lastname " />
                    
< asp:BoundField DataField = " firstname "  HeaderText = " firstname "   />
                    
< asp:TemplateField >
                    
< ItemTemplate >
                    
< asp:Label ID = " lblCity "  runat = " server "  Text = ' <%#Eval("city") %> ' ></ asp:Label >
                    
</ ItemTemplate >
                    
</ asp:TemplateField >
                    
< asp:ButtonField Text = " 删除 "  CommandName = " delete " />
                    
</ Columns >
                    
< PagerStyle HorizontalAlign = " Center "  ForeColor = " Aquamarine "  BackColor = " Azure " />
                    
</ asp:GridView >
                
</ td >
            
</ tr >
            
< tr >
            
< td style = " width: 112px " >
            
< asp:Button ID = " cmdSelectAll "  runat = " server "  Text = " 全选 "  OnClick = " cmdSelectAll_Click "   />& nbsp; < asp:Button ID = " cmdFindSelected "  runat = " server "  Text = " 取得选择的项 "  OnClick = " cmdFindSelected_Click "   />
            
</ td >
            
</ tr >< tr >
            
< td >< asp:Label ID = " message "  runat = " server " ></ asp:Label ></ td >
            
</ tr >
        
</ table >

GridViewCheckBox.aspx.cs如下:

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值