惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
S
SegmentFault 最新的问题
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
DataBreaches.Net
F
Fortinet All Blogs
TaoSecurity Blog
TaoSecurity Blog
D
Docker
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
宝玉的分享
宝玉的分享
腾讯CDC
Google Online Security Blog
Google Online Security Blog
Recorded Future
Recorded Future
T
The Exploit Database - CXSecurity.com
T
The Blog of Author Tim Ferriss
V
V2EX
S
Securelist
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
CERT Recently Published Vulnerability Notes
A
Arctic Wolf
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
Y
Y Combinator Blog
P
Proofpoint News Feed
T
Tor Project blog
AWS News Blog
AWS News Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
博客园 - 聂微东
T
Threat Research - Cisco Blogs
B
Blog
Attack and Defense Labs
Attack and Defense Labs
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
N
News and Events Feed by Topic
博客园 - 司徒正美
H
Help Net Security
C
Cisco Blogs
C
Check Point Blog
S
Secure Thoughts

博客园 - Steven Xiao

asp.net接收API Post Json数据为空要注意的事项 解决Asp.net 程序在 IIS 5.1 上运行不支持转换Decimal类型小数点的问题 - Steven Xiao 把XML 文件转换为 String 字符串 - Steven Xiao C#语言规范 3.0 版 分享DotNetBar控件制作office 2007风格界面的视频教程(winform office 2007 风格) XMLHttpRequest对象 SQL 2005实现单表分页的查询语句 分享实现web用户控件调用.aspx页面里的方法(从而达到访问母页面中控件的目的) 软件开发者面试百问 分享一个DotNetBar做的三层架构的winForm程序 分享一个不错的js提示信息代码(tooltips) - Steven Xiao - 博客园 dropdownlist实现树型结构的栏目信息 asp.net 2.0实现多语言(二) asp.net 2.0实现多语言(一) 一个封装的在后台弹出JS Alert消息和JS confirm信息以及跳转到指定的页面 一个小小的WEB程序源代码 網絡收藏: 弹出窗口总结 - Steven Xiao 收藏的 sql经典语句 ---来自网上 ASP.NET button控件样式
gridview 实现全选和反选--补充
Steven Xiao · 2007-12-28 · via 博客园 - Steven Xiao

a.aspx 文件中的代码:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>gridview控件示例</title>
   
    <link href="App_Themes/Theme1/index.css" rel="stylesheet" type="text/css" />
   
 <script type="text/javascript" language="javascript">
      
    function DoCheck(flag)// 全选 flag=1 反选 flag=0
    {
        var inputs = document.forms[0].elements;
        for (var i=0; i < inputs.length; i++)
        if (inputs[i].type == 'checkbox')
        {
            if (flag)
                inputs[i].checked = true;
           else
                inputs[i].checked =!inputs[i].checked;
        }
    }
     
   
</script>
</head>
<body >    

    <form id="form1" runat="server">
<asp:GridView id="gvMyDeptNotFinished" runat="server" OnPageIndexChanging="gvMyDeptNotFinished_PageIndexChanging" AllowPaging="True" AutoGenerateColumns="False" SkinID="gvcaselist">
<Columns>
 <asp:TemplateField Visible="false" >              
<ControlStyle Width="20px"  />
 <HeaderTemplate>    
 <a onclick="DoCheck(0);" href="#">反选</a> <%--反选--%>
 <a onclick="DoCheck(1);" href="#">全选</a> <%--全选--%>                  
</HeaderTemplate>
<ItemTemplate>
<div style="text-align:center;width:50px;">
    <asp:CheckBox ID="CheckBox1" runat="server"/>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" Visible="false" >
<ControlStyle Width="0px" />
<ItemTemplate>
    <asp:Label ID="lblID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="姓名">
<ControlStyle Width="100px"></ControlStyle>
<ItemTemplate>
<div style="text-align:left; padding-left:5px;">
<a href='<%# "MyDeptNotFinishedDetails.aspx?emp_code="+Eval("emp_code")+"&titleid="+Request.QueryString["titleid"]+"&cur_status="+Request.QueryString["cur_status"]%>' target="_self" class="nav2"><%# Eval("pic_cn")%></a>

