DropDownList无刷新二级联动

(21) 2024-03-01 13:12

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说DropDownList无刷新二级联动,希望能够帮助你!!!。
一、数据表CAT结构:

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张
CREATE
 
TABLE
 
[
dbo
]
.
[
CAT
]
 (
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张    

[
CAT_ID
]
 
[
int
]
 
IDENTITY
 (
1

1

NOT
 
NULL
 ,
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张    

[
CAT_NAME
]
 
[
nvarchar
]
 (
20
) COLLATE Chinese_Taiwan_Stroke_CI_AS 
NULL
 ,
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张    

[
PARENT_ID
]
 
[
int
]
 
NULL
 
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

ON
 
[
PRIMARY
]

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

GO

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

ALTER
 
TABLE
 
[
dbo
]
.
[
CAT
]
 
ADD
 
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张    

CONSTRAINT
 
[
DF_CAT_PARENT_ID
]
 
DEFAULT
 (
0

FOR
 
[
PARENT_ID
]
,
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张    

CONSTRAINT
 
[
PK_CAT
]
 
PRIMARY
 
KEY
  
CLUSTERED
 
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张    (
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张        

[
CAT_ID
]

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张    )  

ON
 
[
PRIMARY
]
 
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

GO

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

二、配置web.config:


<system.web>部分加入:

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张
<
httpHandlers
>

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张      

<
add verb
=
"
POST,GET
"
 path
=
"
ajax/*.ashx
"
 type
=
"
Ajax.PageHandlerFactory, Ajax
"
 
/>

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

</
httpHandlers
>

三、添加
AjaxMethod.cs:

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张
using
 System;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

using
 System.Data;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

using
 System.Data.SqlClient;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

using
 System.Configuration;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

using
 System.Collections;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

using
 System.Web;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

using
 System.Web.Security;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

using
 System.Web.UI;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

using
 System.Web.UI.WebControls;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

using
 System.Web.UI.WebControls.WebParts;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

using
 System.Web.UI.HtmlControls;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第32张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第33张


/// <summary>
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张
/// AjaxMethod 的摘要描述
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第35张
/// </summary>



DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张
public
 
class
 AjaxMethod
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第32张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第33张


{
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张    
GetDataSet
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张    
public static DataSet GetCatList(string id)
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张    
{
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        
string sql = "SELECT CAT_ID,CAT_NAME FROM CAT WHERE PARENT_ID='" + id.ToString() + "'";
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        
return GetDataSet(sql);
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第51张    }

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第35张}

四、Dropdownlist.aspx代码:

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第32张
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第33张
<%

@ Page Language="C#" AutoEventWireup="true" CodeFile="Dropdownlist.aspx.cs" Inherits="Dropdownlist" EnableEventValidation="false" 
%>

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
>

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

<
HTML
>

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

<
HEAD
>

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

<
title
>
DropDownList示例
</
title
>

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第32张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第33张

<
script 
language
="javascript"
>


DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张
//Cat联动部分
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张
function catResult() 
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张    
var bcat=document.getElementById("<%=ddlbCat.ClientID%>");
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张    
if (bcat.value != "")
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张    
{
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张       AjaxMethod.GetCatList(bcat.value,get_cat_Result_CallBack);
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第51张    }

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张    
else
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张    
{
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张      document.all(
"<%=ddlCat.ClientID%>").length=0;    
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张      document.all(
"<%=ddlCat.ClientID%>").options.add(new Option("--Select--",""));
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第51张    }

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第51张}

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张            
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张
function get_cat_Result_CallBack(response)
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张
{
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张    
if (response.value != null)
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张    
{                    
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        document.all(
"<%=ddlCat.ClientID%>").length=0; 
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        document.all(
"<%=ddlCat.ClientID%>").options.add(new Option("--Select--",""));             
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        
var ds = response.value;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        
if(ds != null && typeof(ds) == "object" && ds.Tables != null)
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张        
{                    
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张            
for(var i=0; i<ds.Tables[0].Rows.length; i++)
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张            
{
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张                
var name=ds.Tables[0].Rows[i].CAT_NAME;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张                
var id=ds.Tables[0].Rows[i].CAT_ID;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张                document.all(
"<%=ddlCat.ClientID%>").options.add(new Option(name,id));
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第51张            }

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第51张        }

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第51张    }
           
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张    
return
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第35张}

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

</
script
>

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

</
HEAD
>

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

<
body
>

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

<
form 
id
="Form1"
 method
="post"
 runat
="server"
>

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

<
asp:DropDownList 
ID
="ddlbCat"
 runat
="server"
 Width
="80px"
></
asp:DropDownList
>
&nbsp;&nbsp;&nbsp;

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

<
asp:DropDownList 
ID
="ddlCat"
 runat
="server"
 Width
="80px"
><
asp:ListItem 
Value
=""
>
--Select--
</
asp:ListItem
></
asp:DropDownList
>

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

</
form
>

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

</
body
>

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

</
HTML
>

五、Dropdownlist.aspx.cs代码:

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张
using
 System;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

using
 System.Data;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

using
 System.Configuration;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

using
 System.Web;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

using
 System.Web.Security;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

using
 System.Web.UI;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

using
 System.Web.UI.WebControls;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

using
 System.Web.UI.WebControls.WebParts;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

using
 System.Web.UI.HtmlControls;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张

using
 System.Data.SqlClient;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第32张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第33张


/// <summary>
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张
/// Dropdownlist 的摘要说明。
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第35张
/// </summary>



DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第1张
public
 partial 
class
 Dropdownlist : System.Web.UI.Page
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第32张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第33张


{
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张    
private void Page_Load(object sender, System.EventArgs e)
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张    
{
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        Ajax.Utility.RegisterTypeForAjax(
typeof(AjaxMethod));
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        
if (!this.Page.IsPostBack)
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张        
{
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张            
//Load Controls
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张
            this.DDLBind(this.ddlbCat, "select [CAT_ID],[CAT_NAME] from CAT where PARENT_ID='0'");
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张            
this.ddlbCat.Attributes.Add("onclick""catResult();");
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第51张        }

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第51张    }

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张    
//Load DropDownList
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张
    private void DDLBind(System.Web.UI.WebControls.DropDownList DDL, string SQLString)
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张    
{
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        
string connectionString = ConfigurationManager.AppSettings["dbConnectionString"];
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        SqlConnection connection 
= new SqlConnection(connectionString);
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        connection.Open();
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        
try
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张        
{
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张            SqlCommand cmd 
= new SqlCommand(SQLString, connection);
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张            SqlDataReader dr 
= cmd.ExecuteReader();
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张            DDL.Items.Clear();
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张            ListItem default_item 
= new ListItem();
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张            default_item.Value 
= "";
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张            default_item.Text 
= "--Select--";
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张            DDL.Items.Add(default_item);
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张            
while (dr.Read())
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张            
{
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张                ListItem ListItem 
= new ListItem();
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张                ListItem.Value 
= dr.GetValue(0).ToString();
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张                ListItem.Text 
= dr.GetString(1);
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张                DDL.Items.Add(ListItem);
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第51张            }

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张            dr.Close();
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第51张        }

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        
finally
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张        
{
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张            
if (connection.State == ConnectionState.Open)
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张                connection.Close();
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第51张        }

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第51张    }

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张    
//Load Selected DropDownList
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张
    public static void DDLOldBind(System.Web.UI.WebControls.DropDownList DDL, string sql, string Id)
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张    
{
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        DDL.Items.Clear();
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        ListItem default_item 
= new ListItem();
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        default_item.Value 
= "";
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        default_item.Text 
= "--Select--";
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        DDL.Items.Add(default_item);
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        
try
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张        
{
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张            DataTable dt 
= ExecuteQuery(sql);
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张            
if (dt.Rows.Count >= 1)
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张            
{
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张                
foreach (DataRow dr in dt.Rows)
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张                
{
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张                    ListItem ListItem 
= new ListItem();
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张                    ListItem.Value 
= dr[0].ToString();
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张                    ListItem.Text 
= dr[1].ToString();
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张                    DDL.Items.Add(ListItem);
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第51张                }

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第51张            }

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张            DDL.SelectedValue 
= Id;
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第51张        }

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张        
catch (System.Data.SqlClient.SqlException E)
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第41张DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第40张        
{
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张            
throw new Exception(DDL.ID.ToString() + E.Message.ToString());
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第51张        }

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第51张    }

DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第34张
DropDownList无刷新二级联动_https://bianchenghao6.com/blog__第35张}

六、Ajax.dll文件.
Ajax.dll下载
今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

上一篇

已是最后文章

下一篇

已是最新文章

发表回复