首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

dropdownlist的二级连接无论如何选择,都是显示第一个数据.为什么

2012-03-17 
dropdownlist的二级连接无论怎么选择,都是显示第一个数据.为什么?比如:第一个列表有 四川河南河北第二个

dropdownlist的二级连接无论怎么选择,都是显示第一个数据.为什么?
比如:第一个列表有 "   四川   河南     河北     "
          第二个列表是根据第一个变化而变化
但是无论第一个选什么值,都是四川的城市 "成都     宜宾     绵阳 "   为什么呢?
代码如下:
<script   language= "vb "   runat= "server ">
Dim   Myds   As   DataSet
        Sub   Page_Load(sender   as   object,   e   as   Eventargs)  
  dim   conn   as   new   oledbconnection()
conn.connectionstring= "Provider=Microsoft.Jet.OLEDB.4.0;   Data   Source= "     &   server.MapPath( "../db/data.mdb ")
conn.open()
      Dim   strSql   As   String
      strSql= "select   *   from   category   order   by   categoryid "
      Dim   MyoleAp   As   OleDbDataAdapter   =   New   OleDbDataAdapter(strSql,conn)  
      Myds=New   DataSet()  
      MyoleAp.Fill(Myds, "category ")
      DropDownList1.DataSource=Myds.Tables( "category ")
      DropDownList1.DataValueField= "categoryid "
      DropDownList1.DataTextField= "category "
      DropDownList1.DataBind()

      strSql= "select   *     from   sortsid   order   by   sortsid "
      MyoleAp.SelectCommand.CommandText=strSql
      MyoleAp.Fill(Myds, "sortsid ")
      DropDownList2.DataSource=Myds.Tables( "sortsid ")
      DropDownList2.DataValueField= "sortsid "
      DropDownList2.DataTextField= "sorts "
          MyoleAp.Dispose()
conn.close()
    End   Sub
 

 
  Sub   DropDownList1_SelectedIndexChanged(ByVal   sender   As   Object,   ByVal   e   As   System.EventArgs)
            Myds.Tables( "sortsid ").DefaultView.RowFilter= "categoryid= ' "   &   DropDownList1.selectedvalue   &   " ' "
      DropDownList2.DataBind()
    End   Sub
</script>
<略>
<form   runat= "server "   >
    <asp:DropDownList   ID= "DropDownList1 "     OnSelectedIndexChanged= "DropDownList1_SelectedIndexChanged "       AutoPostBack= "true "     runat= "server "   />
        <asp:DropDownList   ID= "DropDownList2 "   runat= "server "   />
</form>

[解决办法]
DropDownList1.DataSource = Myds.Tables( "category ")
DropDownList1.DataValueField = "categoryid "
DropDownList1.DataTextField = "category "
DropDownList1.DataBind()

这几句前后加上
If (Not IsPostBack) Then 和 End If
变为

If (Not IsPostBack) Then
DropDownList1.DataSource = Myds.Tables( "category ")
DropDownList1.DataValueField = "categoryid "
DropDownList1.DataTextField = "category "
DropDownList1.DataBind()
End If



防止提交返回时仍然重新载入DropDownList1的数据并默认将Value置0.
就行了.

热点排行