下拉值未在ViewState

时间:2016-11-25 06:24:18

标签: c# asp.net viewstate

我已经在db和viewstate中保存了下拉列表值,但是在页面加载时它没有被加载。 有人可以告诉我哪里出错了

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();
        }
}

1 个答案:

答案 0 :(得分:0)

确保在页面加载时首先使用值填充下拉列表,然后使用以下条件:

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