Ajax实现无刷新三联动下拉框
1.html代码
Ajax实现无刷新三联动下拉框
<form id="Form1" method="post" runat="server">
form>
2.cs代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.
Web;
using System.
Web.SessionState;
using System.
Web.UI;
using System.
Web.UI.
WebControls;
using System.
Web.UI.HtmlControls;
namespace AjaxTest
{
/**////
/// Summary description for WebForm1.
///
public class
WebForm1 : System.
Web.UI.Page
{
protected System.
Web.UI.
WebControls.DropDownList DropDownList1;
protected System.
Web.UI.
WebControls.DropDownList DropDownList2;
protected System.
Web.UI.
WebControls.TextBox TextBox1;
protected System.
Web.UI.
WebControls.DropDownList DropDownList3;
private void Page_Load(object sender, System.EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(AjaxMethod));
if(!Page.IsPostBack)
{
this.DropDownList1.DataSource=AjaxMethod.GetProvinceList();
this.DropDownList1.DataTextField="province";
this.DropDownList1.DataValueField="provinceID";
this.DropDownList1.DataBind();
this.DropDownList1.Attributes.Add("onclick","cityResult();");
this.DropDownList2.Attributes.Add("onclick","areaResult();");
}
}
Web Form Designer generated code#region
Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
//
CODEGEN: This call is required by the ASP
.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/**////
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}3.AjaxMethod
using System;
using System.Data;
using System.Data.SqlClient;
namespace AjaxTest
{
/**////
/// Summary description for AjaxMethod.
///
public class AjaxMethod
{
GetProvinceList#region GetProvinceList
public static DataSet GetProvinceList()
{
string sql="select * from province";
return GetDataSet(sql);
}
#endregion
GetCityList#region GetCityList
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public DataSet GetCityList(int provinceid)
{
string sql="select * from city where father="+provinceid;
return GetDataSet(sql);
}
#endregion
GetAreaList#region GetAreaList
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public DataSet GetAreaList(int cityid)
{
string sql="select * from area where father="+cityid;
return GetDataSet(sql);
}
#endregion
GetDataSet#region GetDataSet
public static DataSet GetDataSet(string sql)
{
string ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlDataAdapter sda =new SqlDataAdapter(sql,ConnectionString);
DataSet ds=new DataSet();
sda.Fill(ds);
return ds;
}
#endregion
}
}4.
web.config
每每听到客户抱怨自己修改和很久的数据由于自己的一个误操作而丢失时,总想让web页面也有像window窗体一样具有提示保存的功能。所以,经过一段时间的研究以后终于实现了这个功能!============ checksave.js =======================<script language="JScript">function window::onbeforeunload(){if (typeof(document.all[‘txtCheckFlag‘]) != "undefined") // to detect whether this page need prompt the save message!{ if (event.clientY<0 && event.clientX>document.body.clientWidth-20 || event.clientY<0 && event.clientX<20 || event.altKey || event.clientY>document.body.clientHeight) event.returnValue="If you have modified some data, you need push the save button to save them. Do you want to save?";}}forms(0).submit(); return false; }else // If does not need { window.location=arg; return true; }}function showsavemessage(arg){if (window.confirm("Save Change?")) { document.all["txtSave"].value="1"; document.forms(0).submit(); return false; }else { if (arg == ".") { return true; } else { window.location=arg; return false; } }}//-->Codebehind="test.aspx.vb" Inherits="Test.test" smartNavigation="True" %> test .NET 7.0" name="GENERATOR"> CODE_LANGUAGE"> vs_defaultClientScript"> vs_targetSchema"> <form id="Form1" method="post" runat="server"> sohu yahoo MTR LinkButton form> ======== test.aspx.vb ===============Public Class test Inherits System.
Web.UI.Page Protected WithEvents txtSave As System.
Web.UI.
WebControls.TextBox Protected WithEvents LinkButton1 As System.
Web.UI.
WebControls.LinkButton Protected WithEvents Button1 As System.
Web.UI.
WebControls.Button Protected WithEvents txtCheckFlag As System.
Web.UI.
WebControls.TextBox#Region "
Web Form Designer Generated Code " ‘This call is required by the
Web Form Designer.
Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init ‘CODEGEN: This method call is required by the Web Form Designer ‘Do not modify it using the code editor. InitializeComponent() End Sub#End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ‘Put user code to initialize the page here txtSave.Style.Item("display") = "none" txtCheckFlag.Style.Item("display") = "none" LinkButton1.Attributes("onclick") = "javascript:return checksave(this.href)" ‘ Page.RegisterStartupScript("Prompt", "<script language=""javascript"">function pageload() { } 0 Then Page.RegisterStartupScript("warning", "<script language=""javascript"">function pageload() {if (showsavemessage(‘.‘)) " + Session("URL") + ";}function pageload() {showsavemessage(‘" + Session("URL") + "‘);} 0 Then Session("URL") = Replace(txtCheckFlag.Text, "javascript:", "") Page.RegisterStartupScript("warning", "<script language=""javascript"">function pageload() {" + Session("URL") + ";} 0 Then Session("URL") = Replace(txtCheckFlag.Text, "javascript:", "") Page.RegisterStartupScript("Redirect", "<script language=""javascript"">function pageload() {" + Session("URL") + ";}function pageload() {window.location=‘" + Session("URL") + "‘;} 0 Then Page.RegisterStartupScript("TxtSave", "<script language=""javascript"">" + Session("URL") + " 0 Then Session("URL") = Replace(txtCheckFlag.Text, "javascript:", "") Page.RegisterStartupScript("TxtSave", "<script language=""javascript"">function pageload() {" + Session("URL") + ";}function pageload() {window.location=‘" + Session("URL") + "‘;}function pageload() {window.location=‘TestNoNeedCheck.aspx‘;}function pageload() {window.location=‘TestNoNeedCheck.aspx‘;}