5 data randomly read in AJAX database dynamic refresh _ajax related in the page

Source: Internet
Author: User
Can not use the top of the database, later found in such a way to achieve ... Here is a summary of this method to write a page. Friends who are interested can study together ....
Foreground code:
Copy Code code 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 ">
<title>ajax randomly generated data </title>
<script language= "javascript" type= "Text/javascript" src= "Ajax/jquery.js" ></script>
<script >
Initialization of the page
$ (document). Ready (function () {randomdata ()});
Page initialization function
function Randomdata ()
{
$.ajax ({
Type: ' POST ',
URL: ' ajaxrandomdata.aspx ',
Data:{action: ' Randon '},
Success:randomdatacallbace
});
}
Page initialization callback function
function Randomdatacallbace (data)
{
if (data== "")
{
Alert ("Cannot generate data for you temporarily");
$ ("#randomData"). HTML ("Generate data for you temporarily");
}
Else
{
$ ("#randomData"). HTML (data);
Randomtime ()://every 5 seconds
}
}
Dynamically changing the data displayed in the page.
function Randomtime ()
{
settimeout (function () {randomdata ()},2000)
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div style= "Text-align:center" id= "Randomdata" >
</div>
</form>
</body>

Background code:
Copy Code code 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>
Create Person: Zhou Xin
Date Created: 2009-6-9
Method Name: InitData
Method action: Dynamically generate tables and randomly generate 5 of data
</summary>
public void InitData ()
{
SqlConnection mycon = new SqlConnection ();
Mycon. ConnectionString = configurationmanager.connectionstrings["Boboconn"]. ToString ();
The key to randomly generating data
String sql = "SELECT Top 5 *,newid () to 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 ' > Dynamic refresh User information </td></tr><tr style= ' color: #6600FF ' &GT;&LT;TD style= ' text-align:left;width:100px ' > Username </td><td style= ' text-align:left;width:200px ' > User full name </td &GT;&LT;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 ();
}
}

Effect chart

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.