Five data records are randomly read from the ajax database and dynamically refreshed on the page.

Source: Internet
Author: User

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:
Copy codeThe Code is as follows:
<% @ 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:
Copy codeThe Code is as follows:
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 ();
}
}



Related Article

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.