【问题标题】:I am having trouble getting my listbox to populate with the output from a sql query我无法让我的列表框填充 sql 查询的输出
【发布时间】:2013-02-15 21:53:03
【问题描述】:

我似乎无法弄清楚我在哪里出错了。我有两个列表框,第一个从 sql 服务器上的存储过程中提取数据。 the second list box is supposed to populate when the an item in the first list box is selected.当第一个列表框中的项目被单击时,第二个列表框的存储过程应该传递所选项目的文本。问题是第二个列表框没有填充。如果有任何有用的反馈,或者可能是一种更简单的方式来完成我正在尝试做的事情,我将不胜感激。

ASP.NET:

<asp:ListBox ID="ListBox1" runat="server" DataSourceID="LOCATION" DataTextField="L_Name" DataValueField="L_Name" AutoPostBack="True"></asp:ListBox>

<asp:SqlDataSource ID="LOCATION" runat="server" ConnectionString="<%$ ConnectionStrings:SAMC_2ConnectionString %>" SelectCommand="L_Get" SelectCommandType="StoredProcedure"></asp:SqlDataSource>

<asp:ListBox ID="ListBox2" runat="server" Height="150px" Width="200px" AutoPostBack="True" DataTextField="C_Name" DataValueField="C_Name" />

<asp:SqlDataSource ID="CompByLocal" runat="server" ConnectionString="<%$ ConnectionStrings:SAMC_2ConnectionString %>" SelectCommand="L_Get_C" SelectCommandType="StoredProcedure">    

<SelectParameters>
<asp:ControlParameter ControlID="ListBox1" DefaultValue="" Name="L_Name" PropertyName="SelectedValue" Type="String" />
<asp:Parameter Name="L_ID" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

VB.NET:

Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim val As String = ListBox1.Items(ListBox1.SelectedIndex).ToString
        TextBox1.Text = val
        ListBox2.Items.Clear()
        ListBox2.DataSource = CompByLocal
        ListBox2.DataBind()
    End Sub

【问题讨论】:

  • 出于好奇;如果你在你的代码后面设置一个断点,它会被命中吗?
  • @RandomWebGuy 如果你将断点放在像int i = 0; 这样的代码行上,它会。
  • @rcdmk 什么?对不起,也许我需要更清楚。如果您在 SelectedIndexChange 方法上设置断点,它会在下拉更改时调用吗?
  • @RandomWebGuy 如果您在调试模式下启动浏览器,是的。
  • @user2077048 提示:尝试使用一些有意义的变量、方法和过程名称。这将使您更专业地查看您的代码,并且任何试图了解您在做什么的人都会更清楚。即使是你一年后回来的时候。

标签: asp.net sql-server vb.net


【解决方案1】:

我的建议是放弃 SqlDataSources 并在后端完成所有操作 - 然后根据我的经验,您可以更准确地了解事情发生的时间。我已尽力在下面编写必要的代码,但我的母语是 C# - 我使用了在线转换器,因此请原谅任何小的语法错误。

ASPX:

<asp:ListBox  ID="ListBox1" runat="server"  DataTextField="L_Name" DataValueField="L_Name" AutoPostBack="True"></asp:ListBox>

<asp:ListBox ID="ListBox2" runat="server" Height="150px" Width="200px" AutoPostBack="True" DataTextField="C_Name" DataValueField="C_Name" />

.VB

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
if (IsPostback) return
Dim results As New DataTable()
Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("SAMC_2ConnectionString "))
    connection.Open()
    Using command As New SqlCommand("L_Get", connection)
        command.CommandType = CommandType.StoredProcedure
        results.Load(command.ExecuteReader())
    End Using
End Using
ListBox1.DataSource = results;
ListBox1.DataBind();
End Sub


Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) 
Dim results As New DataTable()
Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("SAMC_2ConnectionString "))
    connection.Open()
    Using command As New SqlCommand("L_Get_C", connection)
        command.CommandType = CommandType.StoredProcedure
        command.Parameters.AddWithValue("L_ID",ListBox1.SelectedValue)
        results.Load(command.ExecuteReader())
    End Using
End Using
ListBox2.DataSource = results
ListBox2.DataBind()
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多