ashx的學習


                嘿嘿,今天我們休息,本來是想總結一下前兩周學習的javascript和jquery,但是感覺好困哦,就沒有認真地學習啦,於是做了一個小小的練習,剛開始學習html使用在項目中還是蠻高興的啦,下面就簡單的總結一下這個小小的登錄頁面。

         一.html的靜態頁面

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style>
        form{
             margin-right:500px;
             margin-top:10px;
             width:300px;
             height:300px;   
          }
    </style>
</head>
<body>
    <form action="first.ashx" method="post">
        <table>
            <tr><td>用戶名:</td><td><input type="text" name="txtname" /></td></tr><br />
            <tr><td>密 &nbsp;碼:</td><td><input type="text" name="txtpwd"  /></td></tr><br />
            <tr><td><input type="submit" name="submit" value="登錄" /></td>
                <td><input type="button" name="btnFindPwd" value="找回密碼 " /></td></tr>
        </table>
    </form>
</body> 

                    這里是寫了一個簡單的html頁面,實現其登錄界面的樣式。

                     二.ashx的文件代碼

using System;
using System.Web;
using System.IO;
using UseiInfoModel;
using UserInfoBll;

public class first : IHttpHandler {  
       public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";          //接受的是html格式的文檔

        string path = context.Request.MapPath("FirstHtml.html");   //獲取文檔的路徑
        string html = File.ReadAllText(path);               //讀取文檔
        context.Response.Write(html);         //然后寫入,即返回給我們的是html頁面
            
        string name=context.Request.Form["txtname"];     //獲取txtname
        string pwd = context.Request.Form["txtpwd"];    //獲取txtpwd
        if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(pwd))    //當文本框不為空
        {
            Userinfobll bll = new Userinfobll();
            Userinfomodelcs model = bll.GetLoginByNamePwd(name, pwd);   //調用數據
            if(bll!=null&&string.IsNullOrEmpty(model.Username)&&string.IsNullOrEmpty(model.Pwd))
            {
                context.Response.Clear();
                context.Response.Write("歡迎" + model.Username + "登陸成功");    //相應報文
            }
        }
    }   
    public bool IsReusable {
        get {
            return false;
        }
    }
}

                這就是新學習ashx文件,實現請求報文和響應報文。在這里實現了html與服務器的交互。
                 三.bll層和dal層的代碼

public class Userinfobll
    {
        Userinfodal dal = new Userinfodal();
        public Userinfomodelcs GetLoginByNamePwd(string name, string pwd)
        {
            return dal.GetLoginByNamePwd(name,pwd);
        }
    }
 public class Userinfodal
    {
        public Userinfomodelcs GetLoginByNamePwd(string name,string pwd)
        {//Id, Username, Pwd
            string sql = "select Id,Username,Pwd from UserLogin where Username=@name and Pwd=@pwd";
            SqlParameter[] parms ={
                                    new SqlParameter("@name",name),
                                    new SqlParameter("@pwd",pwd)
                                };
            SqlDataReader reader= DBHelp.ExecuteReader(sql,parms);
            Userinfomodelcs model = new Userinfomodelcs();
            if (reader.Read())
            {
                model.Id = Convert.ToInt32(reader[0]);
                model.Username = reader[1].ToString();
                model.Pwd = reader[1].ToString();
            }
            return model;
        }
    }
public static class DBHelp
    {
        private static string connection = ConfigurationManager.ConnectionStrings["sql"].ToString();
        public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] parms)
        {
            SqlConnection conn = new SqlConnection(connection);
            conn.Open();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = sql;
                cmd.Parameters.AddRange(parms);
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandTimeout = 5;
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
        }
    }
 public class Userinfomodelcs
    {//Id, Username, Pwd
        public int Id { set; get; }
        public string Username { set; get; }
        public string Pwd { set; get; }
    }

               嘿嘿,一直以為使用aspx實現其數據的提交與響應,今天學習了ashx感覺這個很奇怪,使用起來還是蠻不熟悉的,首先在實現其代碼的過程中感覺不是直接和頁面交互,而是一切和數據有關的和頁面和有關的都要去實現,並不是很簡單的那樣,嘿嘿,這只是個人的意見,不知道大家在學習這個時間是不是這樣的感覺那,怎么說那?可能接下來我們要學習ajax,學習完這個就好多啦,與頁面的交互會更加的方便吧,但是之前也沒怎么接觸ajax,只是看到啦和js中使用,具體的還是不了解的,就寫到這里啦,最近學習的理論知識還沒有總結,感覺真的是需要再給點時間理解一下,需要了解清楚在總結。要繼續努力!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM