關於前台的頁面我們后台輸出


一。從后台直接輸出HTML格式的內容。

二。我們一開始是在AJAX到后台,后台得到DATATABLE傳遞過來。然后對應的數據進行解析,JS輸出到前台(帶分頁)。

 缺點:JS的內容容易被看到。

JS第一種格式:一條條數據,並帶有對應的操作(刪除,查看)等功能。)——很實用。列出數據並帶有分頁時。

/* ------------------------------------- 分頁模塊(編輯,刪除,其他) ------------------------------------- */
function pager(page, maxPage, total, per, countsIsVisible, key, colum, utilUrl, editUrl, otherSetName, otherSetUrl)//分頁的輔助方法
{
//(請求的當前頁,最大頁數,總信息量,頁的大小,詳細分頁信息是否可見,"關鍵字的<input/>ID",數據的列數,"字典表類型","后台處理類URL","添加或修改標題","添加或修改頁面")

var first = "<font style='font-size:12px'>首頁</font>";
var previous = "<font style='font-size:12px'>上一頁</font>";
var next = "<font style='font-size:12px'>下一頁</font>";
var last = "<font style='font-size:12px'>尾頁</font>";

var s = "<div id='pagerDiv' class='pager-block' style='width:100%;text-align:right'><div class='pager-links' style='float:left; text-align:left; font-family:courier new,sans-serif'>";

//判斷當前頁是否是第一頁,從而控制顯示方式
if (page == 1) {
s += (first + " " + previous + " <b>");
}
else {
if (first != "") s += ("<a href='javascript:getPageListSet(1,\"" + key + "\"," + colum + ",\"" + utilUrl + "\",\"" + editUrl + "\",\"" + otherSetName + "\",\"" + otherSetUrl + "\")'>" + first + "</a> ");
s += ("<a href='javascript:getPageListSet(" + (page - 1) + ",\"" + key + "\"," + colum + ",\"" + utilUrl + "\",\"" + editUrl + "\",\"" + otherSetName + "\",\"" + otherSetUrl + "\")'>" + previous + "</a> <b>");
}

//判斷:當總頁數多於6頁時,省略中間頁碼,只顯示第一頁和最后一頁
var j = (page - 5 < 1) ? 1 : page - 5;
var k = (page + 5 > maxPage) ? maxPage : page + 5;
//顯示第一頁
if (j > 1) s += " <a href='javascript:getPageListSet(1,\"" + key + "\"," + colum + ",\"" + utilUrl + "\",\"" + editUrl + "\",\"" + otherSetName + "\",\"" + otherSetUrl + "\")'>1</a> ... ";
//顯示中間頁碼
for (var i = j; i < k + 1; i++) {
if (page == i) {
s += ("<span class='t2'>" + i + "</span> ");
continue;
}
s += ("<a href='javascript:getPageListSet(" + i + ",\"" + key + "\"," + colum + ",\"" + utilUrl + "\",\"" + editUrl + "\",\"" + otherSetName + "\",\"" + otherSetUrl + "\")'>" + i + "</a> ");
}
//顯示最后一頁
if (maxPage > k) s += " ... <a href='javascript:getPageListSet(" + maxPage + ",\"" + key + "\"," + colum + ",\"" + utilUrl + "\",\"" + editUrl + "\",\"" + otherSetName + "\",\"" + otherSetUrl + "\")'>" + maxPage + "</a> ";
//判斷當前頁是否是最后一頁,從而控制顯示方式
if (page == maxPage) {
s += ("</b>" + next + " " + last);
}
else {
s += ("</b><a href='javascript:getPageListSet(" + (page + 1) + ",\"" + key + "\"," + colum + ",\"" + utilUrl + "\",\"" + editUrl + "\",\"" + otherSetName + "\",\"" + otherSetUrl + "\")'>" + next + "</a> ");

if (last != "") s += ("<a href='javascript:getPageListSet(" + maxPage + ",\"" + key + "\"," + colum + ",\"" + utilUrl + "\",\"" + editUrl + "\",\"" + otherSetName + "\",\"" + otherSetUrl + "\")'>" + last + "</a>");
}
//根據輸入的頁號,直接顯示該頁數據
s += " 轉到 <input type='text'id='gotopage' name='gotopage' value='" + page + "' size='3' /> <input type='button'class='button' value='Go' style='height:19px;' onclick='ongoto(\"" + maxPage + "\",\"" + key + "\"," + colum + ",\"" + utilUrl + "\",\"" + editUrl + "\",\"" + otherSetName + "\",\"" + otherSetUrl + "\")' />";
s += ("</div>");

//控制“總記錄”“每頁”“總頁數”的顯示
if (countsIsVisible) {
s += ("<div class='pager-stat' style='float:lift;'>");
s += ("總記錄:<span class='t2'>" + total + "</span> ");
s += ("每頁:<span class='t2'>" + per + "</span> ");
s += ("總頁數:<span class='t2'>" + maxPage + "</span></div>");
}
s += "</div>";

return s;
}
function ongoto(maxPage, key, colum, utilUrl, otherSetName, otherSetUrl)//直接跳轉到設定好的某一頁
{
if(isDigits("gotopage")){
var p = document.getElementById("gotopage").value;

if(p!=0){
p = (parseInt(p) > parseInt(maxPage)) ? maxPage : p;
getPageListSet(p, key, colum, utilUrl, otherSetName, otherSetUrl);}
else{
alert("請輸入正確的頁碼數字!");
getPageListSet(1, key, colum, utilUrl, otherSetName, otherSetUrl);

return false;
}
}
else{
alert("請輸入正確的頁碼數字!");
getPageListSet(1, key, colum, utilUrl, otherSetName, otherSetUrl);

return false;
}
}