</div><%--这里参数从前一页传过来的,也有从数据绑定的,要注意不要写错了哦--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="数量">
<ControlStyle Width="150px"></ControlStyle>
<ItemTemplate>
  <div class="myclass1">
   <asp:Literal ID="litcasesum" runat="server" Text='<%# Eval("sumcase") %>'></asp:Literal>                              
   </div>               
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="点数">
<ControlStyle Width="150px"></ControlStyle>
<ItemTemplate>
  <div style="text-align:left; color:Red ;">
   <asp:Literal ID="litScore" runat="server" Text='<%# Eval("CaseWeight") %>'></asp:Literal>                             </div>               
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="当前操作人">
<ControlStyle Width="100px"></ControlStyle>
<ItemTemplate>
  <div style="text-align:left;">
   <%# Eval("operator") %>                              
   </div>               
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="当前状态">
<ControlStyle Width="250px"></ControlStyle>
<ItemTemplate>
<asp:Label ID="lblcasename" runat="server" Text='<%# Eval("cur_status_name") %>'  ></asp:Label>             
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<DIV class="gvbottom">&nbsp;
<asp:Button id="btnGoToBack" onclick="btnGoToBack_Click" runat="server" Text="返回" SkinID="loginbtn"></asp:Button> &nbsp;
<asp:Button id="btnToConfirm" runat="server" Text="确认" OnClientClick="javascript:return confirm('确认选择?')" SkinID="loginbtn" OnClick="btnToConfirm_Click" ></asp:Button>
</DIV>
</form>
</body>

</html>


===========

.aspx.cs文件代码


 // using System.Data.SqlClient;

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetMyDeptNotFinishedCase();
        }
    }


/// <summary>
    /// gridview數據綁定
    /// </summary>
    private void GetMyDeptNotFinishedCase()
    {
        DataSet ds = new DataSet();
        ds = bll.RunProcedure("CP_MyDeptNotFinished_Person");
        if (ds.Tables[0].Rows.Count > 0)
        {
            gvMyDeptNotFinished.DataSource = ds.Tables[0];
            gvMyDeptNotFinished.DataBind();
        }
        else
        {
         //Response.Write("取数据出错,或没有记录");
        }

    }


   /// <summary>
    /// 确认 button 按钮事件
    /// </summary>
    /// <returns></returns>
protected void btnToConfirm_Click(object sender, EventArgs e)
    {
 
        string allselect = GetSelected();
        if (string.IsNullOrEmpty(allselect))
        {
            //Response.Write("您还没有选择数据,请至少选择一项!");
            return;
        }

        string[] allempcode = allselect.Split(',');

        int rowsAffected = 0;       
        
        foreach (string emp_code in allempcode)
        {
           
            try
            {
                SqlParameter[] parameters ={
                                        new SqlParameter("@emp_code",SqlDbType.NVarChar)
                                        };
                parameters[0].Value = emp_code;
              

                bll.RunProcedure("CP_Help", parameters, out rowsAffected); //使用存储过程
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);

            }
        }

        if (rowsAffected > 0)
        {
               //Response.Write("操作成功!");
               
        }
        else
        {
            //Response.Write("操作失败!");
            Return;
        }

       GetMyDeptNotFinishedCase();//重新再绑定数据到gridview控件


   }


   /// <summary>
    /// 取所有已被選擇項的ID值,
    /// </summary>
    /// <returns></returns>

 private string GetSelected()
    {
        string emp_code = null;
        foreach (GridViewRow gvrow in gvMyDeptNotFinished.Rows)
        {
            CheckBox ch = (CheckBox)gvrow.Cells[0].FindControl("CheckBox1");
            if (ch.Checked)
            {
                Label lbl = (Label)gvrow.Cells[1].FindControl("lblID");
                emp_code += lbl.Text + ",";               
            }
        }

        if (!string.IsNullOrEmpty(emp_code))
        {
            emp_code = emp_code.Substring(0, emp_code.Length - 1);
        }
        return emp_code;
    }


 /// <summary>
    /// 返回按鈕事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnGoToBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("MyDeptNotFinished.aspx?titleid="+Request.QueryString["titleid"].ToString());
   
   
    }


另外,我们在数据绑定到gridview控件,有时需要把一项的单个值特别显示出来,比如使用不同的颜色
如下图所示:(申请了变更)

其实这样也很容易实现的,只要我们改下SQL查询语句就可以做到的:
SQL:
SELECT         ID, case_name, ctrl_proc, CONVERT(nvarchar(20), updatedate, 111)
                          AS updatedate,
CaseWeight, 
                          CASE is_apply WHEN '1' THEN '<font color=red>是</font>' ELSE '否' END AS is_apply
FROM             mytable