说说vb.net ajaxpro 实现dropdownlist二级无刷新联动~!
Code
<div class="div_RSL">
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="proID" DataTextField="proName"AutoPostBack="false" DataValueField="proID">
</asp:DropDownList>
<asp:SqlDataSource ID="proID" runat="server" ConnectionString="<%$ ConnectionStrings:opperajaxConnectionString %>"SelectCommand="SELECT [proID], [proName] FROM [TB_province] ORDER BY [proID]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</div>
<div class="div_RSL">
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="proID" DataTextField="proName"AutoPostBack="false" DataValueField="proID">
</asp:DropDownList>
<asp:SqlDataSource ID="proID" runat="server" ConnectionString="<%$ ConnectionStrings:opperajaxConnectionString %>"SelectCommand="SELECT [proID], [proName] FROM [TB_province] ORDER BY [proID]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</div>
这里dropdownlist1是服务器控件绑定,很方便~!
Code
function ShowCity(id)
{
var res= Other_Register2.GetCity(id).value;
var drop = document.getElementById("<%=DropDownList2.UniqueID %>");
drop.length=0;
for(var i=0;i<res.Rows.length;i++)
{
drop.options.add(new Option(res.Rows[i].TB_cityName,res.Rows[i].TB_cityID));
}
}
function ShowCity(id)
{
var res= Other_Register2.GetCity(id).value;
var drop = document.getElementById("<%=DropDownList2.UniqueID %>");
drop.length=0;
for(var i=0;i<res.Rows.length;i++)
{
drop.options.add(new Option(res.Rows[i].TB_cityName,res.Rows[i].TB_cityID));
}
}
alert(res)将返回一个[object object]类型
alert(res.rows)将返回一个object类型
alert(res.rows[0].value)将返回一个[object object]类型
如果返回null类型,接收不到datatable数据,则可能是你的ajaxpro.2.dll并不支持datatable输出,建议你下载文章末尾ajaxpro.2.dll附件~!
vb.net写法:
Code
Sub Page_Load() Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
DropDownList1.Attributes("onchange") = "ShowCity(this.options[selectedIndex].value)"
End If
AjaxPro.Utility.RegisterTypeForAjax(GetType(Other_Register2))
End Sub
Function GetCity() Function GetCity(ByVal sproid) As DataTable
Dim sqlconnection As New SqlConnection(ShareFunction.GetDBConnectionString())
sqlconnection.Open()
Dim sql As String = "select TB_cityID,TB_cityName from TB_city where proID=" & ShareFunction.SafeRequest(sproid, 1) & ""
Dim dataada As New SqlDataAdapter(sql, sqlconnection)
Dim dt As New DataTable
dataada.Fill(dt)
Return dt
End Function
Sub Page_Load() Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
DropDownList1.Attributes("onchange") = "ShowCity(this.options[selectedIndex].value)"
End If
AjaxPro.Utility.RegisterTypeForAjax(GetType(Other_Register2))
End Sub
Function GetCity() Function GetCity(ByVal sproid) As DataTable
Dim sqlconnection As New SqlConnection(ShareFunction.GetDBConnectionString())
sqlconnection.Open()
Dim sql As String = "select TB_cityID,TB_cityName from TB_city where proID=" & ShareFunction.SafeRequest(sproid, 1) & ""
Dim dataada As New SqlDataAdapter(sql, sqlconnection)
Dim dt As New DataTable
dataada.Fill(dt)
Return dt
End Function
这里使用dataadapter对象将查询数据存入datatable中~!
外出网 www.waichu.net
发酷网 www.faako.com