The backend stores data to the database using WebServices in. net. When Using Ajax to call WebSerivce, especially when using post for submission, you must pay attention to the protocol and put it on the IIS server for normal calls.
HTML code:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> no need to display replies </title>
<Style type = "text/css">
Body
{
Font-size: 12px;
Color: #414141;
Margin: 0px;
Padding: 0px;
}
# DivView
{
Margin: 0px;
Padding: 0px;
Width: 30%;
}
# DivView ul
{
List-style-type: none;
Line-height: 28px;
Margin: 0px;
Padding: 0px;
}
# DivView li
{
Border: solid 1px # ccc;
Border-bottom: 0px;
Line-height: 28px;
}
# Ul
{
List-style-type: none;
Line-height: 24px;
Margin: 0px;
Padding: 0px;
Width: 30%;
}
# Ul li
{
Border: solid 1px # ccc;
Border-bottom: 0px;
Line-height: 28px;
}
. Input
{
Border: solid 1px # ccc;
Width: 200px;
Background-color: # fff;
}
</Style>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div id = "divView" runat = "server">
</Div>
<Ul id = "ul">
<Li style = "background-color: # ccc; font-size: 14px; font-weight: 100"> reply </li>
<Li> title: <input type = "text" id = "txtTitle" class = "input"/> </li>
<Li> content: <textarea class = "input" id = "ttaContext" rows = "5" cols = "20"> </textarea> </li>
<Li>
<Input type = "button" value = "Submit" id = "btnSubmit"/> </li>
</Ul>
</Form>
</Body>
<Script type = "text/javascript">
Var xmlHttp;
Function $ (str) {return document. getElementById (str );}
Function createXmlHttp ()
{
XmlHttp = window. ActiveXObject? New ActiveXObject ("msxml2.xmlHttp"): new XMLHttpRequest ();
}
Function ajax ()
{
Var title = $ ("txtTitle"). value;
Var context = $ ("ttaContext"). value;
If (title = "")
{
Alert ("Enter the title ");
$ ("TxtTitle"). focus ();
} Else if (context = "")
{
Alert ("Enter content ");
$ ("TtaContext"). focus ();
} Else
{
CreateXmlHttp ();
XmlHttp. onreadystatechange = function ()
{
If (xmlHttp. readyState = 4)
{
Var temp = xmlHttp. responseText;
Var reg =/> (\ d) </ig;
Var regex =/\ d/ig;
If(parseInt(regex.exec(reg.exe c (temp)> 0)
{
$ ("TxtTitle"). value = "";
$ ("TtaContext"). value = "";
If ($ ("liNull ")! = Null)
{
$ ("DivView"). removeChild ($ ("liNull "));
}
Var ul = document. createElement ("ul ");
Var li_t = document. createElement ("li ");
Var li_c = document. createElement ("li ");
Li_t.innerHTML = "<B>" + title + "</B> ";
Ul. appendChild (li_t );
Li_c.innerHTML = context;
Ul. appendChild (li_c );
$ ("DivView"). appendChild (ul );
} Else
{
// Call the sending server at the prompt layer .....
}
}
}
XmlHttp. open ("post", "WebService. asmx/InsertPostById", true );
XmlHttp. setRequestHeader ("Content-type", "application/x-www-form-urlencoded ");
XmlHttp. send ("title =" + escape (title) + "& context =" + escape (context ));
}
}
$ ("BtnSubmit"). onclick = ajax;
</Script>
</Html>
C # code:
/// <Summary>
/// Add reply Method
/// </Summary>
/// <Param name = "title"> information title </param>
/// <Param name = "context"> information content </param>
/// <Returns> </returns>
[WebMethod]
Public string InsertPostById (string title, string context)
{
SqlCommand cmd = new SqlCommand ();
Using (SqlConnection conn = new SqlConnection (ConfigurationManager. ConnectionStrings ["SQL _STRING"]. ToString ()))
{
Conn. Open ();
Cmd. Connection = conn;
Cmd. CommandType = CommandType. Text;
Cmd. CommandText = "insert into tbposts (PTITLE, PCONTEXT) VALUES (@ PTITLE, @ PCONTEXT )";
SqlParameter [] parms = new SqlParameter []
{
New SqlParameter ("@ PTITLE", SqlDbType. NVarChar, 100 ),
New SqlParameter ("@ PCONTEXT", SqlDbType. NVarChar, 3000)
};
Parms [0]. Value = Server. UrlDecode (title );
Parms [1]. Value = Server. UrlDecode (context );
Cmd. Parameters. AddRange (parms );
String val = cmd. ExecuteNonQuery (). ToString ();
Cmd. Parameters. Clear ();
Return val;
}
}
Page loading C # method:
Protected void Page_Load (object sender, EventArgs e)
{
List <post> postList = GetPostList ();
If (postList. Count <= 0)
{
This. divView. InnerHtml = "<li id = 'linull '> no reply! </Li> ";
Return;
}
Foreach (post postInfo in postList)
{
String temp = "<ul> <li> <B>" + postInfo. PTitle + "</B> </li> <li>" + postInfo. PContext + "</li> </ul> ";
This. divView. InnerHtml + = temp;
}
}
Public List <post> GetPostList ()
{
SqlCommand cmd = new SqlCommand ();
SqlConnection conn = new SqlConnection (ConfigurationManager. ConnectionStrings ["SQL _STRING"]. ToString ());
Try
{
Conn. Open ();
Cmd. CommandType = CommandType. Text;
Cmd. Connection = conn;
Cmd. CommandText = "select pid, PTITLE, pcontext from tbposts ";
List <post> posts = new List <post> ();
Using (SqlDataReader rdr = cmd. ExecuteReader (CommandBehavior. CloseConnection ))
{
While (rdr. Read ())
{
Post postInfo = new post (rdr. GetInt32 (0), rdr. GetString (1), rdr. GetString (2 ));
Posts. Add (postInfo );
}
Return posts;
}
}
Catch
{
Conn. Close ();
Return null;
}
}
// A class in the class is declared here
Public class post
{
Public post (){}
/// <Summary>
///
/// </Summary>
/// <Param name = "pid"> ID </param>
/// <Param name = "ptitle"> title </param>
/// <Param name = "pcontext"> content </param>
Public post (int pid, string ptitle, string pcontext)
{
This. PID = pid;
This. PTitle = ptitle;
This. PContext = pcontext;
}
Public int PID;
Public string PTitle;
Public string PContext;
}
Of course, page loading can also be implemented using Ajax, but it does not feel that it is necessary to spend that much ..