The original FormView data source is lost when another button is clicked

I want a preface to this, saying that I am starting to work with asp.net, especially when it comes to working with controls FormView. I searched long and hard and spent hours debugging this issue.

I have 3 FormViewson one aspx page. Each FormViewhas its own EditItemTemplateand PagerTemplatewith DefaultMode="Edit". I do not use SqlDataSource, but instead binding the databinding programmatically on PageLoadat if(!Page.IsPostBack), and also calling the data binding method when the method is called PageIndexChanging. The pager template contains the "Back" and "Next" button installed with CommandArgument="Prev"and CommandArgument="Next", respectively, and both are installed with CommandName="Page".

Paging works great on the first FormView. When I click the "Back" or "Next" button, the pages (i.e. Re-link) respectively. During the first FormViewpaging event, I also successfully call the binding methods for the 2nd and 3rd FormView, as I want them to bind data specific to the page selected in the 1st FormView.

But, when I go back to the first page of the 1st FormView(i.e. PageIndex = 0), and then try to go to the page forward in the second FormView, then the datakey for the first FormViewwill be empty. In fact, it formview1.DataSourceis null for the first FormViewwhen I try to press the navigation button on the second FormView.

Then here, where I thought it was weird ... if I click the 1st time a second time FormView, THEN formview1.DataSourcewill be fine, and then I can go into the 2nd FormView.

All views in forms and buttons are set to true. I tried to call formview1.DataBind()inside the formview2paging event before any paging would happen, but there would be no success. I also tried setting the properties: UseSubmitBehavior="False"and CausesValidation="False"on the search buttons. Admittedly, I did this without understanding the behavior, but rather, realized it, seeing that it was proposed in solutions for other, several related problems.

The templates are quite long, since each of them has many fields. But the tags FormViewlook like this:

<asp:FormView ID="fvHeader" runat="server" DataKeyNames="ObjectID" DefaultMode="Edit" AllowPaging="True" OnModeChanging="fvHeader_ModeChanging" OnPageIndexChanging= "fvHeader_PageIndexChanging"> 
<EditItemTemplate> ..... </EditItemTemplate> </asp:FormView>

PagerTemplates:

       <PagerSettings Mode="NextPrevious" />
       <PagerTemplate>
           <span class="labels">Page: <%#fvHeader.PageIndex+1%> of <%#fvHeader.PageCount %></span>&nbsp;
           <asp:Button ID="btnBack" runat="server" CommandArgument="Prev" CommandName="Page" CssClass="btnHdr" Text="&lt;&lt; Back" />&nbsp;
           <asp:Button ID="btnNext" runat="server" CommandArgument="Next" CommandName="Page" CssClass="btnHdr" Text="Next &gt;&gt;" />&nbsp;</PagerTemplate>

, 'fvHeader' - , 'formview1' .

/ # 1- :

    protected void fvHeader_PageIndexChanging(object sender, FormViewPageEventArgs e)
            {
                    fvHeader.PageIndex = e.NewPageIndex;
                    bindFV_Initial(); 

                    //rebind fvSub1 (2nd formview) to get the 1st obs of the newly selected header record
                    fvSub1.ChangeMode(FormViewMode.Edit);
                    fvSub1.PageIndex = 0;
                    bindSub1_Initial();

//rebind 2nd subform
                    fvSub2.ChangeMode(FormViewMode.Edit);
                    fvSub2.PageIndex = 0;
                    bindSub2_Initial(); 

            }
     private void bindFV_Initial()
            {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }

                    conn.Open();

                    if (dtEOS == null || dtEOS.Rows.Count == 0)
                    {
                        sqlda = new SqlDataAdapter("USE dbWEF SELECT * FROM tblHeader WHERE [UserID] = '" + Session["User"] + "' AND [ProjectName] = '" + Session["Project"] + "'", conn); 
                        sqlda.Fill(dtEOS);
                    }

                    fvHeader.DataSource = dtEOS;
                    fvHeader.DataBind();

                    conn.Close();

                    if (dtEOS.Rows.Count > 0)
                    {
                        fillDD_fvHeader(); //Fill dropdowns and databind ddls
                    }
                }

            }

, , 2- .

    Error in: bindSub1_Initial.
    System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
   at System.Collections.ArrayList.get_Item(Int32 index)
   at System.Collections.Specialized.OrderedDictionary.get_Item(Int32 index)
   at System.Web.UI.WebControls.DataKey.get_Item(Int32 index)
   at RenewableEnergyDataEntry.Forms.EagleObservationSurvey.bindSub1_Initial() 

, , 2- , datatable , - datakey formview1, , - null, formview1 . , KEEPS- , formview2 . ...

+4
1

. , . , dataSource: (dtEOS). , reset . ASP.net( / ).

, , .

0

Source: https://habr.com/ru/post/1568667/


All Articles