gridview 实现全选和反选--补充

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 οnclick="DoCheck(0);" href="#">反选</a> <%--反选--%>
 <a οnclick="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" οnclick="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

  



 


转载于:https://www.cnblogs.com/xiaoxijin/archive/2007/12/28/1018958.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值