| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 编程论坛
共有 1168 人关注过本帖
标题:dropdownlist赋值问题
只看楼主 加入收藏
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:19
帖 子:1761
专家分:3642
注 册:2011-3-24
结帖率:98.8%
  问题点数:0  回复次数:3   
dropdownlist赋值问题
<asp:DropDownList ID="depNo" runat="server" Width="160px" AutoPostBack="True" />
要怎么把MSSQL的值塞进depNo这个控件里?

SQL查询命令:
Dim mySQL As String = "SELECT DepartmentNo, DepartmentName FROM tblMstDepartment Where DepartmentNo Like '72%' And DeptLevelNo In (50, 80) Order By DepartmentNo"
2017-05-25 18:22
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:19
帖 子:1761
专家分:3642
注 册:2011-3-24
  得分:0 
程序代码:

Private Sub GetDepNo()
        Dim i As Integer = 0
        Dim mySQL As String = "SELECT DepartmentNo, DepartmentName FROM tblMstDepartment Where DepartmentNo Like '72%' And DeptLevelNo In (50, 80) Order By DepartmentNo"
        Try
            Using mySqlDS As SqlDataSource = New SqlDataSource
                mySqlDS.ProviderName = strDbType
                mySqlDS.ConnectionString = WebConfigurationManager.ConnectionStrings(strCnnType & "DbBTA").ConnectionString
                mySqlDS.SelectCommand = mySQL
                mySqlDS.DataSourceMode = SqlDataSourceMode.DataSet
                Dim my_args As New DataSourceSelectArguments
                Dim my_DV As Data.DataView = mySqlDS.Select(my_args)
                For i = 0 To my_DV.Count - 1 Step 1
                    depNo.Attributes.Add("", "") '???这边要怎么写???
                Next i
            End Using
        Catch ex As Exception
            Dim strErr As String = "●●● 程式错误讯息如下 ●●●" & "\n\n" & Replace(Replace(Replace(Replace(Trim(ex.ToString()), Chr(10), ""), Chr(13), ""), "\", "\\"), "'", "\'")
            ScriptManager.RegisterStartupScript(Me, Me.GetType(), "alert", "alert('" & strErr & "');", True)
        End Try
    End Sub

不要選我當版主
2017-05-25 19:11
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:19
帖 子:1761
专家分:3642
注 册:2011-3-24
  得分:0 
自己找出来了...

程序代码:

Private Sub GetDepNo()
        Dim dbCmdSQL As SqlCommand
        Dim mySQL As String = "SELECT DepartmentNo, DepartmentName FROM tblMstDepartment Where DepartmentNo Like '72%' And DeptLevelNo In (50, 80) Order By DepartmentNo"
        depNo.Items.Clear()
        depNo.Items.Add(New ListItem(""))
        Try
            Using mySqlDS = New SqlConnection(ConfigurationManager.ConnectionStrings(strCnnType & "DbBTA").ToString)
                dbCmdSQL = New SqlCommand(mySQL, mySqlDS)
                mySqlDS.Open()
                Using dbDRSQL = dbCmdSQL.ExecuteReader
                    While dbDRSQL.Read
                        Dim str As String = dbDRSQL("DepartmentNo").ToString & " " & dbDRSQL("DepartmentName").ToString
                        depNo.Items.Add(New ListItem(str))
                    End While
                End Using
            End Using
        Catch ex As Exception
            Dim strErr As String = "●●● 程式错误讯息如下 ●●●" & "\n\n" & Replace(Replace(Replace(Replace(Trim(ex.ToString()), Chr(10), ""), Chr(13), ""), "\", "\\"), "'", "\'")
            ScriptManager.RegisterStartupScript(Me, Me.GetType(), "alert", "alert('" & strErr & "');", True)
        End Try
    End Sub

不要選我當版主
2017-05-26 10:58
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:19
帖 子:1761
专家分:3642
注 册:2011-3-24
  得分:0 
连动也OK了...

程序代码:

Protected Sub depNo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles depNo.SelectedIndexChanged
        Dim DepNo As String = sender.SelectedValue : DepNo = Mid(DepNo, 1, 4)
        Dim dbCmdSQL As SqlCommand
        Dim mySQL As String
        UserNo.Items.Clear() : UserNo.Items.Add(New ListItem(""))
        ClassNo.Items.Clear() : ClassNo.Items.Add(New ListItem(""))
        Try
            mySQL = "SELECT EmployeeNo, EmployeeName FROM tblMstEmployee Where DepartmentNo = '" & DepNo & "' AND Active = 'Y'"
            Using mySqlDS = New SqlConnection(ConfigurationManager.ConnectionStrings(strCnnType & "DbBTA").ToString)
                dbCmdSQL = New SqlCommand(mySQL, mySqlDS)
                mySqlDS.Open()
                Using dbDRSQL = dbCmdSQL.ExecuteReader
                    While dbDRSQL.Read
                        Dim str As String = dbDRSQL("EmployeeName").ToString & " (" & dbDRSQL("EmployeeNo").ToString & ")"
                        UserNo.Items.Add(New ListItem(str))
                    End While
                End Using
            End Using

            mySQL = "SELECT ClassID, SubClassID, FileID FROM tblClassList"
            Using mySqlDS = New SqlConnection(ConfigurationManager.ConnectionStrings(strCnnType & "DbBTA").ToString)
                dbCmdSQL = New SqlCommand(mySQL, mySqlDS)
                mySqlDS.Open()
                Using dbDRSQL = dbCmdSQL.ExecuteReader
                    While dbDRSQL.Read
                        Dim str As String = GetClassName(0, dbDRSQL("ClassID").ToString, dbDRSQL("SubClassID").ToString, dbDRSQL("FileID").ToString)
                        ClassNo.Items.Add(New ListItem(str))
                    End While
                End Using
            End Using
        Catch ex As Exception
            Dim strErr As String = "●●● 程式错误讯息如下 ●●●" & "\n\n" & Replace(Replace(Replace(Replace(Trim(ex.ToString()), Chr(10), ""), Chr(13), ""), "\", "\\"), "'", "\'")
            ScriptManager.RegisterStartupScript(Me, Me.GetType(), "alert", "alert('" & strErr & "');", True)
        End Try
    End Sub

不要選我當版主
2017-05-26 11:33







关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.078180 second(s), 9 queries.
Copyright©2004-2018, BCCN.NET, All Rights Reserved