function del(id, utilUrl, key, colum, otherSetName, otherSetUrl)//刪除一條數據(id,"后台處理類","編輯頁面","關鍵字的<input/>ID",數據列數,"字典表類型")
{
if (confirm("您確定刪除此條數據嗎?")) {
$.ajax({
url: utilUrl,
type: 'POST',
data: "id=" + id + "&operationType=" + 3,
dataType: 'json',
timeout: 1000,
error: function(data) { alert(data.result); },
success: function(data) {
alert(data.result);
}

});
getPageListSet(document.getElementById("gotopage").value, key, colum, utilUrl, otherSetName, otherSetUrl);
}
}
/* ------------------------------------- 查詢,並返回分頁信息(注意頁面的命名規則,dataTb,pageTb分別是存放數據和存放分頁信息的Table) ------------------------------------- */

function removeAll() //清空容器中已經存在的數據(注意頁面中的容器的命名規則)
{
var t = document.getElementById("dataTb"); //數據容器
var pageTb = document.getElementById("pageTb"); //分頁信息容器

for (var i = t.rows.length - 1; i >= 1; i--) {
t.deleteRow(i);
}
for (var j = pageTb.rows.length - 1; j >= 0; j--) {
pageTb.deleteRow(0);
}
}

function getPageListSet(pageIndex, keyInputId, colum, utilUrl, otherSetName, otherSetUrl)//獲取含有刪除,修改,設置功能的分頁數據(當前請求的頁碼,"關鍵字的<input/>ID",數據的列數,"后台處理類URL","添加或修改頁面的URL","設置彈出表單的標題名稱","查看詳情頁面對應的URL")
{
var operationType = 1; //操作類型
var pdata = "operationType=" + operationType + "&pageIndex=" + pageIndex; //操作字符串 參數傳遞

if (keyInputId != "") {
var xx = keyInputId.split(","); //獲得關鍵字的數組
for (var i in xx) {
var keyi = xx[i];
pdata += "&" + keyi + "=" + document.getElementById(keyi).value; //遍歷並添加 。因為這里要取到對應的值,所以 傳遞的是 ID值。
}
} else {
var x = "";
pdata += "&startTime=" + x + "&endTime=" + x + "&Question=" + x + ""; //若取不到。這里直接寫為空
}

$.ajax({
url: utilUrl, //后台處理類
//body 的是到 'QuestionUtil.aspx'
type: "POST",
data: pdata,
dataType: "json",
timeout: 1000,
error: function(data) { /*alert("出現錯誤!或者是您沒有權限!");*/ },
success: function(data) {
removeAll(); //首先清空網頁中已經存在的歷史數據

if (data.result != null)//后台出現錯誤,返回的是"{result:'查詢失敗!'}",則提示錯誤信息
{
alert(data.result);
}
else {
if (data.total == 0)//如果返回的數據數為0
{
alert("您所要查詢的數據不存在!");
}
else {

var table = document.getElementById("dataTb"); //獲得顯示數據的容器
var cell, row, shenhe;
var width = window.screen.availWidth;
var height = window.screen.availHeight;
var iRow;
//在容器中輸出數據
for (iRow = 0; iRow < data.rows.length; iRow++) {//按行輸出
row = table.insertRow(iRow + 1); //因為 TABLE的標頭已經在HTML頁面設置完成 所以每行都要 +1
row.setAttribute("onmouseover", "this.className='highlight'");
row.setAttribute("onmouseout", "this.className=''");

if (iRow % 2 != 0) {
row.style.background = "#DFEFFA";
} //設置奇數偶數行的背景不一樣

for (var iCell = 0; iCell < colum + 1; iCell++) { //按列輸出 COLUM 是6 我們共有7 列 所以每列都要 +1
cell = row.insertCell(iCell); //從零到6 iCell < colum + 1 小於 7

if (iCell == 0) {
var tag = document.getElementById("tag").value; //tag 賦值
id = data.rows[iRow].cell[5]; //ID 是傳過來的 第六個值 從0開始
//編輯,刪除,“查看詳細內容是”從頁面傳遞過來的參數設置的。

cell.innerHTML = "<a href='javascript:del(\"" + id + "\",\"" + utilUrl + "\",\"" + keyInputId + "\"," + colum + ",\"" + otherSetName + "\",\"" + otherSetUrl + "\")'>刪除</a>|<a onclick=\"javascript:GB_myShow('" + otherSetName + "','" + otherSetUrl + "?id=" + id + "');\" href='javascript:void(0) '>" + otherSetName + "</a>";

cell.setAttribute("align", "center");
continue;
}
if (iCell == 1) {
//var isActive = data.rows[iRow].cell[0];
var check = data.rows[iRow].cell[6];
// if (isActive == "True") {
if (check == "審核通過" || check == "信審核通過" || check == "信審核通過回答") {
cell.innerHTML = "<img src='http://www.cnblogs.com/OpanelStyle/img/check_right.gif'/>";
}

if (check == "審核未通過") {
cell.innerHTML = "<img src='http://www.cnblogs.com/OpanelStyle/img/check_error.gif'/>";
}
// }
// else {
// if (check == "未審核" || check == "信未審核") {
// cell.innerHTML = "<lable><span style='color:Red'>未審核</span></lable>";
// }
// }
cell.setAttribute("align", "center");
continue;
}
if (iCell == 2) {//標題
var Question = data.rows[iRow].cell[1];
if (Question.length >= 28) {
Question = Question.substring(0, 28) + "...";
}
else {
Question = data.rows[iRow].cell[1];
}

cell.innerHTML = Question;
cell.setAttribute("align", "center");
continue;
}
// if (iCell == 3) {//答案
// var answer = data.rows[iRow].cell[2];
// if (answer.length >= 35) {
// answer = answer.substring(0, 35) + "...";
// }
// else {
// answer = data.rows[iRow].cell[2];
// }

// cell.innerHTML = answer;
// cell.setAttribute("align", "center");
// continue;
// }
if (iCell == 5) {
var date = data.rows[iRow].cell[4];
date = date.substr(0, date.lastIndexOf(":"));
cell.innerHTML = date;
cell.setAttribute("align", "center");
continue;
}
cell.setAttribute("align", "center");
cell.appendChild(document.createTextNode(data.rows[iRow].cell[iCell - 1]));
cell = null;
}
row = null;
}

var pageTable = document.getElementById("pageTb"); //獲得分頁模塊的容器
var pageIndex = data.page; //從后台獲取索引頁號
var total = data.total; //從后台獲取總的數據量
var pageSize = 10; //頁的大小
var maxPage = 0; //最大頁
if (total % pageSize == 0)//計算最大頁數
{
maxPage = Math.floor(total / pageSize);
} else {
maxPage = Math.floor(total / pageSize) + 1;
}

//輸出分頁模塊
if (data.rows.length != 0) {
row1 = pageTable.insertRow(0);
cell = row1.insertCell(0);
row1.setAttribute("colspan", "11");
//調用pager(page, maxPage, total, per, countsIsVisible)獲得分頁模塊,並從容器中輸出
cell.innerHTML = pager(pageIndex, maxPage, total, pageSize, true, keyInputId, colum, utilUrl, otherSetName, otherSetUrl);
}
}
}
}
});
}


