简体   繁体   中英

Dropdown value is not getting save in ViewState

I have saved the dropdown value in db and in the viewstate, but on page load it is not getting loaded. Can someone please tell me where am I going wrong

protected void Page_Load(object sender, System.EventArgs e)
{
if (ViewState["ddSelectedValue"] != "0")
        {
            dd_RuleFor.SelectedValue = Convert.ToInt32(ViewState["ddSelectedValue"]).ToString();
        }
}

protected void btn_Save_Click(object sender, System.EventArgs e)
{
     if(chkRuleApprover.Checked == true && dd_RuleFor.SelectedValue != "0")
        {

            strSQL = "update UserFile  set Rule_Approval_Selection=" + dd_RuleFor.SelectedItem.Value  + " where UID=" + intUserID;
            objCmd = new System.Data.SqlClient.SqlCommand(strSQL, oconn);
            objCmd.ExecuteNonQuery();

            ViewState["ddSelectedValue"] = dd_RuleFor.SelectedValue.ToString();
        }
}

Make sure you are filling the dropdown with the values first on pageload and after than use below condition:

if(!Page.Ispostback())
{
if (ViewState["ddSelectedValue"] != "0")
    {
        dd_RuleFor.SelectedValue = Convert.ToInt32(ViewState["ddSelectedValue"]).ToString();
    }
}

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