1. 程式人生 > >asp.netajax與jquery和bootstrap的無刷新完美實現

asp.netajax與jquery和bootstrap的無刷新完美實現

white sleep 火狐 scale 入學 scrip style 進行 itl

20190421asp.netajaxjquerybootstrap的無刷新完美實現

設計代碼和後臺代碼中重要部分加粗和深色以及字號加大。

設計前臺代碼:

<%@ Page Title="添加/修改商家" Language="C#" MasterPageFile="~/Site_cg007.Master" AutoEventWireup="true"

CodeBehind="web_cg007.aspx.cs" Inherits="jfydxfjz.web_cg007" EnableEventValidation="false" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>開發者</title>

<script type="text/javascript" src="scripts/bootstrap_v30/jquery/jquery-

1.8.3.min.js"></script>

<script type="text/javascript" src="scripts/layer/layer.js"></script>

<script type="text/javascript" src="scripts/bootstrap_v30/bootstrap/js/bootstrap.min.js"></script>

<link href="StyleSheet_dn.css" rel="stylesheet" type="text/css" />

<link href="scripts/bootstrap_v30/bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css" />

<style type="text/css">

body{color:#686868;}

@media all and (max-width:768px){

p{font-size:12px;}

h2{font-size:16px;}

h3{font-size:14px;}

li{font-size:14px;margin-left:30px;}

.myimg{width:50%;}

}

/*模態窗口取消背景變暗的效果

.modal-backdrop-zrp

{

opacity: 0 !important;

filter: alpha(opacity=0) !important;

}

*/

</style>

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server">

</asp:ScriptManager>

<div class="container">

<div class="row">

<div class="col-md-4 col-xl-12 text-center">

<asp:UpdateProgress ID="UpdateProgress_gly" runat="server" AssociatedUpdatePanelID="UpdatePanel_sjgl">

<ProgressTemplate><asp:Image ID="Image_gly"

CssClass="myimg" runat="server" ImageUrl="~/images/progress.gif" /></ProgressTemplate>

</asp:UpdateProgress>

</div>

</div>

</div>

<asp:UpdatePanel ID="UpdatePanel_sjgl" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">

<ContentTemplate>

<div class="container">

<center>

<table class="nav-justified">

<tr>

<td style="text-align:left;">

<input type="text" runat="server" id="tbx_kfz_sjqq" placeholder="商家名稱或QQ" />

</td>

<td>

<asp:Button ID="btn_kfz_cxsjqq" runat="server" Text="查詢" CssClass="btn_dn" OnClick="btn_kfz_cxsjqq_Click" />

</td>

<td>

<button class="btn-primary" data-toggle="modal" data-target="#tjsjmodal" id="btn_html_tj">添加</button>

</td>

<td>

<asp:Button ID="btn_kfz_tjgly" runat="server" Text="添加管理員" CssClass="btn_dn" OnClick="btn_kfz_tjgly_Click" />

</td>

</tr>

<tr style="text-align:center">

<td colspan="4" style="padding-top:5px;text-align:center">

<!--采用自帶的分頁功能-->

<center>

<asp:GridView ID="GridView_sjgl" runat="server" Width="100%" AllowPaging="True" AutoGenerateColumns="False" CssClass="grid_dn" DataKeyNames="ID" HorizontalAlign="Center" OnRowCommand="GridView_sjgl_RowCommand" OnRowDataBound="GridView_sjgl_RowDataBound" OnPageIndexChanging="GridView_sjgl_PageIndexChanging">

<Columns>

<asp:BoundField DataField="ID" HeaderText="編號">

<HeaderStyle CssClass="header_dn" />

<ItemStyle HorizontalAlign="Left" />

</asp:BoundField>

<asp:BoundField DataField="Dwmc" HeaderText="商家">

<HeaderStyle CssClass="header_dn" />

<ItemStyle HorizontalAlign="Left" />

</asp:BoundField>

<asp:BoundField DataField="sqbh" HeaderText="授權令">

<HeaderStyle CssClass="header_dn" />

<ItemStyle HorizontalAlign="Left" />

</asp:BoundField>

<asp:TemplateField HeaderText="試用">

<HeaderStyle CssClass="header_dn" />

<ItemTemplate>

<asp:Button ID="btn_jk_sy" runat="server" Text="試用" CssClass="btn"

CommandArgument=‘<%# Eval("ID") %>‘ CommandName="cmn_jz_sy" />

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="註冊">

<HeaderStyle CssClass="header_dn" />

<ItemTemplate>

<asp:Button ID="btn_jk_zc" runat="server" Text="註冊" CssClass="btn"

CommandArgument=‘<%# Eval("ID") %>‘ CommandName="cmn_jz_zc" />

</ItemTemplate>

</asp:TemplateField>

</Columns>

<RowStyle CssClass="row_dn" />

<HeaderStyle CssClass="header_dn" />

<PagerSettings FirstPageText="首頁" LastPageText="尾頁" Mode="NextPreviousFirstLast" NextPageText="下頁" PreviousPageText="上頁" />

<PagerStyle HorizontalAlign="Center" />

<SelectedRowStyle CssClass="selectrow_dn" />

</asp:GridView>

</center>

</td>

</tr>

</table>

</center>

</div>

<!--添加商家彈出窗口:#tjsjmodal-->

<div id="tjsjmodal" class="modal fade bs-example-modal-sm" data-backdrop="static">

<div class="modal-dialog modal-sm">

<div class="modal-content">

<div class="modal-header">

<button class="close" data-dismiss="modal">×</button>

<h4 class="modal-title">添加商家</h4>

</div>

<div class="modal-body">

<div class="form-group">

商家名稱

<input type="text" runat="server" id="tbx_sjmc" placeholder="輸入商家名稱" />

</div>

<div class="form-group">

授權口令

<input type="text" runat="server" id="tbx_sqkl" placeholder="輸入或者空白" />

</div>

<div class="form-group">

QQ

<input type="text" runat="server" id="tbx_lxqq" placeholder="輸入QQ號" />

</div>

<div class="form-group">

<asp:Label ID="lbl_ts" runat="server" ForeColor="Red" Font-Size="Large" />

</div>

</div>

<div class="modal-footer">

<button class="btn btn-default" data-dismiss="modal">關閉</button>

<asp:Button ID="btn_gb" CssClass="btn" Text="關閉" runat="server" OnClick="btn_gb_Click" Visible="false" />

<asp:Button ID="btn_tjsj" CssClass="btn" Text="添加" runat="server" OnClick="btn_tjsj_Click" />

</div>

</div>

</div>

</div>

</ContentTemplate>

<Triggers>

</Triggers>

</asp:UpdatePanel>

</asp:Content>

後臺代碼:

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 System.Data.SqlClient;

namespace jfydxfjz

{

public partial class web_cg007 : System.Web.UI.Page

{

private jf_cyclxfjzEntities sjklj = new jf_cyclxfjzEntities();//數據庫連接

private webncxk_class gj = new webncxk_class();//加解密工具

protected void Page_Load(object sender, EventArgs e)

{

//加入用戶登錄認證判斷---處理中

cy_kfz db_kfz = Session["E1DAD727864"] as cy_kfz;

if (db_kfz == null) //不是開發者

{

this.Response.Redirect("~/Default.aspx");

}

if (!Page.IsPostBack) //數據控件綁定數據,必須放入這裏,不然每次對數據控件的操作都無效

{

this.GridView_sjgl.DataSource = sjklj.cy_sqdw.OrderByDescending(aa => aa.ID);

this.GridView_sjgl.DataBind();

}

}

protected void GridView_sjgl_RowCommand(object sender, GridViewCommandEventArgs e)

{

try

{

if (e.CommandName == "cmn_jz_sy") //單擊了試用按鈕

{

int id = int.Parse(e.CommandArgument.ToString());

//定位記錄

var cxsy = from aa in sjklj.cy_sqdw

where aa.ID == id

select aa;

//授權口令生成器

//生成隨機試用口令

Random sjs = new Random(DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute +

DateTime.Now.Second + DateTime.Now.Millisecond + 20316);

int scsjs = sjs.Next(10000, 99999);

string sqkl = "a" + scsjs.ToString();//試用口令

//沒有加密的口令:

string ywkl=sqkl;

sqkl = webncxk_class.Encrypt(sqkl, webncxk_class.salt, webncxk_class.jmmy);//加密,自動生成的

if (string.IsNullOrWhiteSpace(cxsy.First().sqbh))//沒有加入試用口令,正式使用默認30年

{

cxsy.First().sqbh = sqkl;

sjklj.SaveChanges();

//重新綁定

(e.CommandSource as Button).Text = ywkl;

this.UpdatePanel_sjgl.Update();//有條件更新,而且子控件

// aspnetajaxjavascript.layermsg(this.UpdatePanel_sjgl, "顧客異步刷新");

return;

}

else

{

//對口令進行解密

string kl = cxsy.First().sqbh.Trim();

kl = gj.Decrypt(kl, webncxk_class.salt, webncxk_class.jmmy);

if (kl.Contains("a"))

{

this.UpdatePanel_sjgl.Update();//有條件更新,而且子控件

aspnetajaxjavascript.layermsg(this.UpdatePanel_sjgl, "已經是試用了");

return;

}

else

{

cxsy.First().sqbh = sqkl;

sjklj.SaveChanges();

//重新綁定

(e.CommandSource as Button).Text = ywkl;

this.UpdatePanel_sjgl.Update();//有條件更新,而且子控件

aspnetajaxjavascript.layermsg(this.UpdatePanel_sjgl, "加入試用成功!");

return;

}

}

}

//註冊

if (e.CommandName == "cmn_jz_zc") //單擊了註冊

{

int id = int.Parse(e.CommandArgument.ToString());

//定位記錄

var cxsy = from aa in sjklj.cy_sqdw

where aa.ID == id

select aa;

//授權口令生成器

//生成隨機正式口令

Random sjs = new Random(DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute +

DateTime.Now.Second + DateTime.Now.Millisecond + 20316);

int scsjs = sjs.Next(10000, 99999);

string sqkl = "b" + scsjs.ToString();//註冊口令

//沒有加密的口令:

string ywkl = sqkl;

sqkl = webncxk_class.Encrypt(sqkl, webncxk_class.salt, webncxk_class.jmmy);//加密,自動生成的

if (string.IsNullOrWhiteSpace(cxsy.First().sqbh))//沒有加入正式口令,正式使用默認30年

{

cxsy.First().sqbh = sqkl;

sjklj.SaveChanges();

//重新綁定

(e.CommandSource as Button).Text = ywkl;

this.UpdatePanel_sjgl.Update();//有條件更新,而且子控件

// aspnetajaxjavascript.layermsg(this.UpdatePanel_sjgl, "顧客異步刷新");

return;

}

//對口令進行解密

string kl = cxsy.First().sqbh.Trim();

kl = gj.Decrypt(kl, webncxk_class.salt, webncxk_class.jmmy);

if (kl.Contains("b"))

{

this.UpdatePanel_sjgl.Update();//有條件更新,而且子控件

aspnetajaxjavascript.layermsg(this.UpdatePanel_sjgl, "已經註冊成功!");

return;

}

else

{

cxsy.First().sqbh = sqkl;

cxsy.First().yxq = DateTime.Now.Year + 30;

sjklj.SaveChanges();

//重新綁定

(e.CommandSource as Button).Text = ywkl;

this.UpdatePanel_sjgl.Update();//有條件更新,而且子控件

// aspnetajaxjavascript.layermsg(this.UpdatePanel_sjgl, "顧客異步刷新");

return;

}

}

}

catch (Exception err)

{

this.UpdatePanel_sjgl.Update();//有條件更新,而且子控件

aspnetajaxjavascript.layermsg(this.UpdatePanel_sjgl, "錯誤:"+err.Message);

}

}

protected void btn_kfz_cxsjqq_Click(object sender, EventArgs e)

{

// this.UpdatePanel_sjgl.Update();

// aspnetajaxjavascript.layermsg(this.UpdatePanel_sjgl, "測試查詢!");

try

{

string cxtj = this.tbx_kfz_sjqq.Value.Trim();

if (cxtj == string.Empty)

{

this.UpdatePanel_sjgl.Update();//有條件更新,而且子控件

aspnetajaxjavascript.layermsg(this.UpdatePanel_sjgl, "請輸入學校名稱或者QQ號!");

return;

}

var cxxx = (from aa in sjklj.cy_sqdw

where aa.Dwmc.Contains(cxtj) || aa.qq.Contains(cxtj)

select aa).OrderByDescending(bb => bb.ID);

this.GridView_sjgl.DataSource = cxxx;

this.GridView_sjgl.DataBind();

this.UpdatePanel_sjgl.Update();//有條件更新,而且子控件

}

catch

{

}

}

protected void GridView_sjgl_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

try

{

int id = int.Parse(this.GridView_sjgl.DataKeys[e.Row.RowIndex].Value.ToString());//獲取主鍵

//獲取加密的授權口令

string sqkl = sjklj.cy_sqdw.Where(aa => aa.ID == id).First().sqbh;

sqkl = gj.Decrypt(sqkl, webncxk_class.salt, webncxk_class.jmmy);//解密

// this.Gv_xx.Rows[e.Row.RowIndex].Cells[2].Text = sqkl;//出錯,索引找不到

e.Row.Cells[2].Text = sqkl;

}

catch

{

}

}

}

protected void GridView_sjgl_PageIndexChanging(object sender, GridViewPageEventArgs e)

{

this.GridView_sjgl.PageIndex = e.NewPageIndex;

this.GridView_sjgl.DataSource = sjklj.cy_sqdw.OrderByDescending(aa => aa.ID);

this.GridView_sjgl.DataBind();

//刷新

this.UpdatePanel_sjgl.Update();

}

protected void btn_kfz_tjgly_Click(object sender, EventArgs e)

{

try

{

//123加密為初始密碼

string jbmm = webncxk_class.Encrypt("123", webncxk_class.salt, webncxk_class.jmmy);

//查看管理員是否還沒有添加,查找在管理員表中沒有的xk_sqdw記錄

var cxmygly = from aa in sjklj.cy_sqdw

where !sjklj.admin_user.Any(bb => bb.dwid == aa.ID)

select aa;//這個方法很不錯

Random sjs = new Random(DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute +

DateTime.Now.Second + DateTime.Now.Millisecond + 20316);

foreach (var kk in cxmygly)

{

int scsjs = sjs.Next(1000, 9999);

admin_user tj_gly = new admin_user

{

dwid = kk.ID,

Yhm =kk.ID.ToString()+"a"+scsjs.ToString(),//按照a分割出dwid

Mm = jbmm

};

sjklj.admin_user.AddObject(tj_gly);

}

sjklj.SaveChanges();

//layeriujq.Msg("管理員添加完成", "6", "3000", this.Page);

this.UpdatePanel_sjgl.Update();//有條件更新,而且子控件

aspnetajaxjavascript.layermsg(this.UpdatePanel_sjgl, "管理員添加完成!");

return;

}

catch

{

}

}

protected void btn_tjsj_Click(object sender, EventArgs e)

{

try

{

//只需要商家名稱和QQ號

string sjmc = this.tbx_sjmc.Value.Trim();

string qq = this.tbx_lxqq.Value.Trim();

string sqxx = this.tbx_sqkl.Value.Trim();

//生成隨機試用口令

Random sjs = new Random(DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute +

DateTime.Now.Second + DateTime.Now.Millisecond + 20316);

int scsjs = sjs.Next(10000, 99999);

string sqkl = "a" + scsjs.ToString();//試用口令

sqkl = webncxk_class.Encrypt(sqkl, webncxk_class.salt, webncxk_class.jmmy);//加密,自動生成的

string sdsqkl = "";//人工生成的授權口令

if (sqxx != string.Empty)

{

sdsqkl = webncxk_class.Encrypt(sqxx, webncxk_class.salt, webncxk_class.jmmy);

}

if (sjmc == string.Empty || qq == string.Empty)

{

//layeriujq.Msg("學校名稱或QQ不能為空", "2", "3000", this.Page);

// this.lbl_tj_xx_ts.Text = "商家名稱或QQ不能為空";

//有條件更新,而且子控件

this.UpdatePanel_sjgl.Update();

// aspnetajaxjavascript.layermsg(this.UpdatePanel_sjgl, "商家名稱或QQ不能為空"); //兩個彈窗,前者會消失

//aspnetajaxjavascript.Msg(this.UpdatePanel_sjgl, "商家名稱或QQ不能為空", "2", "2000");//bootstrap模態與layer沖突

this.lbl_ts.Text = "商家名稱或QQ不能為空";

ScriptManager.RegisterStartupScript(this.UpdatePanel_sjgl, this.UpdatePanel_sjgl.GetType(),

"updateScript", "$(‘.modal‘).modal();",true);//改為false,則不返回提示結果

//使用火狐瀏覽器完美實現,QQ瀏覽器也不錯,谷歌瀏覽器的低版本則不能很好實現(高版本可以),需要重慶打開該頁面(重定向)才可以

// System.Threading.Thread.Sleep(200);

//ScriptManager.RegisterStartupScript(this.UpdatePanel_sjgl, this.UpdatePanel_sjgl.GetType(),

//"updateScript", "$(‘#btn_html_tj‘).click();", true);//模態窗口消失後,200毫秒再自動點擊

//ScriptManager.RegisterStartupScript(this.UpdatePanel_sjgl, this.UpdatePanel_sjgl.GetType(),

// "updateScript", "$(‘#tjsjmodal‘).modal(‘show‘);", true);

// aspnetajaxjavascript.Msg(this.UpdatePanel_sjgl, "商家名稱或QQ不能為空", "2", "50");//bootstrap模態與layer沖突

// ScriptManager.RegisterStartupScript(this.UpdatePanel_sjgl, this.UpdatePanel_sjgl.GetType(),

// "updateScript", "$(‘#btn_html_tj‘).click();", true);//模態窗口消失後,200毫秒再自動點擊

return;

}

//查找學校名稱是否唯一

var cxxx = from aa in sjklj.cy_sqdw

where aa.Dwmc ==sjmc

select aa;

if (cxxx.Count() > 0)

{

//layeriujq.Msg("該學校名稱已經存在", "2", "3000", this.Page);//另一個頁面調用該提示無效,只能當前頁面

// this.lbl_tj_xx_ts.Text = "該學校名稱已經存在";

this.UpdatePanel_sjgl.Update();//有條件更新,而且子控件

aspnetajaxjavascript.layermsg(this.UpdatePanel_sjgl, "該商家名稱已經存在");

return;

}

else //添加

{

if (sqxx == string.Empty) //自動生成

{

cy_sqdw tj_sqdw = new cy_sqdw

{

Dwmc = sjmc,

qq = qq,

sqbh = sqkl

};

sjklj.cy_sqdw.AddObject(tj_sqdw);

sjklj.SaveChanges();

// this.lbl_tj_xx_ts.Text = "添加試用學校成功!";

this.UpdatePanel_sjgl.Update();//有條件更新,而且子控件

aspnetajaxjavascript.layermsg(this.UpdatePanel_sjgl, "添加試用商家成功!");

}

else

{

cy_sqdw tj_sqdw = new cy_sqdw

{

Dwmc = sjmc,

qq = qq,

sqbh = sdsqkl

};

sjklj.cy_sqdw.AddObject(tj_sqdw);

sjklj.SaveChanges();

// this.lbl_tj_xx_ts.Text = "添加試用學校成功!";

this.UpdatePanel_sjgl.Update();//有條件更新,而且子控件

aspnetajaxjavascript.layermsg(this.UpdatePanel_sjgl, "添加試用商家成功!");

}

}

}

catch(Exception err)

{

this.UpdatePanel_sjgl.Update();//有條件更新,而且子控件

aspnetajaxjavascript.layermsg(this.UpdatePanel_sjgl, "錯誤:"+err.Message);

}

}

protected void btn_gb_Click(object sender, EventArgs e)

{

// this.UpdatePanel_sjgl.Update();//有條件更新,而且子控件

// ScriptManager.RegisterStartupScript(this.UpdatePanel_sjgl, this.UpdatePanel_sjgl.GetType(),

// "updateScript", "$(‘#tjsjmodal‘).modal(‘hide‘);", true);

// this.Page_Load(null, null);//野蠻刷新,最下策

this.Response.Redirect("~/web_cg007.aspx");//重定向打開本頁面

}

}

}

asp.netajax與jquery和bootstrap的無刷新完美實現