ASP.Net下拉列表不会重新显示我选择的选项

时间:2015-07-31 07:37:56

标签: html asp.net webforms

我有一个4页的ASP.NET表单,它在会话中存储数据。所有这一切都运行正常但我在查看用户初始从我的下拉列表中选择的值时遇到问题。

第一页HTML使用DropDownList

<asp:DropDownList ID="ddlInnoc" runat="server" class="form-control">
     <asp:ListItem Value="0">- - Please Select - -</asp:ListItem>
     <asp:ListItem Value="Male">Male</asp:ListItem>
     <asp:ListItem Value="Female">Female</asp:ListItem>
</asp:DropDownList>

重新显示所选值的第一页代码

protected void Page_Load(object sender, EventArgs e)
    {
        if (txtData1.Text == string.Empty && Session["pg1input"] != null)
        {
            txtData1.Text = Session["pg1input"].ToString();
        }

        if (ddlInnoc.SelectedValue == string.Empty && Session["pg1dd"] != null)
        {
            ddlInnoc.SelectedValue = Session["pg1dd"].ToString();
        }
    }

    protected void pg1button_Click(object sender, EventArgs e)
    {
        Session["pg1input"] = txtData1.Text;
        Session["pg1dd"] = ddlInnoc.SelectedValue;
        Response.Redirect("/Session/pg2.aspx");
    }

Page 3 HTML代码

            <div class="form-group">
                <div class="col-xs-12">
                    <asp:Label ID="Label1" class="col-md-2 control-label" runat="server" Text="Name:"></asp:Label>
                    <div class="col-md-3 form-control-static">
                        <%=Session["pg1input"] %>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-12">
                    <asp:Label ID="Label2" class="col-md-2 control-label" runat="server" Text="Sex:"></asp:Label>
                    <div class="col-md-3 form-control-static">
                        <%=Session["pg1dd"] %>
                    </div>
                </div>
            </div>
<div class="row">
     <div class="col-sm-offset-4 col-xs-12">
          <asp:LinkButton ID="pg1EditButton" runat="server" OnClick="pg1EditButton_Click" CssClass="btn btn-default">
               <span aria-hidden="true" class="glyphicon glyphicon-pencil"></span> Edit
          </asp:LinkButton>
     </div>
</div>

Page 3编辑按钮代码

protected void pg1EditButton_Click(object sender, EventArgs e)
{
    Response.Redirect("/Session/pg1.aspx");
}

2 个答案:

答案 0 :(得分:0)

我认为条件if (ddlInnoc.SelectedValue == string.Empty是不正确的,因为ddlInnoc.SelectedValue的初始值将为0而不是String.Empty..try将初始值检查为“0”

答案 1 :(得分:0)

要解决此问题,请添加

if (ddlInnoc.SelectedValue != "0" && Session["pg1dd"] != null)
{
     ddlInnoc.SelectedValue = Session["pg1dd"].ToString();
}