DataGrid的应用以及常用方法(Vb.Net版)

    技术2022-05-11  85

    <HTML>     <HEAD>         <title>Info_Manage</title>         <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">         <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">         <meta content="JavaScript" name="vs_defaultClientScript">         <meta content=" http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">         <LINK href="StyleSheet1.css" type="text/css" rel="stylesheet">         <script language="javascript">         var CurrentColor;         Function CheckAll()function CheckAll(sel){         var num;         num=document.getElementsByName("CheckBox2").length;          for(var i=0;i<num;i++)            {document.getElementsByName("CheckBox2")[i].checked =sel;             }         }         Function ItemOver()function ItemOver(Obj)         {          CurrentColor=Obj.style.backgroundColor;          Obj.style.backgroundColor="#B9D1F3";         }         Function ItemOut()function ItemOut(Obj)         {          Obj.style.backgroundColor=CurrentColor;         }                  Function Check_Set()function Check_Set(msg0,msg1)         {         var num=document.getElementsByName("CheckBox2").length;          for(var i=0;i<num;i++)            {            if(document.getElementsByName("CheckBox2")[i].checked)            {            if(msg0!=null||msg0!="")            {            return confirm(msg0);}            else{            return true;                        }                       }             }             alert(msg1);             return false;           }                      Function Select_Item()function Select_Item(sel)         {             if(sel == false)             {                 document.getElementsByName("CkAllName")[0].checked = false;             }             else             {                 var len = document.getElementsByName("CheckBox2").length;                 var num = 0;                 for(var i=0;i<len;i++)                 {                     if(document.getElementsByName("CheckBox2")[i].checked)                     {                         num = num + 1;                     }                 }                 if(num >= len)                 {                     document.getElementsByName("CkAllName")[0].checked = true;                 }             }         }         </script>     </HEAD>     <body MS_POSITIONING="GridLayout">         <form id="Form1" method="post" runat="server">             <FONT face="宋体">                 <TABLE id="Table1" style="Z-INDEX: 101; LEFT: 112px; WIDTH: 686px; POSITION: absolute; TOP: 40px; HEIGHT: 288px"                     cellSpacing="1" cellPadding="1" align="center" border="0">                     <TR>                         <TD vAlign="bottom" align="center"><asp:label id="Label1" runat="server" Width="64px">搜索类别</asp:label><asp:dropdownlist id="DropDownList1" runat="server" Width="120px"></asp:dropdownlist> <asp:label id="Label2" runat="server" Width="80px">标题关键字</asp:label><asp:textbox id="TextBox1" runat="server"></asp:textbox><asp:button id="Button1" runat="server" Text="搜索"></asp:button>                               <asp:button id="Button8" runat="server" Text="重置"></asp:button>                                  <asp:button id="Button7" runat="server" Width="72px" Text="删除所选"></asp:button></TD>                     </TR>                     <TR>                         <TD align="center"><asp:datagrid id="DataGrid1" runat="server" Width="680px" DataKeyField="Id" HorizontalAlign="Center"                                 AllowPaging="True" PageSize="15" AutoGenerateColumns="False">                                 <SelectedItemStyle HorizontalAlign="Center"></SelectedItemStyle>                                 <AlternatingItemStyle HorizontalAlign="Center"></AlternatingItemStyle>                                 <ItemStyle HorizontalAlign="Center"></ItemStyle>                                 <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>                                 <Columns>                                     <asp:TemplateColumn HeaderText="序号">                                         <HeaderStyle HorizontalAlign="Center" Width="60px"></HeaderStyle>                                         <HeaderTemplate>                                             <INPUT οnclick="CheckAll(this.checked)" type="checkbox" id="Ckall" name="CkAllName">全选                                         </HeaderTemplate>                                         <ItemTemplate>                                             <INPUT id="CheckBoxitem" οnclick=Select_Item(this.checked) type="checkbox"  value='<%#DataBinder.Eval(Container.DataItem,"Id")%>' name="CheckBox2">                                         </ItemTemplate>                                     </asp:TemplateColumn>                                     <asp:BoundColumn DataField="Inf_Catalog_Name" HeaderText="类别">                                         <HeaderStyle HorizontalAlign="Center" Width="100px"></HeaderStyle>                                     </asp:BoundColumn>                                     <asp:HyperLinkColumn Target="_self" DataTextField="Inf_Title" HeaderText="标题">                                         <HeaderStyle HorizontalAlign="Center" Width="200px"></HeaderStyle>                                     </asp:HyperLinkColumn>                                     <asp:BoundColumn DataField="Inf_Author" HeaderText="作者">                                         <HeaderStyle HorizontalAlign="Center" Width="60px"></HeaderStyle>                                         <ItemStyle HorizontalAlign="Center"></ItemStyle>                                         <FooterStyle HorizontalAlign="Center"></FooterStyle>                                     </asp:BoundColumn>                                     <asp:BoundColumn DataField="Inf_Dt" HeaderText="日期" DataFormatString="{0:d}">                                         <HeaderStyle HorizontalAlign="Center" Width="80px"></HeaderStyle>                                     </asp:BoundColumn>                                     <asp:BoundColumn DataField="Inf_Uid" HeaderText="录入人">                                         <HeaderStyle HorizontalAlign="Center" Width="60px"></HeaderStyle>                                     </asp:BoundColumn>                                 </Columns>                                 <PagerStyle Visible="False"></PagerStyle>                             </asp:datagrid></TD>                     </TR>                     <TR>                         <TD vAlign="top" align="center"><asp:button id="Button2" runat="server" Text="首页"></asp:button> <asp:button id="Button3" runat="server" Text="上页"></asp:button><asp:label id="Msg" runat="server" Width="248px" CssClass="Center">Label</asp:label><asp:button id="Button4" runat="server" Text="下页"></asp:button> <asp:button id="Button5" runat="server" Text="末页"></asp:button>                                     <asp:textbox id="Num" runat="server" Width="40px"></asp:textbox><asp:button id="Button6" runat="server" Text="跳转"></asp:button></TD>                     </TR>                 </TABLE>             </FONT>         </form>     </body> </HTML>  Public Class Info_ManageClass Info_Manage     Inherits System.Web.UI.Page     Dim SqlStr As String = "select Information.*,Catalog.Catalog_Name As Inf_Catalog_Name  from Information Inner Join [Catalog] On Information.Inf_Catalog_Id=[Catalog].Id " '''用于记录Sql语句     Dim AddStr As String = "" '''用于记录Sql条件语句     Private Const ord As String = " order by Information.Id"     Protected WithEvents Msg As System.Web.UI.WebControls.Label     Protected WithEvents Num As System.Web.UI.WebControls.TextBox     Protected WithEvents Button8 As System.Web.UI.WebControls.Button     Protected WithEvents Label3 As System.Web.UI.WebControls.Label     Dim Db As New DataAccess Web 窗体设计器生成的代码#Region " Web 窗体设计器生成的代码 "     '该调用是 Web 窗体设计器所必需的。     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()     End Sub     Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid     Protected WithEvents Label1 As System.Web.UI.WebControls.Label     Protected WithEvents Label2 As System.Web.UI.WebControls.Label     Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox     Protected WithEvents Button1 As System.Web.UI.WebControls.Button     Protected WithEvents Button2 As System.Web.UI.WebControls.Button     Protected WithEvents Button3 As System.Web.UI.WebControls.Button     Protected WithEvents Button4 As System.Web.UI.WebControls.Button     Protected WithEvents Button5 As System.Web.UI.WebControls.Button     Protected WithEvents Button6 As System.Web.UI.WebControls.Button     Protected WithEvents Button7 As System.Web.UI.WebControls.Button     Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList     '注意: 以下占位符声明是 Web 窗体设计器所必需的。     '不要删除或移动它。     Private designerPlaceholderDeclaration As System.Object     Private Sub Page_Init()Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init         'CODEGEN: 此方法调用是 Web 窗体设计器所必需的         '不要使用代码编辑器修改它。         InitializeComponent()     End Sub #End Region     '''加载分类信息     Sub AddCatalog()Sub AddCatalog()         Dim Dt As New DataTable         DropDownList1.Items.Clear()         Dt = Db.RunAdpSelect("Select * from Catalog order by Id")         DropDownList1.Items.Insert(0, New ListItem("请选择信息类别", -1))         For I As Int16 = 0 To Dt.Rows.Count - 1             DropDownList1.Items.Add(New ListItem(Dt.Rows(I)("Catalog_Name"), Dt.Rows(I)("Id")))         Next         DropDownList1.DataBind()     End Sub     '''计算各种数据     Sub CountInfo()Sub CountInfo()         ViewState("PageSize") = 15         ViewState("CurrentPageIndex") = 0         ViewState("Count") = Db.RunExeScale("Select Count(*) From Information" & AddStr)         If ViewState("Count") Mod ViewState("PageSize") = 0 Then             ViewState("PageCount") = ViewState("Count") / ViewState("PageSize")         Else             ViewState("PageCount") = Fix(ViewState("Count") / ViewState("PageSize") + 1)         End If     End Sub     '''定制信息     Sub BindData()Sub BindData()         Dim Dt As New DataTable         Dt = Db.RunPageSelect(SqlStr & AddStr & ord, ViewState("PageSize") * ViewState("CurrentPageIndex"), ViewState("PageSize"), "Information")         DataGrid1.DataSource = Dt         DataGrid1.DataBind()         Msg.Text = "<font Color=Red>信息:" & ViewState("Count") & "条---页:" & ViewState("CurrentPageIndex") + 1 & "/" & ViewState("PageCount") & "</font>"         If ViewState("PageCount") = 1 Then             Button2.Enabled = False             Button3.Enabled = False             Button4.Enabled = False             Button5.Enabled = False             Button6.Enabled = False             Exit Sub         End If         If ViewState("CurrentPageIndex") = 0 Then             Button2.Enabled = False             Button3.Enabled = False             Button4.Enabled = True             Button5.Enabled = True         ElseIf ViewState("CurrentPageIndex") = ViewState("PageCount") - 1 Then             Button4.Enabled = False             Button5.Enabled = False             Button2.Enabled = True             Button3.Enabled = True         End If     End Sub     Private Sub Page_Load()Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load         '在此处放置初始化页的用户代码         If Not Page.IsPostBack Then             '    If Session("Uid") = "" orElse Session("Uid") Is Nothing Then             '        Response.Write("<script language='javascript'>parent.parent.location.href='../WebForm1.aspx';</script>")             '    Else             Button7.Attributes.Add("onclick", "return Check_Set('确定删除选择项吗?将无法恢复!','请选择要删除的项!');")             AddCatalog()             CountInfo()             BindData()             '    End If         End If     End Sub     '''首页     Private Sub Button2_Click()Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click         ViewState("CurrentPageIndex") = 0         BindData()     End Sub     '''末页     Private Sub Button5_Click()Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click         ViewState("CurrentPageIndex") = ViewState("PageCount") - 1         BindData()     End Sub     '''上页     Private Sub Button3_Click()Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click         If ViewState("CurrentPageIndex") > 0 Then             ViewState("CurrentPageIndex") = ViewState("CurrentPageIndex") - 1         End If         BindData()     End Sub     '''下页     Private Sub Button4_Click()Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click         If ViewState("CurrentPageIndex") < ViewState("PageCount") - 1 Then             ViewState("CurrentPageIndex") = ViewState("CurrentPageIndex") + 1         End If         BindData()     End Sub     '''页面跳转     Private Sub Button6_Click()Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click         If IsNumeric(Num.Text.Trim) = False orElse (CInt(Num.Text.Trim) > ViewState("PageCount")) orElse (CInt(Num.Text.Trim) < 1) Then             Response.Write("<script language='javascript'>alert('所输入内容必须为总页数内的整数数字!');</script>")         Else             ViewState("CurrentPageIndex") = CInt(Num.Text.Trim) - 1             BindData()         End If         Num.Text = ""     End Sub     '''搜索     Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click         AddStr = " Where Inf_Catalog_Id= " & DropDownList1.SelectedValue & " And Inf_Title like '%" & TextBox1.Text.Trim & "%' "         CountInfo()         BindData()     End Sub     '''重置     Private Sub Button8_Click()Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click         AddStr = ""         CountInfo()         BindData()     End Sub     '''删除选择的项     Private Sub Button7_Click()Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click         Dim Ids As String = Request.Form("CheckBox2")         Db.RunInsertUpdateDelete("Delete From Information Where Id in (" & Ids & ")")         Response.Write("<script language='javascript'>alert('删除完毕!');</script>")         CountInfo()         BindData()     End Sub     '''添加鼠标属性     Private Sub DataGrid1_ItemCreated()Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated         If e.Item.ItemIndex <> -1 Then             e.Item.Attributes.Add("onmouseover", "ItemOver(this)")             e.Item.Attributes.Add("onmouseout", "ItemOut(this)")         End If     End Sub End Class  

    最新回复(0)