//刷新重置
function reset() {
window.location.reload() ;
}

第二種方式:輸出帶圖片的各種信息。不帶操作。只是有對應的查看的超鏈接。(很實用。例如廣告排名)

/* ------------------------------------- 分頁模塊(編輯,刪除,其他) ------------------------------------- */
function pager(page, maxPage, total, per, countsIsVisible, key, colum, tableType, utilUrl)//分頁的輔助方法
{
//(請求的當前頁,最大頁數,總信息量,頁的大小,詳細分頁信息是否可見,"關鍵字的<input/>ID",數據的列數,"字典表類型","后台處理類URL","添加或修改頁面")

var first = "<font style='font-size:12px'>首頁</font>";
var previous = "<font style='font-size:12px'>上一頁</font>";
var next = "<font style='font-size:12px'>下一頁</font>";
var last = "<font style='font-size:12px'>尾頁</font>";

var s = "<div id='pagerDiv' class='pager-block' style='width:100%;text-align:right'><div class='pager-links' style='float:left; text-align:left; font-family:courier new,sans-serif'>";

//判斷當前頁是否是第一頁,從而控制顯示方式
if (page == 1) {
s += (first + " " + previous + " <b>");
}
else {
if (first != "") s += ("<a href='javascript:getPageListSet(1,\"" + key + "\"," + colum + ",\"" + tableType + "\",\"" + utilUrl + "\")'>" + first + "</a> ");
s += ("<a href='javascript:getPageListSet(" + (page - 1) + ",\"" + key + "\"," + colum + ",\"" + tableType + "\",\"" + utilUrl + "\")'>" + previous + "</a> <b>");
}

//判斷:當總頁數多於6頁時,省略中間頁碼,只顯示第一頁和最后一頁
var j = (page - 5 < 1) ? 1 : page - 5;
var k = (page + 5 > maxPage) ? maxPage : page + 5;
//顯示第一頁
if (j > 1) s += " <a href='javascript:getPageListSet(1,\"" + key + "\"," + colum + ",\"" + tableType + "\",\"" + utilUrl + "\")'>1</a> ... ";
//顯示中間頁碼
for (var i = j; i < k + 1; i++) {
if (page == i) {
s += ("<span class='t2'>" + i + "</span> ");
continue;
}
s += ("<a href='javascript:getPageListSet(" + i + ",\"" + key + "\"," + colum + ",\"" + tableType + "\",\"" + utilUrl + "\")'>" + i + "</a> ");
}
//顯示最后一頁
if (maxPage > k) s += " ... <a href='javascript:getPageListSet(" + maxPage + ",\"" + key + "\"," + colum + ",\"" + tableType + "\",\"" + utilUrl + "\")'>" + maxPage + "</a> ";
//判斷當前頁是否是最后一頁,從而控制顯示方式
if (page == maxPage) {
s += ("</b>" + next + " " + last);
}
else {
s += ("</b><a href='javascript:getPageListSet(" + (page + 1) + ",\"" + key + "\"," + colum + ",\"" + tableType + "\",\"" + utilUrl + "\")'>" + next + "</a> ");

if (last != "") s += ("<a href='javascript:getPageListSet(" + maxPage + ",\"" + key + "\"," + colum + ",\"" + tableType + "\",\"" + utilUrl + "\")'>" + last + "</a>");
}
//根據輸入的頁號,直接顯示該頁數據
s += " 轉到 <input type='text'id='gotopage' name='gotopage' value='" + page + "' size='3' /> <input type='button'class='button' value='Go' style='height:19px;' onclick='ongoto(\"" + key + "\"," + colum + ",\"" + tableType + "\",\"" + utilUrl + "\")' />";
s += ("</div>");

//控制“總記錄”“每頁”“總頁數”的顯示
if (countsIsVisible) {
s += ("<div class='pager-stat' style='float:lift;'>");
s += ("總記錄:<span class='t2'>" + total + "</span> ");
s += ("每頁:<span class='t2'>" + per + "</span> ");
s += ("總頁數:<span class='t2'>" + maxPage + "</span></div>");
}
s += "</div>";

return s;
}
function isDigits(id)//驗證字符串是否符合正整數格式
{

var str = document.getElementById(id).value
var regu = /^\d+$/;
return regu.test(str);
}
function ongoto(key, colum, tableType, utilUrl)//直接跳轉到設定好的某一頁
{
if (!isDigits("gotopage") || document.getElementById("gotopage").value == "0") {
alert("請你正確選擇頁碼!");
window.location.reload();
} else {
getPageListSet(document.getElementById("gotopage").value, key, colum, tableType, utilUrl);
}
}


