zoukankan      html  css  js  c++  java
  • 利用HTTP处理句柄,定制DataGrid

    1、显示数据库中的图片;
    2、利用global.asax的Session_Start来记录登陆者信息;

    global.asax.cs 代码:

            protected void Session_Start(Object sender, EventArgs e)
            

                SqlConnection connection 
    =new SqlConnection("user id=sa;data source=localhost;persist security info=True;initial catalog=NetManager;password=xiayc");
            
    /*SQLCom,Str_IpAddr,Str_Brow,Str_Option*/
                
    string strBrow,strIpAddr,strOption;

                strIpAddr
    =Request.UserHostAddress ;
                
    if (Request.UserAgent.IndexOf("MSIE")> -1 )
                
    {
                    strBrow
    ="Internet Explore"+Request.Browser.Version ;
                  
                }

                
    else
                
    {
                    strBrow
    =Request.Browser.Browser + Request.Browser.Version;
                }


                
    if (Request.UserAgent.IndexOf("Windows NT 5.2")> -1)
                
    {
                    strOption 
    = "Windows 2003 ";
                 
                }

                
    else
                
    {
                    
    if (Request.UserAgent.IndexOf("Windows NT 5.1")> -1)
                    
    {
                        strOption 
    = "Windows XP ";

                    }

                    
    else 
                    
    {
                        
    if (Request.UserAgent.IndexOf("Windows NT 5.0")> -1)
                        
    {
                            strOption 
    ="Windows 2000  ";
                        }

                        
    else
                        
    {
                            strOption 
    = Request.Browser.Platform;
                        }



                    }

                }


                DateTime datetime
    =new DateTime() ;
                datetime 
    =DateTime.UtcNow;
                datetime 
    = DateTime.Now;

                
    string s =datetime.ToString ("d");
                datetime 
    = DateTime.Parse(s);

                

                

            }

    webform1.aspx.cs代码

    25-38行为添加代码

     1using System;
     2using System.Collections;
     3using System.ComponentModel;
     4using System.Data;
     5using System.Drawing;
     6using System.Web;
     7using System.Web.SessionState;
     8using System.Web.UI;
     9using System.Web.UI.WebControls;
    10using System.Web.UI.HtmlControls;
    11using System.Data.SqlClient;
    12
    13namespace WebApplication1
    14{
    15    /// <summary>
    16    /// WebForm1 的摘要说明。
    17    /// </summary>

    18    public class WebForm1 : System.Web.UI.Page
    19    {
    20        protected System.Web.UI.WebControls.DataGrid DataGrid1;
    21    
    22        private void Page_Load(object sender, System.EventArgs e)
    23        {
    24            // 在此处放置用户代码以初始化页面
    25            if (!IsPostBack){
    26                SqlConnection connection = new SqlConnection("user id=sa;data source=localhost;persist security info=True;initial catalog=Northwind;password=xiayc");
    27                try 
    28                {
    29                    connection.Open();
    30                    SqlCommand command = new  SqlCommand("select EmployeeID,LastName,FirstName,Title FROM Employees",connection);
    31                    SqlDataReader reader = command.ExecuteReader();
    32                    DataGrid1.DataSource =reader;
    33                    DataGrid1.DataBind ();
    34                }

    35                finally{
    36                    connection.Close ();
    37                }

    38            }

    39
    40        }

    41
    42        #region Web 窗体设计器生成的代码
    43        override protected void OnInit(EventArgs e)
    44        {
    45            //
    46            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    47            //
    48            InitializeComponent();
    49            base.OnInit(e);
    50        }

    51        
    52        /// <summary>
    53        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    54        /// 此方法的内容。
    55        /// </summary>

    56        private void InitializeComponent()
    57        {    
    58            this.Load += new System.EventHandler(this.Page_Load);
    59
    60        }

    61        #endregion

    62
    63        private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
    64        {
    65        
    66        }

    67    }

    68}

    69

    webform1.aspx html代码:

     1<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
     2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
     3<HTML>
     4  <HEAD>
     5        <title>WebForm1</title>
     6<meta content="Microsoft Visual Studio .NET 7.1" name=GENERATOR>
     7<meta content=C# name=CODE_LANGUAGE>
     8<meta content=JavaScript name=vs_defaultClientScript>
     9<meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetSchema>
    10  </HEAD>
    11<body MS_POSITIONING="GridLayout">
    12<form id=Form1 method=post runat="server"><FONT face=宋体><asp:datagrid id=DataGrid1 style="Z-INDEX: 101; LEFT: 56px; POSITION: absolute; TOP: 48px" runat="server" Width="328px" Height="192px" AutoGenerateColumns="False">
    13                    <Columns>
    14                        <asp:TemplateColumn HeaderText="照片">
    15                            <ItemTemplate>
    16                                <img src='<%# "ImageGrabber.ashx?id=" + DataBinder.Eval(Container.DataItem,"EmployeeID") %>' border="0">
    17                            </ItemTemplate>
    18                        </asp:TemplateColumn>
    19                        <asp:BoundColumn DataField="LastName" HeaderText="Last Name"></asp:BoundColumn>
    20                        <asp:BoundColumn DataField="FirstName" HeaderText="First Name"></asp:BoundColumn>
    21                    </Columns>
    22                </asp:datagrid></FONT></form>
    23    </body>
    24</HTML>
    25

    注意 14行-17行 为DataGrid的模板行,该模板行输出一个典型的<img>标签
         <img src='ImageGrabber.ashx?id=1'>

    ImageGrabber.ashx是一个HTTP处理句柄。当被调用时,它ProcessRequest方法从查询字符串中截获雇员ID,
    并使用它执行自己的数据库查询。这次查询获取相应记录的Photo域,利用它创建一个位图,并把位图流作为JPEG传递给客户端。

    ImageGrabber.ashx代码:
    仅一行:
    <%@ WebHandler language="c#" Class="WebApplication1.ImageGrabber" Codebehind="ImageGrabber.ashx.cs" %>

    ImageGrabber.ashx.cs代码:

     1using System;
     2using System.IO;
     3using System.Web;
     4using System.Drawing;
     5using System.Drawing.Imaging ;
     6using System.Data.SqlClient;
     7
     8namespace WebApplication1
     9{
    10    /// <summary>
    11    /// ImageGrabber 的摘要说明。
    12    /// </summary>

    13    public class ImageGrabber:IHttpHandler
    14    {
    15        public void ProcessRequest(HttpContext context)
    16        {
    17            string id = (string) context.Request["id"];
    18            if (id !=null)
    19            {
    20                MemoryStream stream =new MemoryStream() ;
    21                SqlConnection connection = new SqlConnection ("user id=sa;data source=localhost;persist security info=True;initial catalog=Northwind;password=xiayc");
    22                Bitmap bitmap=null;
    23                Image image=null;
    24
    25                try
    26                {
    27                    connection.Open();
    28                    SqlCommand command =new SqlCommand ("SELECT Photo from Employees WHERE EmployeeID = '" + id +"'" ,connection);
    29                    
    30                    byte[] blob =(byte[]) command.ExecuteScalar();
    31                    stream.Write (blob,78,blob.Length-78);
    32                    bitmap=new Bitmap(stream);
    33
    34                    int width =48;
    35                    int height = (int)(width*((double) bitmap.Height /(double) bitmap.Width ));
    36                    image =bitmap.GetThumbnailImage(width,height,null,IntPtr.Zero );
    37
    38
    39                    context.Response.ContentType="image/jpeg";
    40                    image.Save (context.Response.OutputStream,ImageFormat.Jpeg );
    41
    42                 
    43                }

    44                finally
    45                {
    46                    if (image !=null)
    47                        image.Dispose();
    48                    if (bitmap !=null)
    49                        bitmap.Dispose();
    50                    stream.Close();
    51                    connection.Close();
    52
    53                }

    54            }

    55        }

    56
    57        public bool IsReusable
    58        {
    59            get {return true;}
    60        }

    61    }

    62}

    63
  • 相关阅读:
    第0次作业
    第4次作业
    第3次作业
    第2次作业
    C#浮点数保留位数
    第0次作业
    软件工程第4次作业
    软件工程第3次作业
    软件工程第2次作业
    软件工程第1次作业
  • 原文地址:https://www.cnblogs.com/xyicheng/p/189993.html
Copyright © 2011-2022 走看看