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