Listview databind causes lost of formview Data

2019-03-01 11:36发布

问题:

within my code , after a research via a Formview , I need to call the listview.databind and this makes impossible to get the Formview data , even if in the screen they still appear . this is my code

  protected void DemandeSearchFormView_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        ListView listview = (ListView)panelPagination.FindControl("listdeclarations");
        ViewState["search"] = "search";
        listview.DataBind();

    }

the databind() normally call this method

 public DeclarationGeneraleBean RechercheByCritere()
    {
        DeclarationGeneraleBean declarationBean = new 
        DeclarationGeneraleBean();
        declarationBean.IdService = (int) Session["idService"];
        if (ViewState["search"] != null)
        {
            TextBox numOrdre = 
        (TextBox)DemandeSearchFormView.FindControl("numtxt");

}

the ViewState["search"] is null , I dont know why ?? it seems that the databind() recharge the page or something like this . Have any one an idea how to deal with this ?

回答1:

Do you set the viewstate in your page load event?

If yes, i think you should add a condition in your Page_Load event :

private void Page_Load()
{
    if (!IsPostBack)
    {


    }
}

it prevent the data to be reloaded on this event, if a post is submited.