2

ここでどこが間違っていたのかわからないようです。2つのリストボックスがあります。最初のリストボックスは、SQLサーバー上のストアドプロシージャからデータを取得します。2番目のリストボックスは、最初のリストボックスの項目が選択されたときに表示されることになっています。2番目のリストボックスのストアドプロシージャには、最初のリストボックスのアイテムがクリックされたときに、選択したアイテムのテキストが渡される必要があります。問題は、その2番目のリストボックスにデータが入力されていないことです。有益なフィードバック、または私がやろうとしていることを実行するためのより簡単な方法をいただければ幸いです。

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
4

1 に答える 1

2

私のアドバイスは、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
于 2013-02-15T22:33:08.980 に答える