PostBack Asp.Net WebForms

0

I started working a little while with WebForms and was left in doubt, because I always mess with asp.net mvc .

Assuming I have a DropDownList and populated DataSource with multiple records, when done PostBack I can not retrieve the data contained in this DropDownList , is there any way to retrieve this data? what is the best way to work with data in PostBack . Thank you.

    private void PopularEmpresasDoGrupoDoUsuario()
    {
        VOB2BUser voUser = GetSessionUser();
        DAOB2BCompany daoCompany = new DAOB2BCompany(((Page)Page).DATABASE);
        DAOB2BUser daoUser = new DAOB2BUser(((Page)Page).DATABASE);
        DAOB2BCompanyGroupUser daoCompanyGroupUser = new DAOB2BCompanyGroupUser(((Page)Page).DATABASE);

        var userGroup = daoCompanyGroupUser.GetUserGroup(voUser.IdUser);
        if (userGroup == null)
        {
            divEmpresasDoGrupoDoUsuario.Visible = false;
        }
        else
        {
            divEmpresasDoGrupoDoUsuario.Visible = true;

            dropDownListEmpresasDoGrupoDoUsuario.DataSource = daoCompany.GetEmpresasVinculadasNoGrupoDeEmpresa(userGroup.IdGroup);
            dropDownListEmpresasDoGrupoDoUsuario.DataBind();

            upEmpresasDoGrupoDoUsuario.Update();

            dropDownListEmpresasDoGrupoDoUsuario.SelectedIndex = 0;
            dropDownListEmpresasDoGrupoDoUsuario_SelectedIndexChanged(dropDownListEmpresasDoGrupoDoUsuario, new EventArgs());
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!this.Page.IsPostBack)
        {                
            if (Session["USER"] != null)
            {                    
                PopularEmpresasDoGrupoDoUsuario();
                SetUserPermissions(fc);
            }
        }            
    }
    
asked by anonymous 06.03.2018 / 15:02

1 answer

1

You can, in any Load event of the page, check whether it is a postback or not. And then get the data that is in the source of the dropdown. Example:

  

I used a DataTable as an example

protected void Page_Load(object sender, EventArgs e)
{

    if (!this.IsPostBack)
    {
          //Não é postback
    }
    else
    {
         DataTable dt  = dropDownList1.DataSource as DataTable;
         if (dt != null)
         {
            //Faz o que precisa com o DataTable
         } 
    }
}
  

ps. Caution, because the code will run on any postback that happens, can greatly affect performance.

    
06.03.2018 / 15:21