ASP.NET DropDownList 发布“”

发布于 2024-09-03 21:15:56 字数 1792 浏览 2 评论 0原文

我在 VB 中使用 ASP.NET 表单版本 3.5

我有一个下拉列表,其中填充了来自数据库的数据以及国家/地区列表

下拉列表的代码

         <label class="ob_label">
            <asp:DropDownList ID="lstCountry" runat="server" CssClass="ob_forminput">
            </asp:DropDownList>
         Country*</label>

是 列表的代码是

 Dim selectSQL As String = "exec dbo.*******************"

    ' Define the ADO.NET objects.
    Dim con As New SqlConnection(connectionString)
    Dim cmd As New SqlCommand(selectSQL, con)
    Dim reader As SqlDataReader

    ' Try to open database and read information.
    Try
        con.Open()
        reader = cmd.ExecuteReader()

        ' For each item, add the author name to the displayed
        ' list box text, and store the unique ID in the Value property.

        Do While reader.Read()
            Dim newItem As New ListItem()
            newItem.Text = reader("AllSites_Countries_Name")
            newItem.Value = reader("AllSites_Countries_Id")
            CType(LoginViewCart.FindControl("lstCountry"), DropDownList).Items.Add(newItem)
        Loop
        reader.Close()

        CType(LoginViewCart.FindControl("lstCountry"), DropDownList).SelectedValue = 182

    Catch Err As Exception
        Response.Redirect("~/error-on-page/")

        MailSender.SendMailMessage("*********************", "", "", OrangeBoxSiteId.SiteName & " Error Catcher", "<p>Error in sub FillCountry</p><p>Error on page:" & HttpContext.Current.Request.Url.AbsoluteUri & "</p><p>Error details: " & Err.Message & "</p>")
        Response.Redirect("~/error-on-page/")
    Finally
        con.Close()
    End Try

提交表单时发生错误这表示字符串“”无法转换为数据类型整数。由于某种原因,下拉列表显示“”而不是所选国家/地区的值。

I am using ASP.NET forms version 3.5 in VB

I have a dropdownlist that is filled with data from a DB with a list of countries

The code for the dropdown list is

         <label class="ob_label">
            <asp:DropDownList ID="lstCountry" runat="server" CssClass="ob_forminput">
            </asp:DropDownList>
         Country*</label>

And the code that the list is

 Dim selectSQL As String = "exec dbo.*******************"

    ' Define the ADO.NET objects.
    Dim con As New SqlConnection(connectionString)
    Dim cmd As New SqlCommand(selectSQL, con)
    Dim reader As SqlDataReader

    ' Try to open database and read information.
    Try
        con.Open()
        reader = cmd.ExecuteReader()

        ' For each item, add the author name to the displayed
        ' list box text, and store the unique ID in the Value property.

        Do While reader.Read()
            Dim newItem As New ListItem()
            newItem.Text = reader("AllSites_Countries_Name")
            newItem.Value = reader("AllSites_Countries_Id")
            CType(LoginViewCart.FindControl("lstCountry"), DropDownList).Items.Add(newItem)
        Loop
        reader.Close()

        CType(LoginViewCart.FindControl("lstCountry"), DropDownList).SelectedValue = 182

    Catch Err As Exception
        Response.Redirect("~/error-on-page/")

        MailSender.SendMailMessage("*********************", "", "", OrangeBoxSiteId.SiteName & " Error Catcher", "<p>Error in sub FillCountry</p><p>Error on page:" & HttpContext.Current.Request.Url.AbsoluteUri & "</p><p>Error details: " & Err.Message & "</p>")
        Response.Redirect("~/error-on-page/")
    Finally
        con.Close()
    End Try

When the form is submitted an error occurs which says that the string "" cannot be converted to the datatype integer. For some reason the dropdownlist is posting "" rather than the value for the selected country.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虚拟世界 2024-09-10 21:15:56

我能想到的有两件事。

1) 您确定 SQL 返回的值是 182 吗?

2)您是否正在重建回发下拉列表?因为它是动态的,所以你也会有太多,否则它不会知道你选择了哪个值。

2 things I can think of.

1) Are you sure the SQL is returning something that has the value of 182 ?

2) Are you rebuilding the Drop-down list on Postback ? as it's dynamic you'll have too else it won't know which value you selected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文