写漏了,上述代码运行时,数据未绑定成功,更不消说第一个dropdownlist的值确定第二个的值,错在哪里?

解决方案 »

  1.   

    <html>
    <asp:label id="label1" Runat="server">选择大类:</asp:label><asp:dropdownlist id="Drop1" Runat="server" AutoPostBack="True" ForeColor="#000033" BackColor="#D6E3FF"
    DataValueField="ParentID" DataTextField="ParentName"></asp:dropdownlist></td>
    <td><asp:label id="Label2" Runat="server">选择小类:</asp:label><asp:dropdownlist id="Dropdownlist1" Runat="server" ForeColor="#000033" BackColor="#D6E3FF" DataValueField="ParentName"
    DataTextField="NodeName"></asp:dropdownlist>
    </tr>
    <tr>
    <td style="WIDTH: 662px" align="right"><asp:label id="Label3" Runat="server">输入关键字:</asp:label><asp:textbox id="textbox1" Runat="server"></asp:textbox></td>
    <td><asp:button id="submit" Runat="server" Text="搜索"></asp:button>
    vb代码
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            theUser = Session("User")
            UserName.Text = theUser
            If Trim(theUser) = "" Then
                Response.Write("<h2 style='Color:red'>")
                Response.Write("对不起,你尚未注册,请重新注册!</h2>")
                Response.End()
            End If
            If Not IsPostBack Then
                Dim Cnn As OleDb.OleDbConnection
                Dim Cmd As OleDb.OleDbDataAdapter
                Dim StrCnn As String
                Dim Ds As New DataSet
                StrCnn = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Server.MapPath("/BBS.mdb")
                StrSQL = "select ParentID, ParentName from TreeNode1 "
                Cnn = New OleDb.OleDbConnection(StrCnn)
                Cnn.Open()
                Cmd = New OleDb.OleDbDataAdapter(StrSQL, Cnn)
                Cmd.Fill(Ds, "TreeNode1")
                Drop1.DataSource = Ds.Tables("TreeNode1").DefaultView
                Drop1.DataTextField = "ParentName"/////////注意这里
                Drop1.DataValueField = "ParentID"///////////注意这里
                Drop1.DataBind()
            End If
        End Sub    Private Sub Drop1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Drop1.SelectedIndexChanged
            Dim Cnn As OleDb.OleDbConnection
            Dim Cmd As OleDb.OleDbCommand
            Dim Dr As OleDb.OleDbDataReader
            Dim Ds As DataSet = New DataSet
            Dim StrCnn As String
            StrCnn = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Server.MapPath("/BBS.mdb")
            Cnn = New OleDb.OleDbConnection(StrCnn)
            Cnn.Open()
            Cmd = New OleDb.OleDbCommand(StrSQL, Cnn)
            StrSQL = "select NodeName from TreeNode1 where ParentName='" + Drop1.SelectedItem.Text.ToString() + "'"
            Dropdownlist1.DataSource = Ds.Tables("TreeNode").DefaultView
            Drop1.DataTextField = "NodeName"////////////////////注意这里
                Drop1.DataValueField = "NodeName"///////////注意这里
            Dropdownlist1.DataBind()
        End Sub
        Private Sub submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit.Click
            Dim theTag As String
            theTag = Session("tag")
            If Drop1.SelectedItem.Text <> "" And Dropdownlist1.SelectedItem.Text <> "" Then
                Response.Redirect("query.aspx?thetype=thetag")
            End If    End Sub
    End Class
      

  2.   

    我按smilnet(苯苯)的作了修改同样不行,dropdownlist显示“未绑定”,显示同样下拉一片空白,无任何绑定。
      

  3.   

    作如下修改后,数据能绑定,但运行时,drop1改变时,dropdownlist1依然各项都有,未能跟着改变。请帮看看,到底怎回事? 
    If Not IsPostBack Then
                Dim Cnn1 As OleDb.OleDbConnection
                Dim Cmd1 As OleDb.OleDbDataAdapter
                Dim StrCnn1 As String
                Dim strSql1 As String
                Dim Ds As New DataSet
                StrCnn1 = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Server.MapPath("/BBS.mdb")
                strSql1 = "select NodeID, ParentName,NodeName from TreeNode1"
                Cnn1 = New OleDb.OleDbConnection(StrCnn1)
                Cnn1.Open()
                Cmd1 = New OleDb.OleDbDataAdapter(strSql1, Cnn1)
                Cmd1.Fill(Ds, "TreeNode1")
                Drop1.DataSource = Ds.Tables("TreeNode1").DefaultView
                Drop1.DataTextField = "ParentName"
                Drop1.DataValueField = "NodeID"
                Drop1.DataBind()
                Dropdownlist1.DataSource = Ds.Tables("TreeNode1").DefaultView
                Dropdownlist1.DataTextField = "NodeName"
                Dropdownlist1.DataValueField = "NodeID"
                Dropdownlist1.DataBind()        End If
        End Sub    Private Sub Drop1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Drop1.SelectedIndexChanged
            Dim Cnn As OleDb.OleDbConnection
            Dim Cmd As OleDb.OleDbCommand
            Dim Dr As OleDb.OleDbDataReader
            Dim Ds As DataSet = New DataSet
            Dim theName As String
            theName = Drop1.SelectedItem.Text.ToString
            Dropdownlist1.Items.Clear()
            Dim StrCnn As String
            StrCnn = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Server.MapPath("/BBS.mdb")
            Cnn = New OleDb.OleDbConnection(StrCnn)
            Cnn.Open()
            Cmd = New OleDb.OleDbCommand(StrSQL, Cnn)
            StrSQL = "select NodeName from TreeNode1 where Parent='" & theName & "'"
            Dropdownlist1.DataSource = Ds.Tables("TreeNode1").DefaultView
            Dropdownlist1.DataTextField = "NodeName"
            Dropdownlist1.DataValueField = "NodeID"
            Dropdownlist1.DataBind()
        End Sub
      

  4.   

    Dropdownlist1.DataSource = Ds.Tables("TreeNode1").DefaultView 不对,数据要根据重新绑定!
    Cmd.Fill(Ds, "TreeNode2")
    Dropdownlist1.DataSource = Ds.Tables("TreeNode2").DefaultView
      

  5.   

    设置drop1的属性AutoPostBack=true;
      

  6.   

    你先绑定第一个控件,然后调用第一个控件的OnSelectedIndexChanged方法,并设置AutoPostBack="True",下面是我使用时的代码(OnSelectedIndexChanged="Selection_Change" ):
    ---------------------------------------------------------------------------void Selection_Change(Object sender, EventArgs e)
    {
    // 设置选择字段类型时,字段长度默认值
    SCN.Desktop.EmpInfoDB cascade = new SCN.Desktop.EmpInfoDB(dsn); // 提取岗位类别,并绑定到DropDownList上
    string str_SuperID = f_EmpDept.SelectedItem.Value;
    f_EmpPosition.DataSource = cascade.GetSubDataDicByID(str_SuperID);
    f_EmpPosition.DataValueField="f_SubCatID";
    f_EmpPosition.DataTextField="f_SubCatName";
    f_EmpPosition.DataBind();
    }
      

  7.   

    在If Not IsPostBack Then      end if中绑定第一个控件没问题,在Private Sub Drop1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Drop1.SelectedIndexChanged中绑定第二个控件还是不成功(设置drop1的AutoPostBack="True")代码如下改动,与drop1绑定在不同table中,二者通过parentName 关联,问题在哪?Private Sub Drop1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Drop1.SelectedIndexChanged
            Dim Cnn As OleDb.OleDbConnection
            Dim Cmd As OleDb.OleDbDataAdapter
            Dim Dr As OleDb.OleDbDataReader
            Dim Ds As DataSet = New DataSet
            Dim theName As String
            theName = Drop1.SelectedItem.Text
            Dropdownlist1.Items.Clear()
            Dim StrCnn As String
            StrCnn = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Server.MapPath("/BBS.mdb")
            Cnn = New OleDb.OleDbConnection(StrCnn)
            Cnn.Open()
            Cmd = New OleDb.OleDbDataAdapter(StrSQL, Cnn)
            StrSQL = "select NodeName from TreeNode2 where ParentName='" & theName & "'"        Cmd.Fill(Ds, "TreeNode2")
            Dropdownlist1.DataSource = Ds.Tables("TreeNode2").DefaultView
            Dropdownlist1.DataTextField = "NodeName"
            Dropdownlist1.DataValueField = "NodeID"
            Dropdownlist1.DataBind()
        End Sub
      

  8.   

    select NodeName from TreeNode2 where ParentName='" & theName & "'  这句在查询分析器中是否正确?
        用 where ParentName=@name,在给@name赋值
    注意下列次序:
    Cnn = New OleDb.OleDbConnection(StrCnn)
    Cnn.Open()
    StrSQL = "select NodeName from TreeNode2 where ParentName='" & theName & "'"
    Cmd = New OleDb.OleDbDataAdapter(StrSQL, Cnn)
      

  9.   

    救命啦!!!怪怪怪!!!象csdn的搜索一样分大类搜索和小类搜索,现在不仅不能绑定drop2而且,点击大类搜索(select语句与小类的值无关)出现下列问题,该怎么改?
    Private Sub Drop1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Drop1.SelectedIndexChanged
            
            Dim Cnn As OleDb.OleDbConnection
            Dim Cmd As OleDb.OleDbDataAdapter
            Dim Dr As OleDb.OleDbDataReader
            Dim Ds As DataSet = New DataSet
            Dim theName As String
            theName = Drop1.SelectedItem.Text
            Dropdownlist1.Items.Clear()
            Dim StrCnn As String
            StrCnn = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Server.MapPath("/BBS.mdb")
            Cnn = New OleDb.OleDbConnection(StrCnn)
            Cnn.Open()
            Cmd = New OleDb.OleDbDataAdapter(strSql, Cnn)
            strSql = "select ParentName,NodeName from TreeNode2 where ParentName='" & theName & "'"        Cmd.Fill(Ds, "TreeNode2")
            Dropdownlist1.DataSource = Ds.Tables("TreeNode2").DefaultView
            Dropdownlist1.DataTextField = "NodeName"
            Dropdownlist1.DataValueField = "ParentName"
            Dropdownlist1.DataBind()
        End Sub
    “/WebBBS”应用程序中的服务器错误。
    --------------------------------------------------------------------------------DataBinder.Eval:“System.Data.DataRowView”不包含名称为 NodeName 的属性。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Web.HttpException: DataBinder.Eval:“System.Data.DataRowView”不包含名称为 NodeName 的属性。源错误: 
    行 120:        Dropdownlist1.DataTextField = "NodeName"
    行 121:        Dropdownlist1.DataValueField = "ParentName"
    行 122:        Dropdownlist1.DataBind()
    行 123:    End Sub
    行 124:
     源文件: C:\Inetpub\wwwroot\WebBBS\Searchz.aspx.vb    行: 122 堆栈跟踪: 
    [HttpException (0x80004005): DataBinder.Eval:“System.Data.DataRowView”不包含名称为 NodeName 的属性。]
       System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName)
       System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName, String format)
       System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e)
       System.Web.UI.Control.DataBind()
       WebBBS.Searchz.Drop1_SelectedIndexChanged(Object sender, EventArgs e) in C:\Inetpub\wwwroot\WebBBS\Searchz.aspx.vb:122
       System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e)
       System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent()
       System.Web.UI.Page.RaiseChangedEvents()
       System.Web.UI.Page.ProcessRequestMain() 
    --------------------------------------------------------------------------------
    版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573
      

  10.   

    先在Page_Load里面绑定第一个DropDownList的值,把第一个DropDownList设置为AutoPostBack=true,在SelectIndexChanged对应的事件里面写代码,根据第一个里面选定的值作为条件来绑定第二个 DropDownList的项
      

  11.   

    可以把数据绑定在客户端hide域里面,js来操作显示,这样速度快点(感觉上)
      

  12.   

    我也同意: xueyhfeng(核桃(努力中…看书充电)) 的说法!
      

  13.   

    这种帮定要注意几个关键的地方:
    (1)在page_load里先帮定DropDownList1,并且应该确保它只执行一次,也就是
    if(!IsPostBack)
    {
        //帮定DropDownList1
       //如有必要,DropDownList2也在这邦定一下
    }
    (2)DropDownList1的AutoPostBack的属性要设为true
    (3)在DropDownList1的SelectIndexChanged的事件中邦定DropDownList2
    建议把DropDownList1和DropDownList2的邦定分别写成一个方法,
    邦定时直接调用这个方法就可以了。
    还有。楼主用的是Vb语言吗?怎么我看到连接字符串用了+?
    我不懂vb,但是好像vb里连接字符串都是用&吧。