当前位置:编程学习 > C#/ASP.NET >>

有关三级联动和页面无刷新的问题

想请教一下,我使用三个SqlDataSource控件分别为三个下拉列表框提供数据。在这种情况下,有没有可能实现下拉框的三级联动?而且是否还能实现页面无刷新?

如果可以的话请给点指点,最好能具体详细点! --------------------编程问答-------------------- SqlDataSource不用这玩意行不。?
--------------------编程问答--------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
  protected void Page_Load(object sender, EventArgs e)
  {
  if (!this.IsPostBack)
  {
  //绑定省
  SqlConnection con = new SqlConnection("server=.;database=dropDownTest;integrated security=true;");
  con.Open();
  SqlCommand cmd = new SqlCommand("select * from provience", con);
  SqlDataReader sdr = cmd.ExecuteReader();
  this.ddlprovince.DataSource = sdr;
  this.ddlprovince.DataTextField = "proName";
  this.ddlprovince.DataValueField = "proID";
  this.ddlprovince.DataBind();
  sdr.Close();
  //绑定市
  SqlCommand cmdcity = new SqlCommand("select * from city where proID=" + this.ddlprovince.SelectedValue, con);
  SqlDataReader sdr1 = cmdcity.ExecuteReader();
  this.ddlcity.DataSource = sdr1;
  this.ddlcity.DataTextField = "cityName";
  this.ddlcity.DataValueField = "cityID";
  this.ddlcity.DataBind();
  sdr1.Close();
  con.Close();
  }


   

  }
  protected void ddlprovince_SelectedIndexChanged(object sender, EventArgs e)
  {
  string proID = this.ddlprovince.SelectedValue;
  SqlConnection con = new SqlConnection("server=.;database=dropDownTest;integrated security=true;");
  con.Open();
  SqlCommand cmd = new SqlCommand("select*from city where proID=" + proID, con);
  SqlDataReader sdr= cmd.ExecuteReader();
  this.ddlcity.DataSource = sdr;
  this.ddlcity.DataTextField = "cityName";
  this.ddlcity.DataValueField = "cityID";
  this.ddlcity.DataBind();
  sdr.Close();
  con.Close();

  }
}

ddlprovince 为省 dropdownlist ID

ddlcity 为市 dropdownlist ID

proName proID为表provience中的字段 也就是省的

cityName cityID为市city表中的字段 是市的


ddlprovince_SelectedIndexChanged为省的dropdownlist 选中事件 触发绑定 

数据库 Demo 完整 --------------------编程问答-------------------- 当然可以,以下是部门和专业两个DropDownList联动,可类推n个,
(不过前面的n-1个的DropDownList的AutoPostBack要设置成true)
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            DataSourceID="SqlDataSource1" DataTextField="部门名称" DataValueField="部门id">
        </asp:DropDownList>

        <asp:DropDownList ID="DropDownList2" runat="server" 
            DataSourceID="SqlDataSource2" DataTextField="专业名称" 
            DataValueField="专业id">
        </asp:DropDownList>
 
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DBaseConnectionString %>" 
            SelectCommand="SELECT [部门id], [部门名称] FROM [部门表]">
        </asp:SqlDataSource>
        
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DBaseConnectionString %>" 
            SelectCommand="SELECT [专业id], [专业名称] FROM [专业表] WHERE ([部门id] = @部门id)">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="部门id" 
                    PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>        </asp:SqlDataSource>
红色是关键!

至于无刷新,简单的可用微软的ajax,套一下就可以了!
用jqury的ajax烦一点! --------------------编程问答-------------------- 貌似不能。
很久很久以前弄过一个无刷新的联动,还是手动创建的XmlHttpRequest对象,你可以看下。
参考 --------------------编程问答--------------------
引用 2 楼 wxr0323 的回复:
C# code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControl……

+1 这个修改下  很容易做成ashx+jquery+xml的无刷新联动 --------------------编程问答--------------------
引用 3 楼 lvyichang 的回复:
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="部门id" 
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters> </asp:SqlDataSource>
红色是关键!


是这样的! --------------------编程问答-------------------- <tr>
            <td style="width: 2490px; height: 28px; text-align: right">
                请选择实验室:</td>
            <td style="width: 181px; height: 28px; text-align: left">
                <asp:DropDownList ID="ddlLab" runat="server" Width="158px" AutoPostBack="True" DataSourceID="sdsLab" DataTextField="edName" DataValueField="experimentDepartID" >
                </asp:DropDownList></td>
            <td style="width: 213px; height: 28px; text-align: left">
            </td>
        </tr>
        <tr>
            <td colspan="3" style="height: 1px; text-align: right; background-color: black;">
            </td>
        </tr>
        <tr>
            <td style="width: 2490px; text-align: right">
                请选择实验课程名称:</td>
            <td style="width: 181px; text-align: left">
                <asp:DropDownList ID="ddlCourse" runat="server" Width="157px" AutoPostBack="True" DataSourceID="sdsExam" DataTextField="courseName" DataValueField="examId" >
                </asp:DropDownList></td>
            <td style="width: 213px; text-align: left">
                [若暂无名称需要先添加]</td>
        </tr>
        <tr>
            <td colspan="3" style="height: 1px; text-align: right; background-color: black;">
            </td>
        </tr>
        <tr>
            <td style="width: 2490px; text-align: right; height: 22px;">
                实验主题(或章节名称):</td>
            <td style="width: 181px; text-align: left; height: 22px;">
                <asp:DropDownList ID="ddlTheme" runat="server" Width="158px" DataSourceID="sdsTheme" DataTextField="labTheme" DataValueField="labThemeId">
                </asp:DropDownList></td>
            <td style="width: 213px; text-align: left; height: 22px;">
                [若暂无名称需要先添加]</td>
        </tr>

<asp:SqlDataSource ID="sdsLab" runat="server" ConnectionString="<%$ ConnectionStrings:devicemanageConnectionString %>"
                    SelectCommand="SELECT experimentDepartID, edName FROM experimentDepart ORDER BY experimentDepartID"></asp:SqlDataSource>
                <asp:SqlDataSource ID="sdsExam" runat="server" ConnectionString="<%$ ConnectionStrings:devicemanageConnectionString %>"
                    SelectCommand="SELECT courseName, examId, experimentDepartID FROM exam where experimentDepartID = @experimentDepartID">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="ddlLab" Name="experimentDepartID" PropertyName="SelectedValue" />
                    </SelectParameters>
                </asp:SqlDataSource>
                <asp:SqlDataSource ID="sdsTheme" runat="server" ConnectionString="<%$ ConnectionStrings:devicemanageConnectionString %>"
                    SelectCommand="SELECT labThemeId, labTheme, examId FROM lab_Theme WHERE (examId = @examId)">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="ddlCourse" Name="examId" PropertyName="SelectedValue" />
                    </SelectParameters>
                </asp:SqlDataSource>

你们说得红色部分我都有啊,但是好像只能联动一次,之后就回不来了。
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,