如何检查视图状态是否存在于asp.net 4.0中

本文关键字:asp net 存在 是否 何检查 检查 视图状态 | 更新日期: 2023-09-27 18:05:47

我只是用asp.net 4.0实现一个可编辑的网格视图。现在我用下面的代码设置ViewState为只存储这个文档:

 protected void DDL_Types1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender as DropDownList;
        GridViewRow row = ddl.Parent.Parent as GridViewRow;
        if (!ddl.SelectedItem.Value.Equals("-1"))
        {
            ViewState["type_id"] = ddl.SelectedItem.Value.ToString();
        }
    }
    protected void DDL_StateNames1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender as DropDownList;
        GridViewRow row = ddl.Parent.Parent.Parent.Parent as GridViewRow;
        if (!ddl.SelectedItem.Value.Equals("-1"))
        {
            ViewState["state_id"] = ddl.SelectedItem.Value.ToString();
        }
    }
    protected void chk_UserOFC1_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = (CheckBox)sender as CheckBox;
        GridViewRow row = chk.Parent.Parent.Parent.Parent as GridViewRow;
        ViewState["UserOFC"] = chk.Checked.ToString();
    }
    protected void chk_UserVAT1_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = (CheckBox)sender as CheckBox;
        GridViewRow row = chk.Parent.Parent.Parent.Parent as GridViewRow;
        ViewState["UserVAT"] = chk.Checked.ToString();
    }
    protected void chk_UserINV1_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = (CheckBox)sender as CheckBox;
        GridViewRow row = chk.Parent.Parent.Parent.Parent as GridViewRow;
        ViewState["UserINV"] = chk.Checked.ToString();
    }
    protected void chk_UserNone1_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = (CheckBox)sender as CheckBox;
        GridViewRow row = chk.Parent.Parent.Parent.Parent as GridViewRow;
        ViewState["UserNone"] = chk.Checked.ToString();
    }

现在在网格视图行更新我必须检查ViewState是否存在,因为当用户不选择它或勾选它的复选框值,然后它没有访问。

这是我的网格视图行更新代码:

protected void  GV_ViewCustomers_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GV_ViewCustomers.Rows[e.RowIndex];
        int customerid = Convert.ToInt32(GV_ViewCustomers.DataKeys[e.RowIndex].Value.ToString());
        if (!string.IsNullOrEmpty(e.NewValues["Name"].ToString()) && !ViewState["type_id"].Equals("-1"))
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                string owname = null, ownmono = null, room = null, build = null, road = null, area = null, city = null, mobile = null, phone = null, email = null, cpname = null, cpmono = null, remark = null;
                bool? UserOFC = null, UserVAT = null, UserINV = null, UserNone = null;
                int? country_id = null, state_id = null;
                if (!string.IsNullOrEmpty(e.NewValues["Ownername"].ToString().Trim()))
                {
                    owname = e.NewValues["Ownername"].ToString().Trim();
                }
                if (!string.IsNullOrEmpty(e.NewValues["Ownermob"].ToString().Trim()))
                {
                    ownmono = e.NewValues["Ownermob"].ToString().Trim();
                }
                if (!string.IsNullOrEmpty(e.NewValues["ContactPerson"].ToString().Trim()))
                {
                    cpname = e.NewValues["ContactPerson"].ToString().Trim();
                }
                if (!string.IsNullOrEmpty(e.NewValues["ContactPersonmob"].ToString().Trim()))
                {
                    cpmono = e.NewValues["ContactPersonmob"].ToString().Trim();
                }
                if (!string.IsNullOrEmpty(e.NewValues["Phone"].ToString().Trim()))
                {
                    phone = e.NewValues["Phone"].ToString().Trim();
                }
                if (!string.IsNullOrEmpty(e.NewValues["Mobile"].ToString().Trim()))
                {
                    mobile = e.NewValues["Mobile"].ToString().Trim();
                }
                if (!string.IsNullOrEmpty(e.NewValues["Room"].ToString().Trim()))
                {
                    room = e.NewValues["Room"].ToString().Trim();
                }
                if (!string.IsNullOrEmpty(e.NewValues["Build"].ToString().Trim()))
                {
                    build = e.NewValues["Build"].ToString().Trim();
                }
                if (!string.IsNullOrEmpty(e.NewValues["Road"].ToString().Trim()))
                {
                    road = e.NewValues["Road"].ToString().Trim();
                }
                if (!string.IsNullOrEmpty(e.NewValues["Area"].ToString().Trim()))
                {
                    area = e.NewValues["Area"].ToString().Trim();
                }
                if (!string.IsNullOrEmpty(e.NewValues["City"].ToString().Trim()))
                {
                    city = e.NewValues["City"].ToString().Trim();
                }
                if (!ViewState["country_id"].Equals("-1"))
                {
                    country_id = int.Parse(ViewState["country_id"].ToString());
                }
                if (!ViewState["state_id"].Equals("-1"))
                {
                    state_id = int.Parse(ViewState["state_id"].ToString());
                }
                if (!string.IsNullOrEmpty(e.NewValues["Email"].ToString().Trim()))
                {
                    email = e.NewValues["Email"].ToString().Trim();
                }
                if (!string.IsNullOrEmpty(e.NewValues["Remark"].ToString().Trim()))
                {
                    remark = e.NewValues["Remark"].ToString().Trim();
                }
                if (!string.IsNullOrEmpty(ViewState["UserOFC"].ToString()))
                   //error occurs doesn't selected from user
                {
                    UserOFC = bool.Parse(ViewState["UserOFC"].ToString());
                }
                if (!string.IsNullOrEmpty(ViewState["UserVAT"].ToString()))
                {
                    UserVAT = bool.Parse(ViewState["UserVAT"].ToString());
                }
                if (!string.IsNullOrEmpty(ViewState["UserINV"].ToString()))
                {
                    UserINV = bool.Parse(ViewState["UserINV"].ToString());
                }
                if (!string.IsNullOrEmpty(ViewState["UserNone"].ToString()))
                {
                    UserNone = bool.Parse(ViewState["UserNone"].ToString());
                }
                db.UpdateParty(e.NewValues["Name"].ToString().Trim(), int.Parse(ViewState["type_id"].ToString()), owname, ownmono, room, build, road, area, city, phone, mobile, email, cpname, cpmono, UserOFC, UserVAT, UserINV, UserNone, state_id, country_id, remark, db.Users.Where(u => u.Username.Equals((String)Session["Username"])).Select(u => u.Ref_no).FirstOrDefault(), customerid);
                db.SubmitChanges();
                GV_ViewCustomers.EditIndex = -1;
                this.FillGrid((String)Session["StartAlpha"] ?? null, (int)Session["GroupByENTYPE"], (String)Session["ColumnName"] ?? null, (String)Session["SearchText"] ?? null);
                UpdatePanel10.Update();
                MPE.Show();
            }
        }
    }

如何检查视图状态是否存在于asp.net 4.0中

viewstate集合实现了一个键/值StateBag。您有两个开箱即用的选项来检查键是否存在。

简单null检查

if (ViewState["type_id"] != null && !ViewState["type_id"].Equals("-1")) 
{
}

使用Contains方法

if (ViewState.Contains("type_id") && !ViewState["type_id"].Equals("-1")) 
{
}