/* ------------------------------------- 查詢,並返回分頁信息(注意頁面的命名規則,dataTb,pageTb分別是存放數據和存放分頁信息的Table) ------------------------------------- */

function removeAll() //清空容器中已經存在的數據(注意頁面中的容器的命名規則)
{
var t = document.getElementById("dataTb"); //數據容器
var pageTb = document.getElementById("pageTb"); //分頁信息容器

for (var i = t.rows.length - 1; i >= 1; i--) {
t.deleteRow(i);
}
for (var j = pageTb.rows.length - 1; j >= 0; j--) {
pageTb.deleteRow(0);
}
}

function getPageListSet(pageIndex, keyInputId, colum, tableType, utilUrl)//獲取含有刪除,修改,設置功能的分頁數據(當前請求的頁碼,"關鍵字的<input/>ID",數據的列數,"數據庫表的類型","后台處理類URL","添加或修改頁面","設置名稱","設置頁面")
{

var operationType = 1; //操作類型
var pdata = "operationType=" + operationType + "&pageIndex=" + pageIndex;


//alert(tableType);
$.ajax({
url: utilUrl, //后台處理類
type: "POST",
data: pdata,
dataType: "json",
timeout: 4000,
error: function(data) { alert("出現錯誤或者是您沒有權限!"); },
success: function(data) {
removeAll(); //首先清空網頁中已經存在的歷史數據

if (data.result != null)//后台出現錯誤,返回的是"{result:'查詢失敗!'}",則提示錯誤信息
{
alert(data.result);
}
else {
if (data.total == 0)//如果返回的數據數為0
{
alert("您所要查詢的數據不存在!");
}
else {

var table = document.getElementById("dataTb"); //獲得顯示數據的容器
var cell, row;
var width = window.screen.availWidth;
var height = window.screen.availHeight;
var iRow;
//在容器中輸出數據
for (iRow = 0; iRow < data.rows.length; iRow++) {

row = table.insertRow(iRow);
row.setAttribute("onmouseover", "this.style.backgroundColor='#F6F6F6'");
row.setAttribute("onmouseout", "this.style.backgroundColor=''");
var Id = data.rows[iRow].cell[10];
var title = data.rows[iRow].cell[0];
var beginDateTime = data.rows[iRow].cell[1];
var endDateTime = data.rows[iRow].cell[2];
var Photo = data.rows[iRow].cell[3];

var ProvinceCaption = data.rows[iRow].cell[4];
var CityCaption = data.rows[iRow].cell[5];

var Contents = data.rows[iRow].cell[6];
var LowNum = data.rows[iRow].cell[7];
var Memo = data.rows[iRow].cell[8];
var UserName = data.rows[iRow].cell[9];

var beginDate = beginDateTime.substr(0, beginDateTime.lastIndexOf(" "));

var endDate = endDateTime.substr(0, endDateTime.lastIndexOf(" "));

for (var iCell = 0; iCell < colum + 1; iCell++) {
cell = row.insertCell(iCell);

if (iCell == 0) {

var str = "";
str += "<div class='play_list'><a href='ActivityDetail.aspx?Id=" + Id + "&DetailTyle=3'target='_blank'><img alt='" + title + "' border='0' height='98' width='148' src='" + Photo + "' /></a>";
str += " <h4 style ='text-align :left;'><small>[活動]</small> <span><a href='ActivityDetail.aspx?Id=" + Id + "&DetailTyle=3' target='_blank'>" + title + "</a></span></h4>";
str += "<div class='xq'><dl> <dt>時 間:</dt><dd style ='text-align :left;'>" + beginDate + "<span>至</span>" + endDate + "</dd></dl>";
str += "<dl><dt>地 點:</dt><dd style ='text-align :left;'>" + ProvinceCaption + "" + CityCaption + "</dd></dl><dl> <dt>組織者:</dt><dd style ='text-align :left;'>" + UserName + "</dd></dl></div>";
str += "<div style ='text-align :left;'><a href='ActivityDetail.aspx?Id=" + Id + "&DetailTyle=3' class='wycj_button' target='_blank'><img src='images/baoming.gif' /></a></div><div class='time'><span><em>1740</em>人已報名</span></div></div>";
cell.innerHTML = str;
cell.setAttribute("align", "center");
//cell.setAttribute("height", "30");
continue;
}

cell.setAttribute("align", "center");
// cell.appendChild(document.createTextNode(data.rows[iRow].cell[iCell - 1]));

cell = null;
}
row = null;
}

var pageTable = document.getElementById("pageTb"); //獲得分頁模塊的容器
var pageIndex = data.page; //從后台獲取索引頁號
var total = data.total; //從后台獲取總的數據量
var pageSize = 10; //頁的大小
var maxPage = 0; //最大頁
if (total % pageSize == 0)//計算最大頁數
{
maxPage = Math.floor(total / pageSize);
} else {
maxPage = Math.floor(total / pageSize) + 1;
}

//輸出分頁模塊
if (data.rows.length != 0) {
row1 = pageTable.insertRow(0);
cell = row1.insertCell(0);
row1.setAttribute("colspan", "11");
//調用pager(page, maxPage, total, per, countsIsVisible)獲得分頁模塊,並從容器中輸出
cell.innerHTML = pager(pageIndex, maxPage, total, pageSize, true, keyInputId, colum, tableType, utilUrl);
}
}
}
}
});

}

三。把分頁模塊獨立到后台。然后從后台輸出HTML格式的此頁碼的數據。(帶分頁)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Web.Cpanel.Dics;
using DBUtility;

namespace Web
{
public partial class DefaultTwo : System.Web.UI.Page
{
private int pageNum { get; set; }
private int all { get; set; }
database.BLL.TeacherOnline tbll = new database.BLL.TeacherOnline();
database.Model.TeacherOnline tmodel = new database.Model.TeacherOnline();
database.BLL.StudentOnline sbll = new database.BLL.StudentOnline();
database.Model.StudentOnline smodel = new database.Model.StudentOnline();
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (Session["usertype"] == null)
{
userType.Value = "";
LinkButton1.Visible = false;
}
else
{
userType.Value = Session["usertype"].ToString();
LinkButton1.Visible = true;
}
Notice();//網站公告
huobanP();//合作伙伴
OperationType operationType = (OperationType)Enum.Parse(typeof(OperationType), Request["operationType"].ToString());//獲取操作類型
switch (operationType)
{
case OperationType.Question://最新問題
type.Value = operationType.ToString();
QuestionNew();
break;
case OperationType.Picture://專家圖片
type.Value = operationType.ToString();
PictureShow();
break;
case OperationType.Notice://網站公告
AllNotice();
break;
case OperationType.FNotice://網站分頁公告方法 PAGE參數 第幾頁
try
{
int param = int.Parse(Request["page"].ToString());
AllNotice(param);
}
catch
{ //Response.Write("<script type='text/javascript'>alert('請您輸入正確的頁碼!');</script>"); AllNotice(1); }
Common.WebMessage.Show(this, "請您輸入正確的頁碼!"); AllNotice();
}
break;
default:
break;
}
}
catch
{

}
}

/// <summary>
/// 網站公告(左側公告)
/// </summary>
public void Notice()
{
string sql = "select Id,Title from SiteNotice order by CreateTime DESC";
DataTable dt = DBUtility.DbHelperSQL.Query(sql).Tables[0];
if (dt.Rows.Count < 4)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["Title"].ToString().Length > 10)
{
dt.Rows[i]["Title"] = dt.Rows[i]["Title"].ToString().Substring(0, 10) + "......";
}
else
{
dt.Rows[i]["Title"] = dt.Rows[i]["Title"].ToString();
}
notice.InnerHtml += "<li><a href='DefaultThird.aspx?operationType=7&Id=" + dt.Rows[i]["Id"].ToString() + "'>" + dt.Rows[i]["Title"].ToString() + "</a></li>";

}
}
else
{
for (int i = 0; i < 4; i++)
{
if (dt.Rows[i]["Title"].ToString().Length > 10)
{
dt.Rows[i]["Title"] = dt.Rows[i]["Title"].ToString().Substring(0, 10) + "......";
}
else
{
dt.Rows[i]["Title"] = dt.Rows[i]["Title"].ToString();
}
notice.InnerHtml += "<li><a href='DefaultThird.aspx?operationType=7&Id=" + dt.Rows[i]["Id"].ToString() + "'>" + dt.Rows[i]["Title"].ToString() + "</a></li>";

}
}
}

/// <summary>
/// 最新問題
/// </summary>
public void QuestionNew()
{
try
{
Photo.InnerHtml = "<span><img alt='' src='../OpanelStyle/img/title_2.gif' /></span><div><a href='Default.aspx'>首頁</a> ><span>最新提問</span></div>";//導航圖片及文字顯示
Information.InnerHtml = "<ul><li><a href='DefaultTwo.aspx?operationType=5' class='hover'>最新提問</a></li><li><a href='DefaultTwo.aspx?operationType=6'>專家風采</a></li><li><a href='DefaultTwo.aspx?operationType=7'>網站公告</a></li></ul>";//信息中心高亮顯示
int i = 0;
string sql = "select top(15) Id,Question,Answer,CreateTime from QuestionInfo where Answer<>'' order by CreateTime desc";
DataTable dt = DBUtility.DbHelperSQL.Query(sql).Tables[0];
string Question = "";

if (dt.Rows.Count < 20)
{
for (i = 1; i < dt.Rows.Count + 1; i++)
{
if (dt.Rows[i - 1]["Question"].ToString().Length > 28)
{//當問題超過28個字時,多余字用"....."代替
Question = dt.Rows[i - 1]["Question"].ToString().Substring(0, 30) + "......";
}
else
{
Question = dt.Rows[i - 1]["Question"].ToString();
}
question.InnerHtml += "<li><a href='DefaultThird.aspx?operationType=5&Id=" + dt.Rows[i - 1]["Id"].ToString() + "'>" + i + "." + Question + "</a><span>" + dt.Rows[i - 1]["CreateTime"].ToString() + "</span></li>";
}
}
else
{
for (i = 1; i < 21; i++)
{
if (dt.Rows[i - 1]["Question"].ToString().Length > 28)
{//當問題超過28個字時,多余字用"....."代替
Question = dt.Rows[i - 1]["Question"].ToString().Substring(0, 30) + "......";
}
else
{
Question = dt.Rows[i - 1]["Question"].ToString();
}
question.InnerHtml += "<li><a href='DefaultThird.aspx?operationType=5&Id=" + dt.Rows[i - 1]["Id"].ToString() + "'>" + i + "." + Question + "</a><span>" + dt.Rows[i - 1]["CreateTime"].ToString() + "</span></li>";
}
}
}
catch
{

}
}

