ASP.Net My Updated Value From Drop Down List Doesn

2019-09-11 02:28发布

问题:

I have a 4 page ASP.NET form which is storing data in the session. A button on my 3rd page clears the session. All this is working fine but I'm having a problem updating a value on my confirmation page as it retains/displays my initial selected value and i cant figure out why.

1st Page HTML With 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>

1st Page Code Behind Which Re-displays Selected Value

protected void Page_Load(object sender, EventArgs e)
    {
        txtData1.Focus();

        if (txtData1.Text == string.Empty && Session["pg1input"] != null)
        {
            txtData1.Text = Session["pg1input"].ToString();
        }

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

        //if (Session["pg1dd"].ToString() == "")
        //{
        //    ddlInnoc.SelectedValue = Session["pg1dd"].ToString();
        //}
    }

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

Page 3 HTML Code

            <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 Edit Button Code Behind

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

I suspect that its something to do with the code behind on my page 1 as all the other fields/radio buttons on the other pages retrieves and displays the updated value on my 3rd page.

回答1:

You are setting it back to the value in your session on postback.

Add this to your Page_Load.

if (!IsPostBack)
{
    //set values from session
}


回答2:

Fix is

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