asp.netvb.netgridview

ASP.NET GridView empty on postback


Having an issue with an ASP.NET GridView is empty on postback that I need some help with. I think it may have something to do with the ViewState not being setup. Anyhow I originally had the code working on single user-form until I refactored code.

Now to paint the picture I have now both a master page and a base form. My master page has the place holder and on my actual user-form I have placed the GridView within the place holder bounds as follows:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderMainBody" Runat="Server">
    <asp:GridView ID="data" runat="server" AutoGenerateColumns="false" EnableViewState="true" ...>
        ...
    </asp:GridView>
</asp:Content>

One of fields in the GridView is an editable comments field mutli-line textbox (the rest are non editable):

<asp:TemplateField HeaderText="Comments">
    <ItemTemplate>
        <asp:TextBox ID="TextBoxComments" runat="server" TextMode="MultiLine" Rows="4" Columns="40" Text='<%# Bind("Comment")%>' />
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="TextBoxCommentsEdit" runat="server" TextMode="MultiLine" Rows="4" Columns="40" Text='<%# Bind("Comment")%>' />
    </EditItemTemplate>
</asp:TemplateField>

I edit one of the rows and click a submit button to postback. The GridView has 10 rows to enter into however on postback there are zero rows so my saving is lost!

My base form contains the code in the OnInit event to load the submit button and thus also handles the click event.

My OnLoad event I call the base Onload which inturn calls my user form's Page_Load handler code which has one line of code namely:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    MyBase.data = Me.data
End Sub

and in the BaseForm is declared as:

Protected WithEvents data As GridView

Protected Overrides Sub OnLoad(e As EventArgs)
    MyBase.OnLoad(e)
    If Not Page.IsPostBack Then
        ...
        BindData(...)
        ...
    End If
End Sub

in this way I can also handle all GridView events in the BaseForm.

So somewhere between the master/baseform/userform/viewstate relationship my GridView data is lost on PostBack. Any ideas?


Solution

  • I didn't mention in the question that, in my OnInit method, I call the following code:

    Dim cpl As ContentPlaceHolder = Master.FindControl("ContentPlaceHolderFooter")
    btnUpdate = New Button
    btn.ID = "btnUpdate"
    cpl.Controls.Add(btnUpdate)
    

    I did not add the button to the footer of the grid as opposed to an additional content placeholder in the master page. That's my fault.

    I moved the code above to the CreateChildControls overrideable method and I also required an additional call to EnsureChildControls in my OnLoad event so my OnInit method with emphasis disintegrated! Why?

    Well the answer was hinted at within the answer to the other question asked on this site I mentioned in my second comment to "Rajan Chauhan" that I checked out and that is apparently whenever you iterate through the collection of controls you mess with the ViewState (hey I am just re-iterating what was said in the other post I have no authority on the matter) before it gets loaded so calling Master.FindControl is a no-no inside OnInit!

    However, saying all that my RowUpdated event does not fire as I am actually editing in view mode because of my ItemTemplate markup so I will stick with what I have as my btnUpdate_Click event still works as before i.e. it does some magical code that I found on some other site that checks each row one by one for change of data and then updates that particular row only. Well I can as there is only 10 rows at most so I do not overload the ViewState too much and if it is important to know I also use paging so in reality I have more than 10 rows but did not want to mention that as I thought that might add to the confusion.