/// <summary>
/// 合作伙伴
/// </summary>
public void huobanP()
{

try
{
DataTable dt = DBUtility.DbHelperSQL.Query("select ComName,ComPage,Photo from FriendsLinks where IsActive='true'").Tables[0];
if (dt.Rows.Count < 5)
{
int k = dt.Rows.Count;
for (int g = 0; g < k; g++)
{
huoban.InnerHtml +=
"<li><a href='" + dt.Rows[g]["ComPage"].ToString() + "' target='_blank'><img src='" + dt.Rows[g]["Photo"].ToString() + "' alt='" + dt.Rows[g]["ComName"].ToString() + "' width ='160' height='68'/></a>";
}
while (k < 5)
{
huoban.InnerHtml +=
"<li><a href='http://www.baidu.com' target='_blank'><img src='App_Themes/images/nopic.gif' alt='在線答疑' width ='160' height='68'/></a>";
k++;
}
}



if (dt.Rows.Count == 5)
{
for (int i = 0; i < dt.Rows.Count; i++)
{

huoban.InnerHtml +=
"<li><a href='" + dt.Rows[i]["ComPage"].ToString() + "' target='_blank'><img src='" + dt.Rows[i]["Photo"].ToString() + "' alt='" + dt.Rows[i]["ComName"].ToString() + "' width ='160' height='68'/></a>";

}
}

}
catch
{


}
}

/// <summary>
/// 專家圖片
/// </summary>
public void PictureShow()
{
try
{
Photo.InnerHtml = "<span><img alt='' src='../OpanelStyle/img/title_1.gif' /></span><div><a href='Default.aspx'>首頁</a> ><span>專家風采</span></div>";//導航圖片及文字顯示
Information.InnerHtml = "<ul><li><a href='DefaultTwo.aspx?operationType=5'>最新提問</a></li><li><a href='DefaultTwo.aspx?operationType=6' class='hover'>專家風采</a></li><li><a href='DefaultTwo.aspx?operationType=7'>網站公告</a></li></ul>";//信息中心高亮顯示

int i = 0;
//string sql1 = "select UserName,RealName,Photo,Memo from TeacherUser where UserName in (select top 10 UserName from TeacherUser order by NewId()) order by UserName ASC";//頭像,用戶名查詢語句 排序需求 雙重嵌套 NEWID 是排序 不能和 ASC 一起
// string sql1 = "select UserName,RealName,Photo,SubjectId from V_TeacherSubject where UserName in (select top 10 UserName from TeacherUser order by NewId())";
string sql1 = "select top (10) UserName,RealName,Photo,SubjectId from V_TeacherSubject order by NewId()";
DataTable dt1 = DBUtility.DbHelperSQL.Query(sql1).Tables[0];
string sql3 = "select Id,Caption from D_Subject";
DataTable dt3 = DBUtility.DbHelperSQL.Query(sql3).Tables[0];

picture.InnerHtml = "";
string[] SubCollect = new string[10];
for (i = 0; i < 10; i++)
{
SubCollect[i] = "";
string[] Split = dt1.Rows[i]["SubjectId"].ToString().Split(new char[] { ',' });

for (int k = 0; k < dt3.Rows.Count; k++)
{
for (int j = 0; j < Split.Length; j++)
{
if (j == 1) { break; }
if (dt3.Rows[k]["Id"].ToString().Equals(Split[j]))
{
SubCollect[i] = SubCollect[i] + dt3.Rows[k]["Caption"].ToString() + ",";
}
}

}

Split = null;
SubCollect[i] = SubCollect[i].ToString().Substring(0, SubCollect[i].ToString().Length - 1);
picture.InnerHtml +=
"<li><a href='DefaultThird.aspx?operationType=6&UserName=" + dt1.Rows[i]["UserName"].ToString() + "' target='_blank'><img src='" + dt1.Rows[i]["photo"].ToString() + "' width='120' height='120'></a><p><a href='DefaultThird.aspx?operationType=6&UserName=" + dt1.Rows[i]["UserName"].ToString() + "' target='_blank'>" + dt1.Rows[i]["RealName"].ToString() + "</a></p><center><p>主 講:<span>" + SubCollect[i] + "</span></p></center></li>";
}
SubCollect = null;
}
catch
{

}
}
/// <summary>
/// 網站公告
/// </summary>
public void AllNotice()
{
try
{
Photo.InnerHtml = "<span><img alt='' src='../OpanelStyle/img/title_4.gif' /></span><div><a href='Default.aspx'>首頁</a> ><span>網站公告</span></div>";//導航圖片及文字顯示
Information.InnerHtml = "<ul><li><a href='DefaultTwo.aspx?operationType=5'>最新提問</a></li><li><a href='DefaultTwo.aspx?operationType=6'>專家風采</a></li><li><a href='DefaultTwo.aspx?operationType=7' class='hover'>網站公告</a></li></ul>";//信息中心高亮顯示
if (pageNum < 1) { pageNum = 1; }
if (all < 1)
{
all = Convert.ToInt32(DbHelperSQL.GetSingle("select count(1) from SiteNotice where 1=1", null));
}
int maxPage;
if (all % 10 == 0)//計算最大頁數
{
maxPage = (all / 10);
}
else
{
maxPage = (all / 10) + 1;
}

DataTable dt = DBUtility.DbHelperSQL.QueryByPage("SiteNotice", "Id,Title,CreateTime", "Id", "CreateTime", 10, pageNum, "asc", "1=1").Tables[0];//取出指定頁面的數據
if (dt.Rows.Count != 0)
{
for (int i = 1; i < dt.Rows.Count + 1; i++)
{
pager.InnerHtml += "<li><a href='DefaultThird.aspx?operationType=7&Id=" + dt.Rows[i - 1]["Id"].ToString() + "'>" + i + "." + dt.Rows[i - 1]["Title"].ToString() + "</a><span style='left'>" + dt.Rows[i - 1]["CreateTime"].ToString() + "</span></li>";
}

pageList(maxPage);
}

}
catch
{

}
}
/// <summary>
/// 網站公告分頁數據
/// </summary>
public void AllNotice(int i)//參數 第幾頁
{

try
{
Photo.InnerHtml = "<span><img alt='' src='../OpanelStyle/img/title_4.gif' /></span><div><a href='Default.aspx'>首頁</a> ><span>網站公告</span></div>";//導航圖片及文字顯示
Information.InnerHtml = "<ul><li><a href='DefaultTwo.aspx?operationType=5'>最新提問</a></li><li><a href='DefaultTwo.aspx?operationType=6'>專家風采</a></li><li><a href='DefaultTwo.aspx?operationType=7' class='hover'>網站公告</a></li></ul>";//信息中心高亮顯示
pageNum = i;
all = Convert.ToInt32(DbHelperSQL.GetSingle("select count(1) from SiteNotice where 1=1", null));
if (all != 0)
{
int maxPage;
if (all % 10 == 0)//計算最大頁數
{
maxPage = (all / 10);
}
else
{
maxPage = (all / 10) + 1;
}
if (pageNum < 1) { pageNum = 1; }
if (pageNum > maxPage) { pageNum = maxPage; }
DataTable dt = DBUtility.DbHelperSQL.QueryByPage("SiteNotice", "Id,Title,CreateTime", "Id", "CreateTime", 10, pageNum, "asc", "1=1").Tables[0];//取出指定頁面的數據
if (dt.Rows.Count != 0)
{
for (int s = 1; s < dt.Rows.Count + 1; s++)
{

pager.InnerHtml += "<li><a href='DefaultThird.aspx?operationType=7&Id=" + dt.Rows[s - 1]["Id"].ToString() + "'>" + s + "." + dt.Rows[s - 1]["Title"].ToString() + "</a><span style='left'>" + dt.Rows[s - 1]["CreateTime"].ToString() + "</span></li>";

}


pageList(maxPage);
}
}
else { Response.Write("<script type='javascript'>alert('沒有對應數據!');</script>"); };//輸出沒有相應數據

}
catch
{

}
}


