简体   繁体   English

ASP.NET C#具有回发功能的两个下拉列表使彼此的索引混乱

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

I'm testing a simple thing where I have a database of video cards and motherboards. 我正在测试一个简单的东西,我有一个视频卡和主板数据库。 The first DropDown List only pulls unique Brands 第一个下拉列表仅拉动独特的品牌

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

And the second dropdown list is populated by the first one. 第二个下拉列表由第一个填充。 The Model is the display field and the Power requirement is the value. 型号是显示字段,功率要求是值。

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

The session variable is set when the index of the first ddl is changed: 更改第一个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;
}

The problem is that when do the same as above, except for my motherboard information, the index of the Model/Power Requirement changes on any sort of click event or postback. 问题是,当执行与上述相同的操作时,除了我的主板信息外,型号/电源需求的索引会在任何类型的单击事件或回发时发生变化。 So if I pick EVGA and Model 5, and then go to my Motherboard ddls and pick ASUS and Model 4, the EVGA Model 5 selection will go to index 1 or something. 因此,如果我选择EVGA和Model 5,然后转到主板ddls并选择ASUS和Model 4,则EVGA Model 5的选择将进入索引1之类。 I've tried playing around with various if (!postback) statements but I just cant figure it out. 我尝试过各种if(!postback)语句,但是我无法弄清楚。

Place the second set of drop downs for the video card and model inside an update panel and it will work: 将视频卡和模型的第二组下拉菜单放在更新面板中,它将起作用:

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

相关问题 ASP.NET Webforms级联下拉列表-第二个列表的选定值在回发时重置 - ASP.NET Webforms cascading dropdown Lists - second list's selected value resets on postback 使用asp.net C#获取下拉列表的选定索引 - Getting selected Index of dropdown using asp.net C# ASP.NET VS2008 C#-下拉列表-回发 - ASP.NET VS2008 C# - dropdown list - postback 如何在回发后保持gridview上的颜色? asp.net c# - How to Keep colors on gridview after Postback? asp.net c# DataList ItemCommand在ASP.NET C#中让我感到困惑吗? - DataList ItemCommand Messing me in asp.net C#? ASP.NET C# 在每一行动态创建带有回发按钮的表 - ASP.NET C# Dynamically create table with postback button on each row 使用 IRepository 和彼此的 ASP.NET C# 服务 - ASP.NET C# Services using IRepository and each other 在表的每一行(ASP.NET/C#)上动态创建回发控件 - Dynamically recreating controls on postback for each row of table (ASP.NET/C#) ASP.NET / Webforms / C#-基于第一个下拉列表填充第二个下拉列表,而无需回发,绑定后的代码 - ASP.NET / Webforms / C# - populating 2nd dropdown based on 1st dropdown without postback with code behind binding ASP.NET填充在选定索引更改时互相引用的列表 - ASP.NET populate lists referencing each other on selected index change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM