简体   繁体   中英

Dropdownlist how to save selected value to ViewState?

Dropdownlist how to save selected value to ViewState ?

I have a dropdownlist that when changed will save the new value into a ViewState variable, so that after a postback, the dropdownlist will retrieve it's selected value from ViewState if previously set.

But I can't store the selected value in DropDownList1_SelectedIndexChanged to ViewState because when reload page (after update database with sql query update) in DropDownList I don't have any selected value.

Can you please help me figure out the problem?

Thanks in advance.

My code below.

<asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true"
 OnSelectedIndexChanged="ddl1_SelectedIndexChanged" 
 EnableViewState="true" ViewStateMode="Enabled">
    <asp:ListItem Value="" Text="[Selected]"></asp:ListItem>
    <asp:ListItem Value="" Text="------"></asp:ListItem>
    <asp:ListItem Value="1" Text="A"> </asp:ListItem>
    <asp:ListItem Value="2" Text="B"> </asp:ListItem>
    <asp:ListItem Value="3" Text="C"> </asp:ListItem>
    <asp:ListItem Value="4" Text="D"> </asp:ListItem>
    <asp:ListItem Value="" Text="------"></asp:ListItem>
</asp:DropDownList>



protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        BindData();

        if (ViewState["List1_Value"] != null)
        {
            ddl1.SelectedValue = ViewState["List1_Value"].ToString();
        }
    }
}


protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
    BindData();
    ViewState["List1_Value"] = ddl1.SelectedValue.ToString();
}

private void SqlUpdate()
{

  sql = String.Format(@" UPDATE ..... ";

    using (OdbcConnection cn =
        new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
    {
        using (OdbcCommand cmd = new OdbcCommand(sql, cn))
        {
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('Ok.');window.location='default.aspx';", true);
        }
    }
}

What you're doing is a postback from Javascript , in this case the ViewState is deleted as it is as if it were a new request.

In this case the only thing is to store it in session or send as a parameter to the page.

At last after update database simply binding a GridView it's easy !:

    cmd.Connection.Open();
    cmd.ExecuteNonQuery();
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('Ok.');", true); 
    BindData();  

You have AutoPostBack enabled, so every time the selection change it posts back to your Page_Load. You are only filling out the value when it is not a postback, so this will never occur.

Try moving the code to set the value outside of the if (!Page.IsPostBack) block. Maybe something like so:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        BindData();
    }

    if (ViewState["List1_Value"] != null)
    {
        ddl1.SelectedValue = ViewState["List1_Value"].ToString();
    }
}

Remove BindData() from your SelectedIndexChanged method:

protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
    ViewState["List1_Value"] = ddl1.SelectedValue.ToString();
}

And as others have mentioned, you attempt to set ddl1.SelectedValue inside a condition that only runs in NOT postback, which means it will never capture the selected value on postback. So you will also want to address that by moving it outside the !Page.IsPostBack condition.

when reload page (after update database with sql query update) in DropDownList I don't have any selected value

Presumably you're "reloading" in one of two ways:

1. Causing a Post-Back

This won't work in your case because you're not using the value in a post-back:

if (!Page.IsPostBack)
{
    BindData();

    if (ViewState["List1_Value"] != null)
    {
        ddl1.SelectedValue = ViewState["List1_Value"].ToString();
    }
}

If you want to bind the selected value in a post-back then you need to move that code outside of that conditional:

if (!Page.IsPostBack)
{
    BindData();
}
if (ViewState["List1_Value"] != null)
{
    ddl1.SelectedValue = ViewState["List1_Value"].ToString();
}

2. Making a New Request

If you're manually "reloading" the page by simply re-requesting the page from the browser's address bar, then the ViewState is going to be empty. ViewState is part of the form values in a POST request, but loading a page from the address bar issues a GET request with no values. So the ViewState will be empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM