Visual Basic ASP.NET 4 Request.Form()

时间:2014-10-20 08:45:33

标签: asp.net visual-studio

这可能听起来像一个愚蠢的问题,但我正在学习Visual Basic ASP.NET 4,并且我被要求创建一个DropDownList,它使用SQL从表中获取信息。我似乎无法从下拉列表中提取所选项目并使用Request.Form在屏幕上显示。

我试过了..

Dim selItem As String = Request.Form("DDList")
Response.Write(selItem)

但这并没有在屏幕上显示任何内容。请帮忙,因为我正在努力解决这个问题。

我添加了我的下拉列表,如下所示:

<asp:DropDownList ID="DDList" runat="server" Width="201px">
    </asp:DropDownList>
    &nbsp;
    <asp:DropDownList ID="DDList2" runat="server" Width="145px">
    </asp:DropDownList>

我的班级/职能

Public Class sqlFunc

Public Shared Function tableData() As DataSet

    Dim oraConnect As New OracleConnection
    oraConnect.ConnectionString = ConfigurationManager.ConnectionStrings("smart_dev").ConnectionString

    Dim oraCommand As New OracleCommand

    oraCommand.Connection = oraConnect
    oraCommand.CommandType = Data.CommandType.Text

    Dim lsSQL As String = ""
    lsSQL = "SELECT code, description FROM ref_code WHERE domain = 'SPECIALTY'"

    oraCommand.CommandText = lsSQL

    Dim da As New OracleDataAdapter(oraCommand)
    Dim ds As New DataSet
    da.Fill(ds)

    Return ds
End Function

我在.aspx.vb页面上使用的内容;

Dim dsData1 As New DataSet
        dsData1 = tableData()


        DDList.DataSource = dsData1
        DDList.DataValueField = "code"
        DDList.DataTextField = "description"
        DDList.DataBind()
        DDList.Items.Insert(0, New ListItem(String.Empty, String.Empty))
        DDList.SelectedIndex = 0

1 个答案:

答案 0 :(得分:0)

如果您使用的是asp:DropDown ASP.net控件,则不需要使用Request.Form获取所选值。您所要做的就是:

Dim selItem As String = DDList.SelectedValue
Response.Write(selItem)

如果你真的想使用Request.Form,请这样做:

Dim selItem As String = Request.Form(DDList.ClientID)
Response.Write(selItem)

此外,请确保启用了ViewState,并且在有PostBack时不要在PageLoad重新绑定DropDown:

Sub Page_Load
    If Not IsPostBack
        ' Bind your DropDown here
    End If
End Sub