/// <summary>
/// 退出系統
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkButton1_Click(object sender, EventArgs e)
{
System.Web.Security.FormsAuthentication.SignOut();//從瀏覽器刪除Forms身份驗證票據
Session.Abandon();
Response.Redirect("/Default.aspx");
//if (Session["usertype"].ToString() == "student")
//{
// DataTable dt = DBUtility.DbHelperSQL.Query("select * from StudentOnline where UserName='" + Session["username"] + "'").Tables[0];
// if (dt.Rows.Count > 0)
// {
// smodel = sbll.GetModel(int.Parse(dt.Rows[0]["Id"].ToString()));
// smodel.Online = false;
// smodel.ExitTime = DateTime.Now;
// smodel.UpdateTime = DateTime.Now;
// sbll.Update(smodel);
// }
//}
//if (Session["usertype"].ToString() == "teacher")
//{
// DataTable dt = DBUtility.DbHelperSQL.Query("select * from TeacherOnline where UserName='" + Session["username"] + "'").Tables[0];
// if (dt.Rows.Count > 0)
// {
// tmodel = tbll.GetModel(int.Parse(dt.Rows[0]["Id"].ToString()));
// tmodel.Online = false;
// tmodel.ExitTime = DateTime.Now;
// tmodel.UpdateTime = DateTime.Now;
// tbll.Update(tmodel);
// }
//}
}

/// <summary>
/// 分頁模塊
/// </summary>
/// <param name="maxPage"></param>
protected void pageList(int maxPage)
{
string s = string.Empty;
string first = "<font style='font-size:12px'>首頁</font>";
string previous = "<font style='font-size:12px'>上一頁</font>";
string next = "<font style='font-size:12px'>下一頁</font>";
string last = "<font style='font-size:12px'>尾頁</font>";
//判斷當前頁是否是第一頁,從而控制顯示方式
if (pageNum == 1)
{
s += (first + " " + previous + " <b>");
}
else
{
if (first != "")
s += ("<a href='DefaultTwo.aspx?operationType=11&page=1'>" + first + "</a> ");
s += ("<a href='DefaultTwo.aspx?operationType=11&page=" + (pageNum - 1) + "'>" + previous + "</a> <b>");
}
//判斷:當總頁數多於6頁時,省略中間頁碼,只顯示第一頁和最后一頁
int j = (pageNum - 5 < 1) ? 1 : pageNum - 5;
int k = (pageNum + 5 > maxPage) ? maxPage : pageNum + 5;

//顯示第一頁
if (j > 1)
s += "<a href='DefaultTwo.aspx?operationType=11&page=1'><font style='font-size:12px'>1</font></a> ... ";
//顯示中間頁碼
for (int i = j; i < k + 1; i++)
{
if (pageNum == i)
{
s += ("<span><font style='font-size:12px'>" + i + "</font></span> ");
continue;
}
s += ("<a href='DefaultTwo.aspx?operationType=11&page=" + i + "'><font style='font-size:12px'>" + i + "</font></a> ");
}
//顯示最后一頁
if (maxPage > k)
s += " ... <a href='DefaultTwo.aspx?operationType=11&page=" + maxPage + "'>" + maxPage + "</a> ";
//判斷當前頁是否是最后一頁,從而控制顯示方式
if (pageNum == maxPage)
{
s += ("</b>" + next + " " + last);
}
else
{
s +=
("</b><a href='DefaultTwo.aspx?operationType=11&page=" + (pageNum + 1) + "'>" + next + "</a> ");

if (last != "") s +=
("<a href='DefaultTwo.aspx?operationType=11&page=" + maxPage + "'>" + last + "</a>");
}
//根據輸入的頁號,直接顯示該頁數據
s += "<font style='font-size:12px'> 轉到</font> <input type='text'id='gotopage' name='gotopage' value='" + pageNum + "' size='2' /> <input type='button'class='button' value='Go' style='height:22px;' onclick='go();'/>";
//控制“總記錄”“每頁”“總頁數”的顯示

s += ("<span style='margin-left:320px'></span><font style='font-size:12px'>總記錄:</font><span style='margin-left:10px'><font style='font-size:12px'>" + all + "</font></span> ");
s += ("<font style='font-size:12px'>共</font><span style='margin-left:4px'><font style='font-size:12px'>" + maxPage + "</font></span><font style='font-size:12px'>頁</font></div>");

pageAll.InnerHtml = "<hr/><table width='100%' class='maintable' cellpadding='0' cellspacing='1' border='0' id='Table1'>'" + s + "'</table>";
}
}
}

