Steven Xiao's blog

远行者需要智慧,更需要耐心! 工欲善其事,必先利其器。 授之鱼,不如授之渔。

博客园 首页 新随笔 联系 订阅 管理
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

  



 


posted on 2007-12-28 19:32  Steven Xiao  阅读(1078)  评论(0编辑  收藏  举报