在DDL获得deafult值之后,不能改变选择

时间:2016-11-15 06:43:23

标签: asp.net webforms

我有一个基于数据的下拉列表,默认选择由用户会话设置。它工作正常,但如果我尝试在列表上选择一个不同的项目,它会跳回到默认值。我正在使用visual studio 2015,web form,asp.net

我尝试以2种方式选择默认项目,但仍然存在同样的问题

if (Session["plyer2"] != null) {

        string DropDownListSelected = Session["plyer1"].ToString();
        DropDownList1.ClearSelection();
        DropDownList1.Items.FindByValue(DropDownListSelected).Selected = true;
        DropDownList2.SelectedValue = Session["plyer2"].ToString(); }

过去2天一直在谷歌搜索没有找到帮助

DDL是autopostback

代码位于.aspx.cs文件中:protected void Page_Load(object sender, EventArgs e)

数据绑定在.aspx文件中

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource5" DataTextField="forshow" DataValueField="forshow" AppendDataBoundItems="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                <asp:ListItem Value="1">בחר גארד</asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource5" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [forshow] FROM [players] WHERE ([position] = @position)">
                <SelectParameters>
                    <asp:QueryStringParameter DefaultValue="גארד" Name="position" QueryStringField="גארד" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>

1 个答案:

答案 0 :(得分:0)

这不是问题的真正答案,但由于我使用会话选择项目,我添加

            Session.Abandon();

所以它取消IF语句下一页加载,这对我来说已经足够了

相关问题