if(this.mylist1.SelectedItem.Value=="1")
     this.mylist2.Items[1].Selected=true;
else
     this.mylist2.Items[2].Selected=true;

解决方案 »

  1.   

    Select Case (Me.MyList1.SelectedItem.Text)
                Case "AA"
                    Me.MyList2.SelectedIndex = 0
                    MyList2.DataBind()
                Case "BB"
                    Me.MyList2.SelectedIndex = 1
                    MyList2.DataBind()
    End Select
      

  2.   

    luoqing(明天将会...) 
    你的代碼當選擇時mylist2是會變,但mylist1卻還是不變。
      

  3.   

    Me.MyList2.SelectedIndex =Me.MyList1.SelectedItem.Text=="AA"?0:1;
      

  4.   

    什么意思?
    “擇時mylist2是會變,但mylist1卻還是不變”
      

  5.   

    public  void  mylist2_SelectedChanged(object  sender,EventArgs  e){  
               this.mylist1.SelectedIndex=(this.mylist2.SelectedItem.Value=="1")?0:1;
      }
      

  6.   

    luoqing(明天将会...)
    我把你的改了一下,還是不行!
    改動如下:
    public void mylist2_SelectedChanged(object sender,EventArgs e){
             this.mylist1.SelectedIndex=(Convert.ToInt32(this.mylist2.SelectedItem.Value));
      }
      

  7.   

    记得设mylist1.autopostback=truemylist2.SelectedIndex=mylist1.SelectedIndex;
      

  8.   

    试试
    public  void  mylist2_SelectedChanged(object  sender,EventArgs  e){  
               this.mylist1.SelectedIndex=(this.mylist2.SelectedItem.Value==1)?0:1;
      }
      

  9.   

    還是不行,不能改變mylist1的Text
      

  10.   

    建议:
    可以将DropDownList:mylist1的设置属性
    AutoPostBack=true
    即可实现预定功能
      

  11.   

    全部给你啦
    =========================================
    aspx:
    <%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="NW.WebForm2" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm2</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="WebForm2" method="post" runat="server">
    <asp:DropDownList id="d1" style="Z-INDEX: 101; LEFT: 261px; POSITION: absolute; TOP: 175px" runat="server" AutoPostBack="True">
    <asp:ListItem Value="a">a</asp:ListItem>
    <asp:ListItem Value="b">b</asp:ListItem>
    </asp:DropDownList>
    <asp:DropDownList id="d2" style="Z-INDEX: 102; LEFT: 361px; POSITION: absolute; TOP: 175px" runat="server">
    <asp:ListItem Value="x">x</asp:ListItem>
    <asp:ListItem Value="y">y</asp:ListItem>
    </asp:DropDownList>
    </form>
    </body>
    </HTML>
    ===========================================
    aspx.cs:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace NW
    {
    /// <summary>
    /// WebForm2 的摘要说明。
    /// </summary>
    public class WebForm2 : System.Web.UI.Page
    {
            protected System.Web.UI.WebControls.DropDownList d1;
            protected System.Web.UI.WebControls.DropDownList d2;
        
    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
                this.d1.SelectedIndexChanged += new System.EventHandler(this.d1_SelectedIndexChanged);
                this.Load += new System.EventHandler(this.Page_Load);        }
    #endregion        private void d1_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                d2.SelectedIndex=d1.SelectedIndex;
            }
    }
    }
      

  12.   

    把这一段
    <asp:DropDownList  id="mylist2"  AutoPostBack="true"/>  
    改成
    <asp:DropDownList  id="mylist2"  OnSelectedIndexChanged="mylist2_SelectedChanged"  AutoPostBack="true"/>
      

  13.   

    上面可能会引起死循环最好这样写
    if(this.mylist2.SelectedItem.Value=="1")
    {
         if(this.mylist1.SelectedItem.Value!="1")
              this.mylist1.Items[1].Selected=true;
    }
    else
    {
        if(this.mylist1.SelectedItem.Value!="2")
               this.mylist1.Items[2].Selected=true;
    }
      

  14.   

    看我的代碼,是哪裡錯了?
    .....
        public void Page_Load(object sender,EventArgs e){
        //Bind mylist1------------------------------
        myconn=new SqlConnection(conn1.connstr);
    mycmd=new SqlCommand("requestClass_01",myconn);
    mycmd.CommandType=CommandType.StoredProcedure;
    myconn.Open();
    mydr=mycmd.ExecuteReader();
           
        mylist1.DataSource=mydr;
    mylist1.DataBind();
    myconn.Close();

    //Bind mylist2--------------------------------
    myconn.Open();
    mycmd1=new SqlCommand("requestClass_02",myconn);
    mycmd1.CommandType=CommandType.StoredProcedure;
    mydr1=mycmd1.ExecuteReader();
           
        mylist2.DataSource=mydr1;
    mylist2.DataBind();

      }
      
      public void mylist1_SelectedChanged(object sender,EventArgs e){
            if(this.mylist2.SelectedItem.Value=="1")
            {
               if(this.mylist1.SelectedItem.Value!="1")
                this.mylist1.Items[1].Selected=true;
            }
            else
            {
               if(this.mylist1.SelectedItem.Value!="2")
               this.mylist1.Items[2].Selected=true;
            }
            }      
    </script>
    ......
    <asp:DropDownList ID="mylist1" runat="server" DataTextField="C005_002" DataValueField="C005_001" OnSelectedIndexChanged="mylist1_SelectedChanged" AutoPostBack="true"/><asp:DropDownList ID="mylist2" runat="server" DataTextField="C007_002" DataValueField="C007_001"/>
    ......
      

  15.   

    public void Page_Load(object sender,EventArgs e){
    if (!page.postback()){ //**************加上试试
        //Bind mylist1------------------------------
        myconn=new SqlConnection(conn1.connstr);
    mycmd=new SqlCommand("requestClass_01",myconn);
    mycmd.CommandType=CommandType.StoredProcedure;
    myconn.Open();
    mydr=mycmd.ExecuteReader();
           
        mylist1.DataSource=mydr;
    mylist1.DataBind();
    myconn.Close();

    //Bind mylist2--------------------------------
    myconn.Open();
    mycmd1=new SqlCommand("requestClass_02",myconn);
    mycmd1.CommandType=CommandType.StoredProcedure;
    mydr1=mycmd1.ExecuteReader();
           
        mylist2.DataSource=mydr1;
    mylist2.DataBind();
    }//**************加上试试

      }
      

  16.   

    最新代碼:(還是不行)
    ......
        public void Page_Load(object sender,EventArgs e){
      if(!Page.IsPostBack){
        //Bind mylist1------------------------------
        myconn=new SqlConnection(conn1.connstr);
    mycmd=new SqlCommand("requestClass_01",myconn);
    mycmd.CommandType=CommandType.StoredProcedure;
    myconn.Open();
    mydr=mycmd.ExecuteReader();
           
        mylist1.DataSource=mydr;
    mylist1.DataBind();
    myconn.Close();

    //Bind mylist2--------------------------------
    myconn.Open();
    mycmd1=new SqlCommand("requestClass_02",myconn);
    mycmd1.CommandType=CommandType.StoredProcedure;
    mydr1=mycmd1.ExecuteReader();
           
        mylist2.DataSource=mydr1;
    mylist2.DataBind();
    }

      }
     
      public void mylist1_SelectedChanged(object sender,EventArgs e){
              if(this.mylist1.SelectedItem.Value=="1")
                 this.mylist2.Items[0].Selected=true;
              else
                 this.mylist2.Items[1].Selected=true;
      }
      
      public void mylist2_SelectedChanged(object sender,EventArgs e){
            if(this.mylist2.SelectedItem.Value=="1")
            {
               if(this.mylist1.SelectedItem.Value!="1")
                this.mylist1.Items[0].Selected=true;
            }
            else
            {
               if(this.mylist1.SelectedItem.Value!="2")
               this.mylist1.Items[1].Selected=true;
            }
      } 
    .....
    <asp:DropDownList ID="mylist1" runat="server" DataTextField="C005_002" DataValueField="C005_001" OnSelectedIndexChanged="mylist1_SelectedChanged" AutoPostBack="true"/><asp:DropDownList ID="mylist2" runat="server" DataTextField="C007_002" DataValueField="C007_001" OnSelectedIndexChanged="mylist2_SelectedChanged" AutoPostBack="true"/>
    ......
        
      

  17.   

    public void Page_Load(object sender,EventArgs e){
    if(!Page.IsPostBack){   
      //Bind mylist1------------------------------
        myconn=new SqlConnection(conn1.connstr);
    mycmd=new SqlCommand("requestClass_01",myconn);
    mycmd.CommandType=CommandType.StoredProcedure;
    myconn.Open();
    mydr=mycmd.ExecuteReader();
           
        mylist1.DataSource=mydr;
    mylist1.DataBind();
    myconn.Close();

    //Bind mylist2--------------------------------
    myconn.Open();
    mycmd1=new SqlCommand("requestClass_02",myconn);
    mycmd1.CommandType=CommandType.StoredProcedure;
    mydr1=mycmd1.ExecuteReader();
           
        mylist2.DataSource=mydr1;
    mylist2.DataBind();
    }

      }
      
      public void mylist1_SelectedChanged(object sender,EventArgs e){
     if(this.mylist1.SelectedItem.Value=="1")
         this.mylist2.Items[1].Selected=true;
    else
         this.mylist2.Items[2].Selected=true;
            }      
    </script>
      

  18.   

    luoqing(明天将会...) 
    我寫成
    public void mylist1_SelectedChanged(object sender,EventArgs e){
     if(this.mylist1.SelectedItem.Value=="1")
         this.mylist2.Items[1].Selected=true;
    else
         this.mylist2.Items[2].Selected=true;
    }  
    報錯:
    Server Error in '/' Application.
    --------------------------------------------------------------------------------Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: indexSource Error: 
    Line 40:              this.mylist2.Items[1].Selected=true;
    Line 41:           else
    Line 42:              this.mylist2.Items[2].Selected=true;
    Line 43:   }所以我改成:Items[0],Items[1]
     
      

  19.   

    各位,朋友,謝謝你們的關心。
    chinchy(人民需要人民币)的方法在靜態下可以,但我的數據從數據庫裡取出時就不行。