2016-06-20 3 views
0

私はビデオカードとマザーボードのデータベースを持っている簡単なことをテストしています。最初のドロップダウンリストはユニークなブランドのみを引き出しますASP.NET C#2つのドロップダウンリストでポストバックがお互いのインデックスを混乱させ続ける

<asp:DropDownList ID="ddlfirst" runat="server" DataSourceID="SqlDataSource1" DataTextField="Brand" DataValueField="Brand" OnSelectedIndexChanged="ddlfirst_SelectedIndexChanged" AutoPostBack="True"> 
    </asp:DropDownList> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HardwareConnectionString %>" SelectCommand="SELECT DISTINCT [Brand] FROM [Computer_Motherboards]"></asp:SqlDataSource> 

2番目のドロップダウンリストには最初のドロップダウンリストが表示されます。モデルは表示フィールドで、電力要件は値です。

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:HardwareConnectionString %>" SelectCommand="SELECT DISTINCT [Brand], [Model], [PowerDraw] FROM [Computer_Motherboards] where Brand = @SessionVar"> 
    <SelectParameters> 
      <asp:SessionParameter Name="SessionVar" SessionField="Mobo_Brand_Selection" ConvertEmptyStringToNull="true" /> 
     </SelectParameters> 
    </asp:SqlDataSource> 

最初のDDLのインデックスが変更されたときにセッション変数が設定されている:

protected void ddlfirst_SelectedIndexChanged(object sender, EventArgs e) 
{ 
      //sets this session variable to whatever the selected brand is 
      Session["Mobo_Brand_Selection"] = ddlfirst.SelectedValue; 
} 

の問題は、上記と同じ操作を行うとき、私のマザーボードの情報のインデックスを除き、任意の種類のクリックイベントまたはポストバックに対してモデル/消費電力要件が変更されます。 EVGAとモデル5を選択してからマザーボードのddlsに移動し、ASUSとモデル4を選択すると、EVGAモデル5の選択はインデックス1などになります。私は様々なif(!postback)ステートメントで遊んでみましたが、私はそれを理解できません。

+0

は、あなたがポストバック後に予想されるものを第一または第二のではないでしょうか?あなたの問題をよりよく説明してください。 – Seano666

+0

[例イメージ](http://i.imgur.com/wjMCQAQ.jpg) 私が意味することとどのドロップダウンリストが影響を受けるかのスクリーンショットを撮りました。 – Aeternus

+0

あなたの問題があります。ページのページロードイベントにドロップダウンリストを設定していますか? –

答えて

0

置き更新パネル内のビデオカードとモデルのためのドロップダウンの第二セットと、それは動作します:ドロップダウン

<asp:DropDownList ID="ddlfirst" runat="server" DataSourceID="SqlDataSource1" DataTextField="Brand" DataValueField="Brand" AutoPostBack="True"> 
</asp:DropDownList> 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HardwareConnectionString %>" SelectCommand="SELECT DISTINCT [Brand] FROM [Computer_Motherboards]"></asp:SqlDataSource> 


<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="Model" DataValueField="PowerDraw"> 
</asp:DropDownList> 
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:HardwareConnectionString %>" SelectCommand="SELECT DISTINCT [Brand], [Model], [PowerDraw] FROM [Computer_Motherboards] where Brand = @Brand"> 
    <SelectParameters> 
     <asp:ControlParameter ControlID="ddlfirst" PropertyName="SelectedValue" Name="Brand" /> 
    </SelectParameters> 
</asp:SqlDataSource> 

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> 
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <ContentTemplate> 
     <asp:DropDownList runat="server" ID="videoCardBrand" AutoPostBack="true"> 
      <asp:ListItem Text="Video card one" Value="1" /> 
      <asp:ListItem Text="Video card two" Value="2" /> 
      <asp:ListItem Text="Video card 3" Value="3" /> 
     </asp:DropDownList> 
     <asp:DropDownList runat="server" ID="videoCardModel" AutoPostBack="true"> 
      <asp:ListItem Text="Video card model1" Value="1" /> 
      <asp:ListItem Text="Video card model2" Value="2" /> 
      <asp:ListItem Text="Video card model3" Value="3" /> 
     </asp:DropDownList> 
    </ContentTemplate> 
</asp:UpdatePanel> 
+0

素晴らしいです、ありがとうございます。作業を確認し、解決策としてマークします。 – Aeternus

関連する問題