DataGrid例子

博客涉及ASP WebForm开发,包含datagrid、textbox等元素,运用了JavaScript技术,还提及assembly相关内容,聚焦信息技术领域的Web开发方面。
webform1.aspx 
以下内容为程序代码:

程序代码:

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="TayeSample.WebForm1" %> 
<%@ Register TagPrefix="cc1" Namespace="WebControlLibrary1" Assembly="WebControlLibrary1" %> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 
<HTML> 
   <HEAD> 
      <title>WebForm1</title> 
      <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"> 
      <meta content="C#" name="CODE_LANGUAGE"> 
      <meta content="JavaScript" name="vs_defaultClientScript"> 
      <meta content=" http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"> 
   </HEAD> 
   <body> 
      <form id="Form1" method="post" runat="server"> 
         <asp:datagrid id="DataGrid1" runat="server" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" 
            BackColor="White" CellPadding="3" GridLines="Horizontal" AutoGenerateColumns="False" DataKeyField="job_id" 
            PageSize="5" AllowPaging="True"> 
            <SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle> 
            <AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle> 
            <ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle> 
            <HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle> 
            <FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle> 
            <Columns> 
               <asp:TemplateColumn HeaderText="序号"> 
                  <ItemTemplate> 
                     <%# (DataGrid1.PageSize * DataGrid1.CurrentPageIndex) + Container.ItemIndex +1 %> 
                  </ItemTemplate> 
               </asp:TemplateColumn> 
               <asp:BoundColumn DataField="job_id" ReadOnly="True" HeaderText="job_id"></asp:BoundColumn> 
               <asp:BoundColumn DataField="job_desc" HeaderText="job_desc"></asp:BoundColumn> 
               <asp:BoundColumn DataField="max_lvl" HeaderText="max_lvl"></asp:BoundColumn> 
               <asp:BoundColumn DataField="min_lvl" HeaderText="min_lvl"></asp:BoundColumn> 
               <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" HeaderText="编辑" CancelText="取消" EditText="编辑"></asp:EditCommandColumn> 
               <asp:ButtonColumn Text="删除" HeaderText="删除" CommandName="Delete"></asp:ButtonColumn> 
               <asp:HyperLinkColumn Text="详细资料" HeaderText="详细资料"></asp:HyperLinkColumn> 
            </Columns> 
            <PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle> 
         </asp:datagrid></form> 
   </body> 
</HTML> 



后代码webform1.aspx.cs 



程序代码:

using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Web; 
using System.Web.SessionState; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 
using System.Data.SqlClient; 

