DropDownList无刷新二级联动(.ashx)

本文介绍了一种使用ASP.NET实现二级联动下拉框的方法。通过客户端JavaScript结合服务器端VB.NET处理,实现了供应商选择后自动更新相应付款方式的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因工作需要,需做一下拉框无刷新二级联动功能。上网搜索到很多种实现方式,最后选中一大神写的,谢谢这位大神!大神文章地址(http://blog.csdn.net/wangjun8868/article/details/2983149)。

我做的功能是,选择供应商,带出对应的付款方式。

Default.aspx 默认页面

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="Module_FFY_LetterOfCredit_Default"
    EnableEventValidation="false" %>

<!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>无标题页</title>

    <script src="../../../js/jquery-1.3.2.min.js" type="text/javascript" language="javascript"></script>

    <script src="../../../js/jquery-ui-1.7.2.custom.min.js" type="text/javascript" language="javascript"></script>

    <script type="text/javascript">
            $(document).ready(function() {
	            $.post('SupHandler.ashx', {}, function(data) { $("#DropDownList1").html(data) }, 'html');
                $.post('PayModeHandler.ashx', {supCode:""}, function(data) { $("#DropDownList2").html(data) }, 'html');
                $("#DropDownList1").change(function()
                {
                    $.post('PayModeHandler.ashx', {supCode:$(this).val()}, function(data) { $("#DropDownList2").empty().html(data) }, 'html');
                });
            });
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <div>
            供应商:   
            <asp:DropDownList ID="DropDownList1" runat="server">
            </asp:DropDownList>
            <br />
            付款方式:<asp:DropDownList ID="DropDownList2" runat="server">
            </asp:DropDownList>
            <br />
            <asp:Button ID="Button1" runat="server" Text="Button" /></div>
    </form>
</body>
</html>

注: EnableEventValidation="false" 一定要有,否则就黄了。

SupHandler.ashx 用于获取供应商信息

<%@ WebHandler Language="VB" Class="SupHandler" %>

Imports System
Imports System.Web
Imports System.Data.OracleClient
Imports YuanHang.DBUtility

Public Class SupHandler : Implements IHttpHandler
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.ContentType = "text/plain"
        Dim ssql As String = "SELECT cid,cname,caccount,cbank,caddress,cpostcode,cplace,ctype,cmode FROM T_FFY_CREDITBENEFICIARY"
        Dim dr As OracleDataReader = DbHelperSQL.ExecuteReader(ssql)
        Dim st As StringBuilder = New StringBuilder()
        While dr.Read()
            st.Append("<option value='" & dr("cid").ToString() & "'>" & _
                        dr("cname").ToString() & "</option>/n")
        End While
        dr.Close()
        context.Response.Write(st.ToString())
    End Sub
 
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

PayModeHandler.ashx 用于获取付款方式信息

<%@ WebHandler Language="VB" Class="PayModeHandler" %>

Imports System
Imports System.Web
Imports System.Data.OracleClient
Imports YuanHang.DBUtility

Public Class PayModeHandler : Implements IHttpHandler
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.ContentType = "text/plain"
        context.Response.Clear()
        Dim strSupCode As String = context.Request.Form("supCode").ToString()

        Dim ssql As StringBuilder = New StringBuilder()
        If strSupCode <> "" Then
            ssql.Append(" SELECT * FROM (")
            ssql.Append(" SELECT 0 AS tag,ctype FROM T_FFY_CREDITBENEFICIARY")
            ssql.Append(" WHERE cid = '" & strSupCode & "'")
            ssql.Append(" UNION")
            ssql.Append(" SELECT 1 AS tag,ctype FROM T_FFY_CREDITBENEFICIARY")
            ssql.Append(" WHERE cid <> '" & strSupCode & "'")
            ssql.Append(" ) ORDER BY tag")
        Else
            ssql.Append(" SELECT distinct ctype FROM T_FFY_CREDITBENEFICIARY")
        End If
        
        Dim dr As OracleDataReader = DbHelperSQL.ExecuteReader(ssql.ToString())
        Dim st As StringBuilder = New StringBuilder()
        While dr.Read()
            st.Append("<option value='" & dr("ctype").ToString() & "'>" & _
                        dr("ctype").ToString() & "</option>/n")
        End While
        dr.Close()
        context.Response.Write(st.ToString())
    End Sub
 
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
End Class
 
 
关于获取下拉框值,如下: 
 
供应商: Request.Form("DropDownList1") 
 
付款方式: Request.Form("DropDownList2") 
 
OVER!!! 

 
 
 
 

                
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值