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

翻译|其它|编辑:郝浩|2007-09-17 10:20:15.000|阅读 2069 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

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>


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:csdn

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP