DataTable未绑定到ASP:DropDownList

时间:2014-07-09 20:49:46

标签: asp.net vb.net dotnetnuke dropdownlistfor

我认为这段代码应该非常简单直接,但我遇到了问题。在页面加载时调用下面的子。它位于加载其他下拉列表的类似函数之间。剩下的所有下降都是填充,但这些不是。另一个dropdown subs使用相同的With子句但绑定到一个DataSet(一个表),该DataSet用对数据库的存储过程调用填充而不是绑定到静态DataTable。

VB代码:

   Private Sub LoadEquations()
            Dim dtMain As DataTable
            Dim dtGypsum As DataTable
            Dim dr As DataRow

            dtMain.Columns.Add("Text")
            dtMain.Columns.Add("Value")

            dr = dtMain.NewRow
            dr.Item("Text") = " "
            dr.Item("Value") = "0"
            dtMain.Rows.Add(dr)
            dtMain.AcceptChanges()

            dr = dtMain.NewRow
            dr.Item("Text") = "Build"
            dr.Item("Value") = "B"
            dtMain.Rows.Add(dr)
            dtMain.AcceptChanges()

            dr = dtMain.NewRow
            dr.Item("Text") = "Maintain"
            dr.Item("Value") = "M"
            dtMain.Rows.Add(dr)
            dtMain.AcceptChanges()

            With Me.ddMainEquation
                .DataSource = dtMain
                .DataTextField = "Text"
                .DataValueField = "Value"
                .DataBind()
            End With


            dtGypsum.Columns.Add("Text")
            dtGypsum.Columns.Add("Value")

            dr = dtGypsum.NewRow
            dr.Item("Text") = " "
            dr.Item("Value") = "0"
            dtGypsum.Rows.Add(dr)
            dtGypsum.AcceptChanges()

            dr = dtGypsum.NewRow
            dr.Item("Text") = "pH Base"
            dr.Item("Value") = "P"
            dtGypsum.Rows.Add(dr)
            dtGypsum.AcceptChanges()

            dr = dtGypsum.NewRow
            dr.Item("Text") = "% Sodium Base"
            dr.Item("Value") = "S"
            dtGypsum.Rows.Add(dr)
            dtGypsum.AcceptChanges()

            With Me.ddGypsumEquation
                .DataSource = dtGypsum
                .DataTextField = "Text"
                .DataValueField = "Value"
                .DataBind()
            End With
        End Sub

DNN / ASP代码:

<tr>
    <td>
        <asp:Label ID="lblMainEquation" runat="server" 
            CssClass="FormLabelLeft">Main Equation</asp:Label></td>
    <td>
        <asp:Label ID="lblGypsumEquation" runat="server" 
            CssClass="FormLabelLeft">Gypsum Equation</asp:Label></td>
</tr>
<tr>
    <td>
        <asp:DropDownList ID="ddMainEquation" TabIndex="34" 
            runat="server" CssClass="FormField"></asp:DropDownList></td>
    <td>
        <asp:DropDownList ID="ddGypsumEquation" TabIndex="34" 
            runat="server" CssClass="FormField"></asp:DropDownList></td>
</tr>

我错过了什么?我使用的是较旧的DNN平台2.0。

1 个答案:

答案 0 :(得分:1)

您应该将数据表声明为对象

尝试以下:

 Private Sub LoadEquations()
    Dim dtMain As New DataTable
    Dim dtGypsum As New DataTable
    Dim dr As DataRow

    dtMain.Columns.Add("Text")
    dtMain.Columns.Add("Value")

    dr = dtMain.NewRow
    dr.Item("Text") = " "
    dr.Item("Value") = "0"
    dtMain.Rows.Add(dr)

    dr = dtMain.NewRow
    dr.Item("Text") = "Build"
    dr.Item("Value") = "B"
    dtMain.Rows.Add(dr)

    dr = dtMain.NewRow
    dr.Item("Text") = "Maintain"
    dr.Item("Value") = "M"
    dtMain.Rows.Add(dr)

    With ddMainEquation
        .DataSource = dtMain
        .DataTextField = "Text"
        .DataValueField = "Value"
        .DataBind()
    End With

    dtGypsum.Columns.Add("Text")
    dtGypsum.Columns.Add("Value")

    dr = dtGypsum.NewRow
    dr.Item("Text") = " "
    dr.Item("Value") = "0"
    dtGypsum.Rows.Add(dr)

    dr = dtGypsum.NewRow
    dr.Item("Text") = "pH Base"
    dr.Item("Value") = "P"
    dtGypsum.Rows.Add(dr)

    dr = dtGypsum.NewRow
    dr.Item("Text") = "% Sodium Base"
    dr.Item("Value") = "S"
    dtGypsum.Rows.Add(dr)

    With ddGypsumEquation
        .DataSource = dtGypsum
        .DataTextField = "Text"
        .DataValueField = "Value"
        .DataBind()
    End With
End Sub

顺便说一句,添加新行后,每次都不需要 AcceptChanges()。添加新行后调用AcceptChanges实际上会将新添加的DataRow的DataRowState从Added更改为Unchanged。检查this