using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
using webwinscom.DAL.proClassIdTableAdapters;namespace webwinscom.admin
{
    public partial class addpro : System.Web.UI.Page
    {   
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {           
               
            }
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string x = DropDownList1.SelectedValue;
            string xx = "SELECT  [Pro_name], [pro_ClassId] FROM [Pro_NameID] where pro_ClassId=" + x;
          
            SqlDataSource2.SelectCommand = xx;                 
        }            
    }
}

解决方案 »

  1.   

    dropdownlist 联动 ,第二个dropdownlist的取值总是(第一个选项)怎么解决?  
      

  2.   

    DropDownList1启用autopostback了么?
      

  3.   

    还是不懂。。指的是选定项?用SelectedValue="";设定选定项。。
      

  4.   

         <tr >
                    <td class="style3" > 
                        <asp:Label ID="Label1" runat="server" Text="所属父类:"></asp:Label>
                        <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" 
                            DataTextField="protype" DataValueField="ProClass" AutoPostBack="True" 
                            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:T_winscomConnectionString %>" 
                            SelectCommand="SELECT DISTINCT [protype], [ProClass] FROM [Pro_TypeID]">
                        </asp:SqlDataSource>
                    </td>
           </tr>
            <tr>
                    <td class="style3" > 
                        <asp:Label ID="Label2" runat="server" Text="类别名称:"></asp:Label>                    <asp:DropDownList ID="DropDownList2" runat="server" 
                            DataSourceID="SqlDataSource2" DataTextField="Pro_name" 
                            DataValueField="pro_ClassId" AutoPostBack="True" 
                            onselectedindexchanged="DropDownList2_SelectedIndexChanged">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:T_winscomConnectionString %>" 
                            SelectCommand="SELECT [Pro_name], [pro_ClassId] FROM [Pro_NameID]">
                        </asp:SqlDataSource>
                    </td>
               </tr> 
      

  5.   

    dropdownlist似乎选不中没value的项
      

  6.   

    DropDownList2.Items.FindByText("").Selected = true;//根据text
    DropDownList2.Items.FindByValue("").Selected = true;//根据value
      

  7.   

    删除SqlDataSource ID="SqlDataSource2"中SelectCommand="SELECT [Pro_name], [pro_ClassId] FROM [Pro_NameID]">DropDownList1变化是赋值
    或设置DropDownList1为参数,根据参数联动
    还有AJAX
      

  8.   

    DropDownList1启用autopostback貌似就解决了、楼主可以尝试
      

  9.   

     ID="DropDownList2"  AutoPostBack="True" 
      

  10.   

    2个DropDownList的代码:
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
                onselectedindexchanged="DropDownList1_SelectedIndexChanged">
            </asp:DropDownList>
            
            <asp:DropDownList ID="DropDownList2" runat="server">
            </asp:DropDownList>DropDownList1的数据你是从数据库里读取的,DropDownList2的数据先不用绑定后台
     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string value = this.DropDownList1.SelectedItem.Value.ToString();
     
                    this.DropDownList2.Items.Clear();
            this.DropDownList2.Items.Add(new ListItem(value, value));//如果你是根据选择的数据再查询出一个集合的话,上面的就不用换下面的
    this.DropDownList2.DataSource = dt;
            this.DropDownList2.DataTextField = "显示的字段";
            this.DropDownList2.DataValueField = "一般是ID";
            this.DropDownList2.DataBind();
        }
      

  11.   

    如果DropDownList1没有启用autopostback,还得启用
      

  12.   

    DropDownList1启用了autopostback,
    DropDownList2取消了autopostback,
    现在DropDownList2能根据DropDownList1的值来读取数据,
    但是无论怎么选Response.Write(DropDownList2.SelectedItem.Text)  显示出来的值总是DropDownList2里面的第一项
      

  13.   

    Just SoSo
    http://www.cnblogs.com/zhangyu028/articles/310568.html
      

  14.   

    问题解决了,我把SelectCommand="SELECT [Pro_name], [pro_ClassId] FROM [Pro_NameID]">
    改成SelectCommand="SELECT [Pro_name] FROM [Pro_NameID]">
    就可以了。
    但还是不懂为什么?