The following are the problems I encountered when writing a program here. At the beginning, the manager assigned me five data records to be dynamically displayed on the page. You cannot use top in the database. You can use this method later... This method summarizes and writes a page. If you are interested, you can study together ....
Front-end code:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "ajaxrandomdata. aspx. cs" inherits = "ajaxrandomdata" %>
<! 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> randomly generated Ajax data </title>
<Script language = "JavaScript" type = "text/JavaScript" src = "ajax/jquery. js"> </SCRIPT>
<SCRIPT>
// Page Initialization
$ (Document). Ready (function () {randomdata ()});
// Page initialization Function
Function randomdata ()
{
$. Ajax ({
Type: 'post ',
URL: 'ajaxrandomdata. aspx ',
Data: {Action: 'rand '},
Success: randomdatacallbace
});
}
// Page initialization callback function
Function randomdatacallbace (data)
{
If (Data = "")
{
Alert ("data cannot be generated for you temporarily ");
$ ("# Randomdata" ).html ("data cannot be generated for you temporarily ");
}
Else
{
$ ("# Randomdata" pai.html (data );
Randomtime (); // execute once every 5 seconds
}
}
// The data displayed on the Dynamic Change page.
Function randomtime ()
{
SetTimeout (function () {randomdata ()}, 2000)
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div style = "text-align: Center" id = "randomdata">
</Div>
</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;
Using system. text;
Public partial class ajaxrandomdata: system. Web. UI. Page
{
String straction = "";
Protected void page_load (Object sender, eventargs E)
{
Straction = request ["action"];
If (straction = "randon ")
{
Initdata ();
}
}
/// <Summary>
/// Created by Zhou Xin
/// Creation Time: 2009-6-9
/// Method Name: initdata
/// Method: dynamically generate a table and randomly generate 5 data entries
/// </Summary>
Public void initdata ()
{
Sqlconnection mycon = new sqlconnection ();
Mycon. connectionstring = configurationmanager. connectionstrings ["boboconn"]. tostring ();
// Key to random data generation
String SQL = "select top 5 *, newid () from loginuser order by newid ()";
Sqldataadapter myda = new sqldataadapter (SQL, mycon );
Dataset myset = new dataset ();
Myda. Fill (myset );
Datatable mytable = myset. Tables [0];
Int n = mytable. Rows. count;
Stringbuilder STR = new stringbuilder ();
Str. append ("<Table Style = 'text-align: Left; color: red; width: 600px '> <tr style = 'color: #00ff00; font-size: 40px; text-align: Center'> <TD colspan = '3'> dynamically refresh User Information </TD> </tr> <tr style = 'color: # 6600ff '> <TD style = 'text-align: Left; width: 100px'> User Name </TD> <TD style = 'text-align: Left; width: 200px '> full user name </TD> <TD style = 'width: 250px'> electronic fuel tank </TD> </tr> ");
For (INT I = 0; I <n; I ++)
{
String username = mytable. Rows [I] ["username"]. tostring ();
String fullname = mytable. Rows [I] ["fullname"]. tostring ();
String email = mytable. Rows [I] ["email"]. tostring ();
If (I % 2! = 0)
{
Str. append ("<tr> <TD>" + username + "</TD> ");
Str. append ("<TD>" + fullname + "</TD> ");
Str. append ("<TD>" + email + "</TD> </tr> ");
}
Else
{
Str. append ("<tr style = 'color: Blue'> <TD>" + username + "</TD> ");
Str. append ("<TD>" + fullname + "</TD> ");
Str. append ("<TD>" + email + "</TD> </tr> ");
}
}
Str. append ("</table> ");
Response. Clear ();
Response. contenttype = "application/text ";
Response. Write (STR );
Response. End ();
}
}