Use stitching string to write a simplified version of the background website (recommended beginners)

Source: Internet
Author: User

Today with the general processing program ASHX do a following effect,

The main purpose is to realize the function, the interface ugly is ugly.

First Display Interface Domehandler.ashx

   public void ProcessRequest (HttpContext context) {context.        Response.ContentType = "text/html";        StringBuilder sb = new StringBuilder (); Connection string str = system.configuration.configurationmanager.connectionstrings["DataBase"].        ConnectionString;        String sql = "SELECT Top (Ten) ProductID, ProductName, PTID, PRODUCTJP, productprice from Products";            using (SqlConnection conn=new SqlConnection (str)) {using (SqlCommand cmd=new SqlCommand (sql,conn)) {Conn.                Open (); using (SqlDataReader reader= cmd. ExecuteReader ()) {while (reader. Read ()) {sb. AppendFormat ("<tr><td>{0}</td><td>{1}</td><td><a href= ' ShowDetail.ashx?") id={0} ' > Details </a><td/><td><a href= ' deletedata.ashx?id={0} ' onclick= ' return confirm (\ "Do you want to delete \" ) ' > Delete </a><td/><td><a href= ' edit.asHX?ID={0} ' > Modify </a><td/></tr> ', reader["ProductID"], reader["ProductName"]); }}}}//Convert virtual path to absolute path string path=context.        Request.mappath ("/listhandler.html");        String Html=system.io.file.readalltext (path); Html=html. Replace ("@changeStr", sb.        ToString ()); Context.            Response.Write (HTML);  }
The listhandler.html is a template, which is an HTML page

Use placeholders to place the characters you want to replace in the page, and replace the data with replace


Then the details page showdetail.ashx



public void ProcessRequest (HttpContext context) {context.        Response.ContentType = "text/html"; Gets the argument passed over the string id = context.        request.querystring["id"]; int sqlid = Int.                Parse (ID); String str=system.configuration.configurationmanager.connectionstrings["DataBase".        ConnectionString;                String sql= "Select ProductID, ProductName, PTID, PRODUCTJP, productprice from Products where [email protected]";                Variable string StringBuilder sb = new StringBuilder ();        System.Data.DataTable dt = new System.Data.DataTable (); Using (SqlDataAdapter Adapter=new SqlDataAdapter (SQL,STR)) {adapter.            SELECTCOMMAND.PARAMETERS.ADD ("@id", sqlid); Adapter.        Fill (DT); } sb. AppendFormat ("<td>{0}</td>", dt.        rows[0]["ProductID"]); Sb. AppendFormat ("<td>{0}</td>", dt.        rows[0]["ProductName"]); Sb. AppendFormat ("<td>{0}</td>", dt.  rows[0]["PRODUCTJP"]);      Sb. AppendFormat ("<td>{0}</td>", dt.                rows[0]["Productprice"]); Gets the contents of another page, string path=context.        Request.mappath ("/showdatadetail.html");        String html = file.readalltext (path); html = HTML. Replace ("@changeStr", sb.        ToString ()); Context.    Response.Write (HTML); }

Then delete the page deletedata.ashx


    public void ProcessRequest (HttpContext context) {context.                Response.ContentType = "text/html"; Gets the id string id = context.        request.querystring["id"];        int sqlid; Int.        TryParse (ID, out sqlid);        String sql = "Delete from the products where [email protected]"; String str = system.configuration.configurationmanager.connectionstrings["DataBase"].        ConnectionString;        if (sqlid==null) {return; } using (SqlConnection conn = new SqlConnection (str)) {using (SqlCommand cmd=new SqlCommand (SQL, conn)) {cmd.                Parameters.Add ("@id", sqlid); Conn.                Open (); int rows = cmd.                ExecuteNonQuery (); if (Rows > 0) {context.                Response.Redirect ("Domehandler.ashx"); } else {context.                Response.Write ("delete failed");     }            }   }                } 

Last slightly difficult to modify the page edit.ashx


   public void ProcessRequest (HttpContext context) {context.                Response.ContentType = "text/html"; Gets whether to show or submit if (context. request["edit"] = = "Edit") {String id = context.            request["id"]; String productName = context.            request["ProductName"]; String Productprice = context.            request["Productprice"];            int sqlid; Int.            TryParse (ID, out sqlid);            if (sqlid = = null) {return; } String sql = "Update products set [email protected],[email protected] where [email protected]"            ; String str = system.configuration.configurationmanager.connectionstrings["DataBase"].            ConnectionString; using (SqlConnection conn = new SqlConnection (str)) {using (SqlCommand cmd = new SqlCommand (sql , conn)) {cmd.                    Parameters.Add ("@id", sqlid); Cmd. Parameters.Add ("@ProductNAme ", productName); Cmd.                    Parameters.Add ("@ProductPrice", Productprice); Conn.                    Open (); int rows = cmd.                    ExecuteNonQuery (); if (Rows > 0) {//Jump page to presentation interface context.                    Response.Redirect ("Domehandler.ashx"); } else {context.                    Response.Write ("modification failed"); }}}} else {//get id String id = context.            request.querystring["id"];            int sqlid; Int.            TryParse (ID, out sqlid);            String sql = "Select ProductID, ProductName, productprice from Products where [email protected]"; String str = system.configuration.configurationmanager.connectionstrings["DataBase"].            ConnectionString;                        System.Data.DataTable dt = new System.Data.DataTable (); if (sQlid = = null) {return; } using (SqlDataAdapter adapter = new SqlDataAdapter (SQL, str)) {adapter.                SELECTCOMMAND.PARAMETERS.ADD ("@id", sqlid); Adapter.            Fill (DT); } string Html=system.io.file.readalltext (context.            Request.mappath ("edit.html")); html = HTML. Replace ("@id", dt. rows[0]["ProductID"].            ToString ()); html = HTML. Replace ("@ProductName", dt. rows[0]["ProductName"].            ToString ()); html = HTML. Replace ("@ProductPrice", dt. rows[0]["Productprice"].            ToString ()); Context.        Response.Write (HTML);  }    }

Send another edit.html.


<! DOCTYPE html>
In fact, this method is similar, as long as mastered the stitching jump skills, you can write their own play.

Which involves some of the database read, not in detail.


Use stitching string to write a simplified version of the background website (recommended beginners)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.