namespace TayeSample 

   /// <summary> 
   /// WebForm1 的摘要说明。 
   /// </summary> 
   public class WebForm1 : System.Web.UI.Page 
   { 
      protected System.Web.UI.WebControls.DataGrid DataGrid1; 
    
    
      private SqlConnection Cn=new SqlConnection("server=(local);database=pubs;uid=sa;pwd="); 

      private void Page_Load(object sender, System.EventArgs e) 
      { 
         // 在此处放置用户代码以初始化页面 
         if(!IsPostBack) 
         { 
            Bind(); 
         } 
          
      } 

      #region Web 窗体设计器生成的代码 
      override protected void OnInit(EventArgs e) 
      { 
         // 
         // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 
         // 
         InitializeComponent(); 
         base.OnInit(e); 
      } 
       
      /// <summary> 
      /// 设计器支持所需的方法 - 不要使用代码编辑器修改 
      /// 此方法的内容。 
      /// </summary> 
      private void InitializeComponent() 
      {     
         this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged); 
         this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand); 
         this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand); 
         this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand); 
         this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand); 
         this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound); 
         this.Load += new System.EventHandler(this.Page_Load); 

      } 
      #endregion 

      // 
      //数据绑定 
      // 
      private void Bind() 
      { 
         string selStr = "SELECT * FROM jobs"; 
         SqlDataAdapter sqlDa = new SqlDataAdapter(selStr,Cn); 

         DataSet ds = new DataSet(); 

         sqlDa.Fill(ds); 

         this.DataGrid1.DataSource = ds; 
         this.DataGrid1.DataBind(); 

      } 


       
      // 
      //添加confirm及onmouseout onmouseover事件 
      // 
      private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e) 
      { 
         if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
         { 
             
            LinkButton delBttn = (LinkButton) e.Item.Cells[6].Controls[0]; 
            delBttn.Attributes.Add("onclick","javascript:return confirm('确定删除" + e.Item.Cells[0].Text + "');"); 



            e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='#f3f3f3'"); 
            if(e.Item.ItemType == ListItemType.Item) 
            { 
               e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#E7E7FF'"); 
            } 

            if(e.Item.ItemType ==ListItemType.AlternatingItem) 
            { 
               e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#F7F7F7'"); 
            } 
         } 
      } 

      // 
      // 编辑 
      // 
      private void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e) 
      { 
         this.DataGrid1.EditItemIndex = e.Item.ItemIndex; 
         this.Bind(); 
      } 

      // 
      // 取消 
      // 
      private void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e) 
      { 
         this.DataGrid1.EditItemIndex = -1; 
         this.Bind(); 
      } 

      // 
      // 删除 
      // 
      private void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e) 
      { 
         string delStr="DELETE FROM jobs WHERE job_id=@job_id"; 

         SqlCommand sqlCmd=new SqlCommand(delStr,Cn); 

         sqlCmd.Parameters.Add("@job_id",SqlDbType.SmallInt,2); 

         sqlCmd.Parameters["@job_id"].Value = this.DataGrid1.DataKeys[e.Item.ItemIndex]; 

         try 
         { 
            Cn.Open(); 
            sqlCmd.ExecuteNonQuery(); 
            Cn.Close(); 

            if(!Page.IsStartupScriptRegistered("delAlert")) 
            { 
               Page.RegisterStartupScript("delAlert",@"<script language='javascript'>alert('删除成功');</script>"); 
            } 

            Bind(); 
         } 
         catch(Exception err) 
         { 
            Response.Write(err.Message); 
         } 
         finally 
         { 
            if(Cn.State == ConnectionState.Open) 
            { 
               Cn.Close(); 
            } 
         } 
          

      } 

      // 
      // 分页 
      // 
      private void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e) 
      { 
         this.DataGrid1.CurrentPageIndex = e.NewPageIndex ; 
         Bind(); 
      } 


      // 
      // 更新 
      // 
      private void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e) 
      { 
         string upStr="UPDATE jobs SET job_desc=@job_desc,max_lvl=@max_lvl,min_lvl=@min_lvl WHERE job_id=@job_id"; 

         SqlCommand upCmd=new SqlCommand(upStr,Cn); 

         upCmd.Parameters.Add("@job_desc",SqlDbType.VarChar,50); 
         upCmd.Parameters.Add("@max_lvl",SqlDbType.TinyInt,1); 
         upCmd.Parameters.Add("@min_lvl",SqlDbType.TinyInt,1); 
         upCmd.Parameters.Add("@job_id",SqlDbType.SmallInt,2); 


         upCmd.Parameters[0].Value = ((TextBox) e.Item.Cells[2].Controls[0]).Text; 
         upCmd.Parameters[1].Value = ((TextBox) e.Item.Cells[3].Controls[0]).Text; 
         upCmd.Parameters[2].Value = ((TextBox) e.Item.Cells[4].Controls[0]).Text; 

         upCmd.Parameters[3].Value = this.DataGrid1.DataKeys[e.Item.ItemIndex]; 

         try 
         { 
            Cn.Open(); 
            upCmd.ExecuteNonQuery(); 
            Cn.Close(); 
            if(!Page.IsStartupScriptRegistered("upAlert")) 
            { 
               Page.RegisterStartupScript("upAlert",@"<script language='javascript'>alert('更新成功')</script>"); 
            } 
            this.DataGrid1.EditItemIndex = -1; 
            Bind(); 
         } 
         catch(Exception err) 
         { 
            Response.Write(err.Message); 
         } 
         finally 
         { 
            if(Cn.State == ConnectionState.Open) 
            { 
               Cn.Close(); 
            } 
         } 


      } 
   } 










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值