title: how to check view state exists in asp.net 4.0
body: <p>i just implement one editable grid view with asp.net 4.0. now i just set ViewState for storing only for this document with this code :</p>
  
  <pre><code> 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();
      }
  </code></pre>
  
  <p>now at grid view row updating i have to check that ViewState exists or not because when user doesn't select it or check it check box values then it not accessed.</p>
  
  <p>here is my grid view row updating code :</p>
  
  <pre><code>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()) &amp;&amp; !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 =&gt; u.Username.Equals((String)Session["Username"])).Select(u =&gt; 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();
              }
          }
      }
  </code></pre>