簡體   English   中英

ASP.NET C#具有回發功能的兩個下拉列表使彼此的索引混亂

[英]ASP.NET C# Two DropDown Lists with postback keep messing up each other's Index

我正在測試一個簡單的東西,我有一個視頻卡和主板數據庫。 第一個下拉列表僅拉動獨特的品牌

<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>

第二個下拉列表由第一個填充。 型號是顯示字段,功率要求是值。

<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和Model 5,然后轉到主板ddls並選擇ASUS和Model 4,則EVGA Model 5的選擇將進入索引1之類。 我嘗試過各種if(!postback)語句,但是我無法弄清楚。

將視頻卡和模型的第二組下拉菜單放在更新面板中,它將起作用:

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM