c#大圣之路笔记——c# DataGrid checkbox 操作
2016-06-29 11:51
453 查看
///html <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="xOEMStoreNominationForCAM.aspx.cs" EnableEventValidation="false" Inherits="PRCSales_external.Store.xOEMStoreNominationForCAM" %> <!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>xOEM Store Nomination for CAM</title> <link href="../css/main.css" rel="stylesheet" type="text/css" /> <link href="../css/Styles_1.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="../js/jquery-1.8.2.js"></script> <script type="text/javascript" src="../js/LocationFilterQuarter.js"></script> <script type="text/javascript"> function CheckAll(chkObject) { var checkArray = document.getElementsByTagName("input"); for (j = 0; j < checkArray.length; j++) { if (checkArray[j].type == "checkbox" & checkArray[j] != chkObject ) { if (chkObject.checked != checkArray[j].checked) checkArray[j].checked = chkObject.checked; } } } function ShowWaiting() { document.getElementById('doing').style.visibility = 'visible'; } function CloseWaiting() { document.getElementById('doing').style.visibility = 'hidden'; } function MyOnload() { document.getElementById('doing').style.visibility = 'hidden'; } if (window.onload == null) { window.onload = MyOnload; } </script> <style type="text/css"> #btnMark ,#btnUnmark { margin-left:5px; } </style> </head> <body> <form id="form1" runat="server"> <table width="100%" id="Table1"> <tr ><td style="height:30px; "><b>xOEM Store Nomination for CAM</b></td></tr> <tr> <tr style="HEIGHT: 1px; BACKGROUND-COLOR: #3366ff"> <td colSpan="4"><FONT style="BACKGROUND-COLOR: #ffffff" face="宋体"></FONT><FONT face="宋体"></FONT></td> </tr> <tr> <td class="fieldtitle" width="30%">Quarter:</td> <td class="fieldtitle" width="20%"><asp:dropdownlist id="ddlQuarter" runat="server" AutoPostBack="true" onselectedindexchanged="ddlQuarter_SelectedIndexChanged" onchange="initData('region')"></asp:dropdownlist></td> <td class="fieldtitle" width="20%"> Region/Grid/City: </td> <td class="fieldtitle" colspan="3" width="30%"> <asp:DropDownList ID="ddlRegion" runat="server" CssClass="fieldList" onchange="initData('subregion')"> </asp:DropDownList> <asp:DropDownList ID="ddlSubRegion" runat="server" CssClass="fieldList" style="display:none;" onchange="initData('grid')"> </asp:DropDownList> <asp:DropDownList ID="ddlGrid" runat="server" CssClass="fieldList" onchange="initData('pref')"> </asp:DropDownList> <asp:DropDownList ID="ddlPref" runat="server" CssClass="fieldList" style="display:none;" onchange="initData('city')"> </asp:DropDownList> <asp:DropDownList ID="ddlCity" runat="server" CssClass="fieldList" onchange="initData('county')"> </asp:DropDownList> <asp:DropDownList ID="ddlCounty" runat="server" CssClass="fieldList" style="display:none;" onchange="initData('town')"> </asp:DropDownList> <asp:DropDownList ID="ddlTown" runat="server" CssClass="fieldList" style="display:none;" onchange="initData('village')"> </asp:DropDownList> <asp:DropDownList ID="ddlVillage" runat="server" CssClass="fieldList" style="display:none;" onchange="initData('village1')"> </asp:DropDownList> <asp:TextBox ID="txtWWID" runat="server" Style="display: none"></asp:TextBox> <asp:TextBox ID="txtQuarter" runat="server" Style="display: none"></asp:TextBox> <asp:TextBox ID="txtRegion" runat="server" Style="display: none"></asp:TextBox> <asp:TextBox ID="txtSubRegion" runat="server" Style="display: none"></asp:TextBox> <asp:TextBox ID="txtGrid" runat="server" Style="display: none"></asp:TextBox> <asp:TextBox ID="txtPref" runat="server" Style="display: none"></asp:TextBox> <asp:TextBox ID="txtCity" runat="server" Style="display: none"></asp:TextBox> <asp:TextBox ID="txtCounty" runat="server" Style="display: none"></asp:TextBox> <asp:TextBox ID="txtTown" runat="server" Style="display: none"></asp:TextBox> <asp:TextBox ID="txtVillage" runat="server" Style="display: none"></asp:TextBox> </td> </tr> <tr> <td class="fieldtitle" width="30%" >Store Name:</td> <td class="fieldtitle" width="20%" colspan="1"><asp:TextBox ID="txtStorNm" runat="server"></asp:TextBox></td> <td class="fieldtitle" width="20%" >Store ID:</td> <td class="fieldtitle" width="30%"><asp:TextBox ID="txtStorID" runat="server"></asp:TextBox></td> </tr> <%-- <tr> <td class="fieldtitle" width="20%" colspan=""><asp:CheckBox ID="chkStore" runat="server" CssClass="fieldtitle" Text="Show Unassigned Store Only"/></td> </tr>--%> <tr style="HEIGHT: 1px; BACKGROUND-COLOR: #3366ff"> <td colSpan="4"><FONT style="BACKGROUND-COLOR: #ffffff" face="宋体"></FONT><FONT face="宋体"></FONT></td> </tr> <tr> <td class="fieldtitle" width="30%" colspan="1">Store list <asp:Label ID="lblCount" runat="server" CssClass="fieldList"></asp:Label> <%-- <asp:Label ID="lblMessage" runat="server" CssClass="fieldList" ForeColor="Red" Text="季度店面标记"></asp:Label> --%> <asp:Label ID="lblLeftTime" CssClass="fieldList" runat="server" ForeColor="Red"></asp:Label> </td> <td align="right" colspan="3" width="70%"> <asp:Button ID="btnSearch" runat="server" Text="Search" CssClass="fieldButton" onclick="btnSearch_Click" Width="80px"/> <asp:Button ID="btnMark" runat="server" Text="Mark" CssClass="fieldButton" onclick="btnMark_Click" Width="80px" /> <asp:Button ID="btnUnmark" runat="server" Text="Unmark" CssClass="fieldButton" onclick="btnUnmark_Click" Width="80px" /> </td> </tr> <tr> <td colspan="4"> <asp:DataGrid ID="GridMain" runat="server" Width="100%" AutoGenerateColumns="false" PageSize="20" AllowPaging="true" onselectedindexchanged="GridMain_SelectedIndexChanged" onpageindexchanged="GridMain_PageIndexChanged"> <AlternatingItemStyle CssClass="datagridAlternating" /> <HeaderStyle CssClass="tableHead" /> <Columns> <asp:TemplateColumn> <HeaderStyle Width="20px"></HeaderStyle> <HeaderTemplate><asp:CheckBox ID ="checkAll" runat="server" onclick="CheckAll(this);" /></HeaderTemplate> <ItemTemplate><asp:CheckBox ID="chkSelected" runat="server" /></ItemTemplate> </asp:TemplateColumn> <asp:BoundColumn DataField="stor_rgn_nm" HeaderText="Store Region"> <ItemStyle HorizontalAlign="Center" Width="4%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="stor_grid_cd" HeaderText="Store Grid"> <ItemStyle HorizontalAlign="Center" Width="3%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="stor_city_std_loc_nm" HeaderText="Store City"> <ItemStyle HorizontalAlign="Center" Width="4%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="stor_id" HeaderText="Store ID"> <ItemStyle HorizontalAlign="Center" Width="3%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="stor_nm" HeaderText="Store Name"> <ItemStyle HorizontalAlign="Center" Width="8%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="stor_addr" HeaderText="Store Addr"> <ItemStyle HorizontalAlign="Center" Width="8%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="brnd_nm" HeaderText="Store Brand"> <ItemStyle HorizontalAlign="Center" Width="4%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="xoem_store_tier" HeaderText="Store Tier"> <ItemStyle HorizontalAlign="Center" Width="3%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="remark" HeaderText="Store Bis Type"> <ItemStyle HorizontalAlign="Center" Width="4%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="oem_stor_id" HeaderText="OEM Store ID"> <ItemStyle HorizontalAlign="Center" Width="4%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="rep_nm" HeaderText="Store Manager"> <ItemStyle HorizontalAlign="Center" Width="3%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="slsprs_nm" HeaderText="SR Owner"> <ItemStyle HorizontalAlign="Center" Width="3%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="stor_sts" HeaderText="Store Status"> <ItemStyle HorizontalAlign="Center" Width="3%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="chnl_rgn_nm" HeaderText="Chnl Region"> <ItemStyle HorizontalAlign="Center" Width="4%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="chnl_grid_cd" HeaderText="Chnl Grid"> <ItemStyle HorizontalAlign="Center" Width="3%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="chnl_city_std_loc_nm" HeaderText="Chnl City"> <ItemStyle HorizontalAlign="Center" Width="4%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="gid_num" HeaderText="Chnl GID"> <ItemStyle HorizontalAlign="Center" Width="4%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="co_id" HeaderText="Chnl ID"> <ItemStyle HorizontalAlign="Center" Width="3%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="co_nm" HeaderText="Chnl Name"> <ItemStyle HorizontalAlign="Center" Width="7%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="co_addr" HeaderText="Chnl Addr"> <ItemStyle HorizontalAlign="Center" Width="7%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="co_cntct_person" HeaderText="Chnl Manager"> <ItemStyle HorizontalAlign="Center" Width="3%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="co_tel" HeaderText="Chnl Tel"> <ItemStyle HorizontalAlign="Center" Width="4%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="mbr_type_nm" HeaderText="Chnl Type"> <ItemStyle HorizontalAlign="Center" Width="3%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="chnl_sts" HeaderText="Chnl Status"> <ItemStyle HorizontalAlign="Center" Width="3%"></ItemStyle> </asp:BoundColumn> </Columns> <PagerStyle Position="TopAndBottom" Mode="NumericPages" /> </asp:DataGrid> </td> </tr> </table> </form> <div id='doing' style='z-index: 12000; left: 0px; width: 100%; cursor: wait; position: absolute; top: 0px; height: 100%; filter: alpha(opacity=45); opacity: 0.5; color: #000000; background-color: #FFFFFF;'> <table width='100%' height='100%' id="Table2"> <tr align='center' valign='middle'> <td> <table id="Table3" class="loading"> <tr align='center' valign='middle'> <td> <img src="../Images/Waitting.gif" /> </td> <td> </td> <td valign="middle"> <span id=txtLoading0 style="font-size:14px; color: #800080; font-weight: bold;">页面正在加载数据,请稍候...</span> <br><span id=txtLoading1 style="font-size:14px; color: #800080;">Loading,please wait...</span> </td> </tr> </table> </td> </tr> </table> </div> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CompIntelligenceWeb.User; using System.Data; using Intel.PRCSMS.Common; using System.Data.SqlClient; using Microsoft.ApplicationBlocks.Data; namespace PRCSales_external.Store { public partial class xOEMStoreNominationForCAM : PageBase { private DataTable dataTable; private string tableName; private DateTime _openDate; private DateTime _cutOffDate; protected void Page_Load(object sender, EventArgs e) { this.btnSearch.Attributes.Add("onclick", "ShowWaiting();"); this.btnMark.Attributes.Add("onclick", "ShowWaiting();"); this.btnUnmark.Attributes.Add("onclick", "ShowWaiting();"); if (!IsPostBack) { ViewState["WWID"] = ((AccessControler)this.Session["_accessctrl"]).GetWWID(); this.txtWWID.Text = ViewState["WWID"].ToString(); initData(); initTable(); gridBind(); bindNextPage(0); if (ViewState["actionFlag"].ToString()=="1") { this.btnMark.Enabled = true; this.btnUnmark.Enabled = true; } else { this.btnMark.Enabled = false; this.btnUnmark.Enabled = false; } } else { tableName = ViewState["TableName"].ToString(); dataTable = (DataTable)ViewState["DataTable"]; } } private void bindNextPage(int p) { tableName = ViewState["TableName"].ToString(); dataTable = (DataTable)ViewState["DataTable"]; GridMain.DataSource = dataTable.DefaultView; GridMain.CurrentPageIndex = p; GridMain.DataBind(); ///标记Style设置 NotedStyle(); } private void initData() { SetCutOffDate(); BindQuarter(); } /// <summary> /// 标记功能的时间控制 /// </summary> private void SetCutOffDate() { string sql = "SELECT TOP 1 open_dt, cls_dt,* FROM prc_frz_dt WHERE (frz_dt_type_id = 27) ORDER BY yyyyqq desc "; ViewState["actionFlag"] = ""; using (IDataReader dr = SqlHelper.ExecuteReader(Application["DSN"].ToString(),CommandType.Text,sql)) { if (dr.Read()) { _openDate = Convert.ToDateTime(dr["open_dt"]); _cutOffDate = Convert.ToDateTime(dr["cls_dt"]); if (_openDate >DateTime.Now) { lblLeftTime.Text = "暂时无法对店面进行标记,该操作尚未开始"; } else if (_cutOffDate <DateTime.Now) { lblLeftTime.Text = "店面标记操作已结束"; } else { TimeSpan countDownSpan = _cutOffDate - DateTime.Now; //if (countDownSpan.Days -7 <0) //{ // lblLeftTime.Text = string.Format("距离店面可标记操作截止日还有{0}天{1}小时。",0,0); //} //else //{ lblLeftTime.Text = string.Format("距离店面可标记操作截止日还有{0}天{1}小时",countDownSpan.Days,countDownSpan.Hours); //} ViewState["actionFlag"] = 1; } } else { lblLeftTime.Text = "店面标记的操作时间还未设定"; ViewState["actionFlag"] = 0; } } } private void BindQuarter() { ddlQuarter.DataSource = Quarter.GetQuarterList(Application["DSN"].ToString()); ddlQuarter.DataBind(); ddlQuarter.SelectedIndex = 0; this.txtQuarter.Text = ddlQuarter.SelectedValue; } private void initTable() { tableName = "xOEMStoreNominationForCAM" + DateTime.UtcNow.ToString(); dataTable = new DataTable(tableName); ViewState["TableName"] = tableName; ViewState["DataTable"] = dataTable; } private void gridBind() { dataTable = new DataTable(); SqlConnection conn = new SqlConnection(Application["DSN"].ToString()); SqlCommand cmd = new SqlCommand(); SqlDataAdapter da = new SqlDataAdapter(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "prc_xoem_stor_nominate_qtr_list"; cmd.Connection = conn; try { cmd.Parameters.AddWithValue("@wwid", ViewState["WWID"].ToString()); cmd.Parameters.AddWithValue("@yyyyqq",ddlQuarter.SelectedValue); if (txtRegion.Text.Trim() == "ALL" || txtRegion.Text.Trim() == "") { cmd.Parameters.AddWithValue("@rgn_cd", DBNull.Value); } else { cmd.Parameters.AddWithValue("@rgn_cd", txtRegion.Text.Trim()); } if (txtGrid.Text.Trim() == "ALL" || txtGrid.Text.Trim() == "") { cmd.Parameters.AddWithValue("@grid_cd", DBNull.Value); } else { cmd.Parameters.AddWithValue("@grid_cd", txtGrid.Text.Trim()); } if (txtCity.Text.Trim() == "ALL" || txtCity.Text.Trim() == "") { cmd.Parameters.AddWithValue("@city_id", DBNull.Value); } else { cmd.Parameters.AddWithValue("@city_id", txtCity.Text.Trim()); } if (txtStorID.Text.Trim() != "") { cmd.Parameters.AddWithValue("@stor_id", txtStorID.Text.Trim()); } else { cmd.Parameters.AddWithValue("@stor_id", DBNull.Value); } if (txtStorNm.Text.Trim() != "") { cmd.Parameters.AddWithValue("@stor_nm", txtStorNm.Text.Trim()); } else { cmd.Parameters.AddWithValue("@stor_nm", DBNull.Value); } da.SelectCommand = cmd; conn.Open(); da.Fill(dataTable); this.lblCount.Text = "Total Number:" + dataTable.Rows.Count+ " "; ViewState["DataTable"] = dataTable; } catch { } finally { conn.Close(); da.Dispose(); cmd.Dispose(); } } /// <summary> /// 提名标记 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnMark_Click(object sender, EventArgs e) { int mkCount = 0; //Dictionary<int, int> list = new Dictionary<int, int>(); for (int i = 0; i < GridMain.Items.Count; i++) { CheckBox CB =(CheckBox)GridMain.Items[i].FindControl("chkSelected"); if (CB.Checked==true) { // list.Add(int.Parse(GridMain.Items[i].Cells[4].Text), int.Parse(GridMain.Items[i].Cells[18].Text)); SqlParameter[] paraList = new SqlParameter[] { new SqlParameter("@yyyyqq",ddlQuarter.SelectedValue), new SqlParameter("@wwid",ViewState["WWID"].ToString()), new SqlParameter("@stor_id",GridMain.Items[i].Cells[4].Text.Trim()), new SqlParameter("@co_id",GridMain.Items[i].Cells[18].Text.Trim()) }; SqlHelper.ExecuteNonQuery(Application["DSN"].ToString(), "prc_xoem_stor_nominate_qtr_mark", paraList); mkCount++; } } if (mkCount ==0) { string msg = "Please select at lease one item to Mark !"; PageAlert(msg); } else { string msg = "Successful Mark store is : " + mkCount + " !"; PageAlert(msg); } //重新加载数据 gridBind(); bindNextPage(0); } /// <summary> /// 取消标记 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnUnmark_Click(object sender, EventArgs e) { int unMkCount = 0; for (int i = 0; i < GridMain.Items.Count; i++) { CheckBox CB = (CheckBox)GridMain.Items[i].FindControl("chkSelected"); if (CB.Checked==true) { SqlParameter[] paraList = new SqlParameter[] { new SqlParameter("@yyyyqq",ddlQuarter.SelectedValue), new SqlParameter("@wwid",ViewState["WWID"].ToString()), new SqlParameter("@stor_id",GridMain.Items[i].Cells[4].Text.Trim()), new SqlParameter("@co_id",GridMain.Items[i].Cells[18].Text.Trim()) }; SqlHelper.ExecuteNonQuery(Application["DSN"].ToString(), "prc_xoem_stor_nominate_qtr_unmark", paraList); unMkCount++; } } if (unMkCount==0) { string msg = "Please select at lease one item to Unmark !"; PageAlert(msg); } else { string msg = "Successful unmark store is : " + unMkCount + " !"; PageAlert(msg); } //重新加载数据 gridBind(); bindNextPage(0); } protected void btnSearch_Click(object sender, EventArgs e) { gridBind(); bindNextPage(0); if (ViewState["actionFlag"].ToString() == "1") { this.btnMark.Enabled = true; this.btnUnmark.Enabled = true; } else { this.btnMark.Enabled = false; this.btnUnmark.Enabled = false; } } protected void GridMain_PageIndexChanged(object source, DataGridPageChangedEventArgs e) { //GridMain.CurrentPageIndex = e.NewPageIndex; bindNextPage(e.NewPageIndex); } /// <summary> /// 标记Style设置 /// </summary> private void NotedStyle() { for (int i = 0; i < GridMain.Items.Count; i++) { if (GridMain.Items[i].Cells[13].Text.Trim() == "覆盖") { GridMain.Items[i].Cells[13].ForeColor = System.Drawing.Color.Red; } if (GridMain.Items[i].Cells[24].Text.Trim() == "覆盖") { GridMain.Items[i].Cells[24].ForeColor = System.Drawing.Color.Red; } } } /// <summary> /// 提示代码 /// </summary> /// <param name="strMsg"></param> private void PageAlert(string strMsg) { this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Alter", "<script language=javascript>alert('" + strMsg + "');</script>"); } protected void ddlQuarter_SelectedIndexChanged(object sender, EventArgs e) { this.txtQuarter.Text = ddlQuarter.SelectedValue; } protected void GridMain_SelectedIndexChanged(object sender, EventArgs e) { } } }
相关文章推荐
- C#中的BackgroundWorker控件
- C#中的线程(四)高级话题
- C#中的线程(三)多线程
- C#中的线程(一)入门
- C#中的线程(二)线程同步
- C#中哈希表(HashTable)用法实例详解(添加/移除/判断/遍历/排序等)
- C#使用foreach循环遍历数组完整实例
- c#传入域名返回对应的IP
- c#获取WebBrowser中的userAgent
- c#geckofx文件流下载
- C#遍历文件夹及其子目录的完整实现方法
- C# 验证数字
- C#简单遍历指定文件夹中所有文件的方法
- c#读取Excel的第一个Sheet表
- C#如何读取Excel文件
- C#基础一
- C#中一些常用的加密和哈希处理
- C#个人总结2——类和对象
- C#使用tesseract3.02识别验证码模拟登录
- c#中Invoke使用方法(线程间操作无效: 从不是创建控件的线程访问它。那么创建它的线程是哪个线程?)