Postback Asp.Net Webforms

Asked

Viewed 492 times

0

I just started working with WebForms and it became a question, due to always messing with the asp.net mvc.

Supposing I have one DropDownList and populated the DataSource of it with various records, when held the PostBack i can’t recover the data that is in that DropDownList, have any way to recover these data? what is the best way to work with the 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);
            }
        }            
    }
  • you want, when selecting the item in the dropdown, it performs the postback and you take the value that was selected ?

  • @Rovannlinhalis, no friend, I want to know how I work with all the data that was uploaded in this Dropdownlist, let’s assume, when performing the postback, I take everything that’s on its datasource and work with those records, you know? I only see one way to work with that, playing in a Viewstate, but I know it’s not good practice, I wonder if there’s any other way to do it!

  • and, the postback can be fired in several different ways, some specific control for you to treat it ? or it would be in all postbacks that occur on the page ?

  • @Rovannlinhalis, I am developing a routine that I will need to take all postback of the page the data and perform a filter on it to find the record I need, IE, I will take this data in all postbacks.

1 answer

1

You can, in every Load event of the page, check whether it is a postback or not. And then take the data that is in the dropdown source. 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. Be careful, as the code will run on any postback that happens, it can considerably affect the performance.

  • So I tried to accomplish this way, but his datasource comes null, I imagine it is due to postback.

  • the dropdown is filled in q way ?

  • I will edit the post by putting the code.

  • Take a look @Rovann

  • tried to store in a Session ?

  • Yes, exactly this is my question, they say that it is not very good to keep data in Session or even in Viewstate, I just like to know even if there is any other way out than this, because I found it strange that C# does not send the data in the postbacks, at least of the component that is performing the postback. But from what I can see there’s no other way out but to work with Session or Viewstate ?

  • Yeah, honestly, it’s been a while since I’ve done webforms, and I can’t remember if I’ve ever done anything like this, but I’ve never had a problem working with dropdown data. But anyway, I hope you can solve it, and if it’s not too much data, that will harm the application, it runs Yes. vlw

  • 1

    In this case would be better Viewstates right? Because at least they are destroyed when changed the page.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.