Front-end code:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "servestudent. aspx. cs" inherits = "servestudent" %>
<! 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 title page </title>
<Script language = "JavaScript" type = "text/JavaScript" src = "ajax/jquery. js"> </SCRIPT>
<Script language = "JavaScript" type = "text/JavaScript">
// Events bound to the server control savebtn during initialization
$ (Function (){
$ ("# Savebtn"). Click (
Function (){
VaR username = $ ("# txtusername"). Val ();
If (username. Length = 0)
{
Alert ("user name cannot be blank ");
Return false;
}
VaR Pwd = $ ("# txtpwd"). Val ();
If (PWD. Length = 0)
{
Alert ("the password cannot be blank ");
Return false;
}
$. Ajax ({
Type: 'post ',
URL: 'servestudent. aspx ',
Data: {Action: 'action', Username: username, PWD: Pwd },
Success: savesuccesscallbace
})
}
)
});
// The callback function after the storage is successful
Function savesuccesscallbace (r)
{
If (r = "OK ")
{
Alert ('saved successfully ');
$ ("# Savespan" pai.html (" saved successfully ");
}
Else
{
$ ("# Savespan" 2.16.html (" failed to save ");
Return;
}
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: textbox id = "txtusername" runat = "server"> </ASP: textbox>
<Asp: textbox id = "txtpwd" runat = "server" textmode = "password"> </ASP: textbox>
<Asp: button id = "savebtn" runat = "server" text = "save"/> </div>
<Span id = "savespan"> </span>
</Form>
</Body>
</Html>
Background code:
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Public partial class servestudent: system. Web. UI. Page
{
String straction = "";
Protected void page_load (Object sender, eventargs E)
{
Straction = request ["action"];
If (straction = "action ")
{
String username = request ["username"];
String Pwd = request ["PWD"];
If (savedata (username, PWD ))
{
Response. Clear ();
Response. contenttype = "application/text ";
Response. Write ("OK ");
Response. End ();
}
Else
{
Response. Clear ();
Response. contenttype = "application/text ";
Response. Write ("no ");
Response. End ();
}
}
}
/// <Summary>
/// Creation Time: 2009-6-9
/// Created by Zhou Xin
/// Method Name: savedata ();
/// Function: used to determine whether the information is successfully saved.
/// </Summary>
/// <Param name = "username"> </param>
/// <Param name = "PWD"> </param>
/// <Returns> </returns>
Public bool savedata (string username, string PWD)
{
Sqlconnection mycon = new sqlconnection ();
Mycon. connectionstring = configurationmanager. connectionstrings ["boboconn"]. tostring ();
Mycon. open ();
String SQL = "insert into test values (@ username, @ PWD )";
Sqlcommand mycom = new sqlcommand (SQL, mycon );
Mycom. Parameters. Add ("@ username", sqldbtype. varchar, 50). value = username;
Mycom. Parameters. Add ("@ PWD", sqldbtype. varchar, 50). value = PWD;
Int n = (INT) mycom. executenonquery ();
Mycon. Close ();
If (n> 0)
{
Return true;
}
Else
{
Return false;
}
}
}