四.所有輸出的數據后台 集成到 ACTION.ASPX.CS 這一個類中。然后用 ENUM的數據來進行規范。再通過switch 來選擇出對應的方法。

  protected void Page_Load(object sender, EventArgs e)
{
try
{
string type = Request["optype"].ToString();
switch (type.ToLower())
{
case "querystudent":
querystudent();
break;
case "checksubmit"://客觀題核分
checksubmit();
break;
case "checksubscore":
checksubscore();
}
}
}



/// <summary>
/// 科目查詢
/// </summary>
private void querycaption()
{
com.sdnu902.BLL.D_Subject bll = new com.sdnu902.BLL.D_Subject();
DataTable dt = bll.GetAllList().Tables[0];

StringBuilder sb = new StringBuilder();
sb.Append("<thead><tr>");
sb.Append("<th>科目</th><th>操作</th>");
sb.Append("</tr></thead><tbody>");

if (dt.Rows.Count <= 0)
{
sb.Append("<tr><td colspan='5'>無數據信息</td></tr>");
}
else
{
foreach (DataRow row in dt.Rows)
{
sb.Append("<tr id='" + row["id"].ToString() + "'>");
sb.Append("<td>" + row["caption"].ToString() + "</td>");
sb.Append("<td><a href='javascript:modify(\"" + row["ID"].ToString() + "\")'>修改</a>|<a href='javascript:del(\"" + row["ID"].ToString() + "\")'>刪除</a></td>");
sb.Append("</tr>");
}
}

sb.Append("</tbody>");

showHTML(sb.ToString());
}


/// <summary>
/// 刪除科目
/// </summary>
private void delcaption()
{
string id = Request["id"];
com.sdnu902.BLL.D_Subject bll = new com.sdnu902.BLL.D_Subject();
try
{
bll.Delete(new Guid(id));
showMsg("刪除成功");
}
catch
{
showMsg("刪除失敗");
}
}
/// <summary>
/// 修改科目
/// </summary>


private void getCaption()
{
string sid = Request["id"];
if (!String.IsNullOrEmpty(sid))
{
com.sdnu902.BLL.D_Subject bll = new com.sdnu902.BLL.D_Subject();
com.sdnu902.Model.D_Subject model = new com.sdnu902.Model.D_Subject();

model = bll.GetModel(Guid.Parse(sid));
string json = com.sdnu902.Common.JsonHelper.ObjectToJson(model).ToLower();
showJson(json);
}
}


/// <summary>
/// 保存科目(添加 和 修改)
/// </summary>

private void savecaption()
{
com.sdnu902.BLL.D_Subject bll = new com.sdnu902.BLL.D_Subject();
com.sdnu902.Model.D_Subject model = new com.sdnu902.Model.D_Subject();

string id = Request["id"];

model.caption = Request["addCaption"];
try
{
if (string.IsNullOrEmpty(id))//add
{
model.id = System.Guid.NewGuid();
bll.Add(model);
showMsg("添加成功");
}
else//modify
{
model.id = Guid.Parse(id);
bll.Update(model);
showMsg("修改成功");
}
}
catch
{
showMsg("保存失敗");
}
}

  

其調用的一些輸出HTML 和 JSON的方法

#region functions

/// <summary>
/// 向前台返回html格式信息
/// </summary>
/// <param name="msg">要顯示的信息</param>
private void showHTML(string html)
{
Response.ContentType = "text/html";

Response.Write(html);
Response.Flush();
Response.Close();
}

/// <summary>
/// 向前台返回json格式的提示信息
/// </summary>
/// <param name="msg">要提示的信息</param>
private void showMsg(string msg)
{
msg = msg.Replace("\n", "\\n");//避免回車換行等特殊字符對json格式的影響
msg = msg.Replace("\r", "\\r");

string st = "{\"result\":\"" + msg + "\"}";
Response.ContentType = "application/json";

Response.Write(st);
Response.Flush();
Response.Close();
}

/// <summary>
/// 向前台輸出json數據
/// </summary>
/// <param name="msg">定義好的json格式數據</param>
private void showJson(string msg)
{
msg = msg.Replace("\n", "\\n");
msg = msg.Replace("\r", "\\r");

Response.ContentType = "application/json";

Response.Write(msg);
Response.Flush();
Response.Close();
}

#endregion

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com