asp.net 利用微軟資料訪問類庫結合AjaxPro實現無重新整理下拉框級聯
1、新建一個web網站,新增如下幾個dll引用。具體的dll檔案可在如下地址下載:http://download.csdn.net/detail/taomanman/4167764
2、新建一個js資料夾,放jquery-1.4.1-vsdoc.js和jquery-1.4.2.min.js兩個js檔案。點選下載:http://download.csdn.net/detail/taomanman/4167768
3、配置Web.config檔案,新增資料庫連線字串及AjaxPro的配置,如下程式碼:
<?xml version="1.0" encoding="utf-8"?>
<!--
有關如何配置 ASP.NET 應用程式的詳細訊息,請訪問
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<!--新增資料庫連線字串開始-->
<connectionStrings>
<add name="MyConnectionString"
connectionString="Data Source=192.168.2.105;Initial Catalog=資料庫名;User ID=sa;Password=sa" providerName="System.Data.SqlClient" />
</connectionStrings>
<!--新增資料庫連線字串結束-->
<system.web>
<compilation debug="true" targetFramework="4.0" />
<!--新增ajaxpro開始-->
<httpHandlers>
<add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
</httpHandlers>
<!--新增ajaxpro結束-->
</system.web>
</configuration>
4、在Default.aspx頁面中新增2個html控制元件select下拉框,新增js檔案,這裡主要是利用AjaxPro實現aspx頁面訪問aspx.cs中的方法,主要程式碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AjaxDemo.Default" %>
<!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>ajax方式無重新整理級聯下拉</title>
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
//頁面載入時執行函式
$(function () {
DDL_Init();
});
//載入下拉框
function loadSelect(jsonObj) {
if (jsonObj == "") {
return '<option title="-1" selected="selected" value="請選擇">請選擇</option>';
}
jsonObj = eval("(" + jsonObj + ")");
var jsonStr = "";
$(jsonObj).each(function (key, value) {
//[{"id": "2011","name": "2011年"}]
//[{ "id": "2", "name": "第2期" }, { "id": "1", "name": "第1期"}]
jsonStr += '<option value="' + this.id + '" title="' + (key - 1) + '">' + this.name + '</option>';
});
return jsonStr;
}
//下拉框恢復
function resetSelect(selectStr) {
return '<option title="-1" selected="selected">' + selectStr + '</option>';
}
$("#YearID").change(function () {
var yearValue = $(this).attr("title").val();
var issueStr;
if (yearValue == "-1") {
issueStr += loadSelect(getIssueNumList(yearValue));
}
$("#Year_IssumNum").html(issueStr);
});
//獲得年份
function getYearList() {
//前臺獲取後臺方法,獲得值
var obj = AjaxDemo.Default.getYearList();
return obj.value;
}
//獲得某個年份下的期
function getIssueNumList(year) {
var obj = AjaxDemo.Default.getIssueNumList(year + "");
return obj.value;
}
//初始化下拉控制元件
function DDL_Init() {
var year = loadSelect(getYearList());
//繫結年份
$("#YearID").append(year);
//繫結期號
var issueNum = loadSelect(getIssueNumList($("#YearID option:first").val()));
$("#Year_IssumNum").append(issueNum);
}
</script>
</head>
<body>
<form id="form1" runat="server">
年份:
<select name="" class="year" id="YearID">
</select>
期號:
<select name="" class="issueNum " id="Year_IssumNum">
</select>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxPro;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using System.Data;
namespace AjaxDemo
{
public partial class Default : System.Web.UI.Page
{
SqlDatabase _db = new SqlDatabase(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
}
AjaxPro.Utility.RegisterTypeForAjax(typeof(Default));
}
[AjaxMethod()]
public string getYearList()
{
string strSQL = "select distinct t.[year] from SeedlingCommentCountyInfo t order by year desc";
DbCommand dbcmd = this._db.GetSqlStringCommand(strSQL);
DataSet ds = _db.ExecuteDataSet(dbcmd);
string dateStr = "[";
if (ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow drDate in ds.Tables[0].Rows)
{
dateStr += "{\"id\": \"" + drDate["year"].ToString() + "\",\"name\": \"" + drDate["year"].ToString() + "年\"},";
}
dateStr = dateStr.TrimEnd(',') + "]";
}
else
{
dateStr = "";
}
return dateStr;
}
[AjaxMethod()]
public string getIssueNumList(string year)
{
string strSQL = "select distinct t.issueNum from SeedlingCommentCountyInfo t where year=" + year + " order by issueNum desc";
DbCommand dbcmd = this._db.GetSqlStringCommand(strSQL);
DataSet ds = _db.ExecuteDataSet(dbcmd);
string issueNumStr = "[";
if (ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow drIssueNum in ds.Tables[0].Rows)
{
issueNumStr += "{\"id\": \"" + drIssueNum["issueNum"].ToString() + "\",\"name\": \"第" + drIssueNum["issueNum"].ToString() + "期\"},";
}
issueNumStr = issueNumStr.TrimEnd(',') + "]";
}
else
{
issueNumStr = "";
}
return issueNumStr;
}
}
}
相關文章
- 泛微e-office結合雲解析實現快速訪問
- 利用內網穿透 實現外網訪問內網 MySQL等資料庫教程內網穿透MySql資料庫
- Holer實現外網訪問SQLServer資料庫SQLServer資料庫
- Holer實現MongoDB資料庫外網訪問MongoDB資料庫
- Holer實現外網訪問MariaDB資料庫資料庫
- Holer實現外網訪問PostgreSQL資料庫SQL資料庫
- C# MVC LayUI實現下拉框二級聯動C#MVCUI
- 使用cglib實現資料庫框架的級聯查詢CGLib資料庫框架
- derby 資料庫 伺服器模式 無法訪問資料庫伺服器模式
- JDBC資料庫訪問JDBC資料庫
- 資料庫升級-物理重新整理資料字典資料庫
- 帝國cms無法重新整理資料 帝國cms無法重新整理資料庫資料庫
- ASP.NET Core6.0-wwwroot資料夾無法訪問解決方法ASP.NET
- zblog域名後無法訪問 zblog資料庫地址修改資料庫
- 【學習筆記】-結合JQuery和Ajax實現區域性資料重新整理筆記jQuery
- 使用R2DBC實現資料庫的響應式訪問資料庫
- springboot結合Redis實現工具類Spring BootRedis
- 微軟研究院聯合華盛頓大學首次實現全自動DNA資料儲存微軟
- 在Rainbond中實現資料庫結構自動化升級AI資料庫
- mysql 無限級分類實現思路MySql
- Spring Boot實現資料訪問計數器Spring Boot
- 實現無感重新整理Token技術:.Net Web API與axios的完美結合WebAPIiOS
- 一文讀懂圖資料庫 Nebula Graph 訪問控制實現原理資料庫
- 外網訪問MySQL資料庫MySql資料庫
- Oracle資料庫限制訪問IPOracle資料庫
- Highcharts結合Asp.net實現動態資料股票式圖形顯示例項 .【轉】ASP.NET
- 微軟改變資料訪問策略 OLE DB再次轉向ODBCOS微軟
- 利用python實現mysql資料庫向sqlserver的同步PythonMySql資料庫Server
- spring boot 結合Redis 實現工具類Spring BootRedis
- 重置資料庫密碼後導致網站無法訪問資料庫密碼網站
- 使用 Jwt-Auth 實現 API 使用者認證以及無痛重新整理訪問令牌JWTAPI
- 【SpringBoot實戰】資料訪問Spring Boot
- Oracle資料庫連結(DBLink)中如何訪問包含BLOB欄位的資料Oracle資料庫
- IIS無法訪問動態連結庫DLL的原因
- 皕傑報表實現下拉框聯動
- 使用 @NoRepositoryBean 簡化資料庫訪問Bean資料庫
- 如何限制ip訪問Oracle資料庫Oracle資料庫
- jmeter 使用 ssh 方式訪問資料庫JMeter資料庫
- 訪問資料庫出現Object reference not set to an instance of an object錯誤資料庫Object