交流,学习,进步!

千里之行始于足下……
  博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

关于VS2005中GridView的自定义分页,单选、多选、排序、自增列的简单应用。
    多选时,只有全部选中时“多选”才选中。

图片效果:

代码如下:

.aspx

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>无标题页</title>
    
<script language="javascript" type="text/javascript">    
    
// 判断多选是否与选中项(没有选中的返回false)
    function slcNo_click()
    {
        
if (document.form1.checkboxname.length)
        {
            
for (var i=0;i<document.form1.checkboxname.length;i++)
            {
                
if(document.form1.checkboxname[i].checked)
                {
                    
return true;
                }
            }
        }
        
else
        {
            
if(document.form1.checkboxname.checked)
            {
                
return true;
            }
        }
        alert(
"请选择后再操作!");
        
return false;
    }
    
    
// 鼠标经过改变行的颜色
    if (!objbeforeItem)
    {
        
var objbeforeItem=null;
        
var objbeforeItembackgroundColor=null;
    }    
    
function ItemOver(obj)
    {
        
if(objbeforeItem)
        {
            objbeforeItem.style.backgroundColor 
= objbeforeItembackgroundColor;
        }
        objbeforeItembackgroundColor 
= obj.style.backgroundColor;
        objbeforeItem 
= obj;
        obj.style.backgroundColor 
= "#B9D1F3";     
    }
    
//        
 
    
// 多选的全选与取消
    function checkJs(boolvalue)
    {
        
if(document.all.checkboxname.length>1)
        {
            
for(var i=0;i<document.all.checkboxname.length;i++)
            {
                document.all.checkboxname[i].checked 
= boolvalue;            
            }
        }
        
else
            document.all.checkboxname.checked 
= boolvalue;
    }
    
//
    
    
// 只有全部选中时“全选”选中
    function SingleCheckJs()
    {
        
var flag1=false;
        
var flag2=false;
        
        
if (document.form1.checkboxname.length)
        {
            
for (var i=0;i<document.form1.checkboxname.length;i++)
            {
                
if(document.form1.checkboxname[i].checked)
                    flag1 
= true;
                
else
                    flag2 
= true;
            }
        }
        
else
        {
            
if(document.form1.checkboxname.checked)
                flag1 
= true;
            
else
                flag2 
= true;
        }
        
        
if(flag1==true&&flag2==false)
            document.getElementById(
"chk").checked = true;
        
else
            document.getElementById(
"chk").checked = false;
    }
    
//
    </script>

</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
<table cellpadding="0" cellspacing="0" border="0" width="80%" style="font-size: 11px">
                
<tr>
                    
<td align="center">
                        
<asp:GridView ID="GridView1" runat="server" Width="100%" CellPadding="4" ForeColor="#333333"
                            AutoGenerateColumns
="False" AllowPaging="True" PageSize="12" BorderColor="Silver"
                            BorderStyle
="Solid" BorderWidth="1px" OnRowDataBound="GridView1_RowDataBound"
                            ShowFooter
="True" EmptyDataText="没有数据记录!!" AllowSorting="True" OnSorting="GridView1_Sorting">
                            
<Columns>
                                
<asp:BoundField HeaderText="编号" DataField="id" Visible="False" />
                                
<asp:TemplateField HeaderText="&lt;input type='checkbox' id='chk' name='chk' onclick='checkJs(this.checked);'  /&gt;全选" FooterText="全选">
                                    
<ItemTemplate>
                                        
<input type="checkbox" id="checkboxname" name="checkboxname" value='<%# DataBinder.Eval(Container.DataItem, "id")%>' onclick='SingleCheckJs();' />
                                    
</ItemTemplate>
                                
</asp:TemplateField>
                                
<asp:TemplateField HeaderText="单选" FooterText="单选">
                                    
<ItemTemplate>
                                        
<input type="radio" id="RadioName" name="RadioName" value='<%# Eval("id")%>' />
                                    
</ItemTemplate>
                                
</asp:TemplateField>
                                
<asp:TemplateField HeaderText="自增列" FooterText="自增列">
                                    
<ItemTemplate>
                                        
<%# (Container.DataItemIndex+1).ToString()%>
                                    
</ItemTemplate>
                                
</asp:TemplateField>
                                
<asp:BoundField HeaderText="姓名" FooterText="姓名" DataField="name" SortExpression="name" />
                                
<asp:BoundField HeaderText="身份证号" FooterText="身份证号" DataField="card" SortExpression="card" />
                                
<asp:BoundField HeaderText="价格" FooterText="价格" DataField="price" DataFormatString="{0:¥#,##0.00}"
                                    HtmlEncode
="False" SortExpression="price" />
                                
<asp:BoundField HeaderText="数字" FooterText="数字" DataField="price" DataFormatString="{0:0.00}"
                                    HtmlEncode
="False" SortExpression="price" />
                                
<asp:BoundField HeaderText="建立时间" FooterText="建立时间" DataField="createdate" DataFormatString="{0:yyyy年MM月dd日 hh时mm分ss秒}"
                                    HtmlEncode
="False" SortExpression="createdate" />
                            
</Columns>
                            
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                            
<EditRowStyle BackColor="#999999" />
                            
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                            
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                            
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                            
<PagerSettings Visible="False" />
                            
<FooterStyle Font-Bold="True" />
                            
<HeaderStyle Font-Bold="False" Font-Italic="False" />
                        
</asp:GridView>
                    
</td>
                
</tr>
                
<tr>
                    
<td align="center" style="height: 25px">
                        
<asp:LinkButton ID="btnFirst" CommandArgument="first" OnClick="PagerButtonClick"
                            runat
="server">首 页</asp:LinkButton>
                        
<asp:LinkButton ID="btnPrev" CommandArgument="prev" OnClick="PagerButtonClick" runat="server">上一页</asp:LinkButton>
                        
<asp:LinkButton ID="btnNext" CommandArgument="next" OnClick="PagerButtonClick" runat="server">下一页</asp:LinkButton>
                        
<asp:LinkButton ID="btnLast" CommandArgument="last" OnClick="PagerButtonClick" runat="server">尾 页</asp:LinkButton>
                        
<asp:Label ID="LblCurrentIndex" runat="server"></asp:Label>
                        
<asp:Label ID="LblPageCount" runat="server"></asp:Label>
                        
<asp:Label ID="LblRecordCount" runat="server"></asp:Label></td>
                
</tr>
                
<tr>
                    
<td>
                        
<asp:Button ID="Button2" runat="server" Text="checkbox得到选择的行" OnClick="Button2_Click">
                        
</asp:Button>
                        
<asp:Button ID="Button1" runat="server" Text="radio得到选择的行" OnClick="Button1_Click"></asp:Button>
                    
</td>
                
</tr>
            
</table>

    
</div>
    
</form>
</body>
</html>

 

.cs

Code

生成数据库:

 

CREATE TABLE [dbo].[test01] (
    
[id] [decimal](180IDENTITY (11NOT NULL ,
    
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    
[card] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    
[price] [decimal](18,2NULL,
    
[createdate] [datetime] NULL 
ON [PRIMARY]
GO

ALTER TABLE [dbo].[test01] ADD 
    
CONSTRAINT [DF_test01_createdate] DEFAULT (getdate()) FOR [createdate],
    
CONSTRAINT [PK_test01] PRIMARY KEY  CLUSTERED 
    (
        
[id]
    )  
ON [PRIMARY] 
GO