类型'System.Web.UI.WebControls.GridView'未标记为可序列化

时间:2018-12-24 06:18:14

标签: c#

我有一个Web表单,我将文本框值绑定到数据表,然后将数据表绑定到我的gridview。我的代码工作得很好,但是从gridview删除一行时,我却报错了,通过使用viewstate将gridview绑定在delete命令上。

  

错误:-在程序集中键入“ System.Web.UI.WebControls.GridView”   'System.Web,版本= 4.0.0.0,文化=中性,   未将PublicKeyToken = b03f5f7f11d50a3a'标记为可序列化。

下面是我的代码。

On Page Load

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dt.Clear();
            }
            if (dt.Columns.Count == 0)
            {
                dt.Columns.Add("RA No");
                dt.Columns.Add("Bill Number");
                dt.Columns.Add("Bill Date");
                dt.Columns.Add("MB Book Number");
                dt.Columns.Add("Page Number");
                dt.Columns.Add("Gross Amount");
                dt.Columns.Add("Statutory Deductions");
                dt.Columns.Add("Net Amount");
            }
        }

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dt.Clear();
            }
            if (dt.Columns.Count == 0)
            {
                dt.Columns.Add("RA No");
                dt.Columns.Add("Bill Number");
                dt.Columns.Add("Bill Date");
                dt.Columns.Add("MB Book Number");
                dt.Columns.Add("Page Number");
                dt.Columns.Add("Gross Amount");
                dt.Columns.Add("Statutory Deductions");
                dt.Columns.Add("Net Amount");
            }
        }

        public void setValues(DataRow dr)
        {
            dr["RA No"] = ddlRANumber.SelectedItem.Text;
            dr["Bill Number"] = txtBillNo.Text;
            dr["Bill Date"] = txtBillNo.Text;
            dr["MB Book Number"] = txtMBBookNo.Text;
            dr["Page Number"] = txtPageNo.Text;
            dr["Gross Amount"] = txtGrossAmt.Text;
            dr["Statutory Deductions"] = txtSDeduction.Text;
            dr["Net Amount"] = txtNetAmt.Text;
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {

                if (ViewState["CurrentTable"] != null)
                {
                    DataTable currentTable = (DataTable)ViewState["CurrentTable"];

                   if (currentTable.Rows.Count > 0)
                    {
                        DataRow dr = currentTable.NewRow();
                        setValues(dr);

                        currentTable.Rows.Add(dr);
                            ViewState["CurrentTable"] = currentTable;
                        gvBillDetails.DataSource = currentTable;
                    }
                }
                else
                {
                    DataRow dr = dt.NewRow();
                    setValues(dr);
                    dt.Rows.Add(dr);
                    ViewState["CurrentTable"] = null;
                    ViewState["CurrentTable"] = dt;
                    gvBillDetails.DataSource = dt;
                }

                gvBillDetails.DataBind();

            }
        }

        protected void gvBillDetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string RA_Bill = e.Row.Cells[1].Text;
                foreach (Button button in e.Row.Cells[0].Controls.OfType<Button>())
                {
                    if (button.CommandName == "Delete")
                    {
                        button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + RA_Bill + "?')){ return false; };";
                    }
                }
            }
        }

        protected void gvBillDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int index = Convert.ToInt32(e.RowIndex);
            DataTable CurrentTable = (DataTable)ViewState["CurrentTable"];
            CurrentTable.Rows[index].Delete();
            if (CurrentTable.Rows.Count <= 0)
            {
                ViewState["CurrentTable"] = null;
            }
            else
            {
                ViewState["CurrentTable"] = null;
                ViewState["CurrentTable"] = gvBillDetails;
            }
            gvBillDetails.DataSource = CurrentTable;
            gvBillDetails.DataBind();
        }

1 个答案:

答案 0 :(得分:0)

我认为导致您出错的行是:

ViewState["CurrentTable"] = gvBillDetails;

您正试图将控件gvBillDetails存储为页面的状态,这是不可能的,因为如错误所述,GridView不是可序列化的类。

我建议您尝试使用专用(可序列化)的类存储两次调用之间需要维护的数据。

希望有帮助!