I have the following error when I use DropDownlist for 3rd or second time! "failed to load viewstate is being loaded must .."

40 Views Asked by At

I have a "listview" for showing list of employee and a "dropdownlist" to select department. The following error occurs when I use "DropDownlist" for 3rd or second time:

"Failed to load viewstate".

The control tree into which viewstate is being loaded must match the control tree that was used to save "viewstate" during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request."

This is asp.net Webform and I have to use this technology and have no other choice .

namespace .Presentation.general
{

public partial class Listg : PageBase
{


    void Page_PreInit(Object sender, EventArgs e)
    {
        this.MasterPageFile = "~/App_MasterPages/empty.Master";
    }
    protected void Page_Init(object sender, EventArgs e)
    {


    }
    protected void Page_Load(object sender, EventArgs e)
    {


        if (!IsPostBack)
        {
            PopulateDepartmentsDropDownList();

            GeneralObjectDataSource.SelectParameters["Department"].DefaultValue = "";

            decimal presence = Convert.ToDecimal(Data.EmployeeDB.Create().GetCountEMPOnlineToday());

            decimal visibles = Convert.ToDecimal(Data.EmployeeDB.Create().GetCountVisiblesEmployees());
            visibles = (visibles == 0 ? 1 : visibles);
            PresenceLabel.Text = System.Math.Round((presence / visibles) * 100, 1).ToString() + "% " + string.Format(" ({0})", presence);



        }

    }

     public void Search(object sender, EventArgs e)
    {

        GeneralObjectDataSource.SelectParameters["name"].DefaultValue = Common.Converter.ConvertToFarsiYK(NameTextBox.Text.Trim());



        if (PresenceRadioBottonList.SelectedValue == "1")
        {
            GeneralObjectDataSource.SelectParameters["onlyPresence"].DefaultValue = "true";
        }
        else
        {
            GeneralObjectDataSource.SelectParameters["onlyPresence"].DefaultValue = "false";
        }

        DataListView.DataBind();
    }

    public void select_department_SelectedIndexChanged(object sender, EventArgs e)
    {

        GeneralObjectDataSource.SelectParameters["name"].DefaultValue = "";
        GeneralObjectDataSource.SelectParameters["Department"].DefaultValue = select_department.SelectedItem.Text;
        GeneralObjectDataSource.DataBind();


        DataListView.DataBind();


    }



    private void PopulateDepartmentsDropDownList()
    {
        select_department.DataSource = Biz.EmployeeBO.GetDepartments();
        select_department.DataTextField = "Name";
        select_department.DataValueField = "ID";
        select_department.DataBind();

        select_department.Items.Insert(0, new ListItem("", "0"));
        select_department.SelectedValue = Biz.Settings.SelectedDepartmentID;
    }
}
}
0

There are 0 best solutions below