asp.netvb.netgridviewautogeneratecolumn

Gridview Columns change on postback


I'm developing my website in asp.net using visual web developer 2010. I have a product page for an online store where it is displaying the product pictures and a gridview is displaying information about the product, there are multiple columns for each product.

The way I have set up my code for this page is that I have autogeneratecolumns = true (For specific reasons) mainly because I am pulling the gridview data dynamically from many different tables and hundreds of different products and categories. This all works fine up until the point where I add in my "Add To Cart" button into the gridview so that there is a button in every row.

Because I have the autogeneratecolumns set to true, when i insert my "Add To Cart" button none of the columns are created yet so the button displays in the first column, followed by the rest of the data. I have a block of code that runs in the pre render stage which takes the button column and moves it to the very last column. THIS is where the problem is at.

For some reason, even when I place all of this pre render code in an if not postback, when another picture is clicked on the page and the page does postback, my column headers change and the "Add To Cart" column is not longer there which creates issues. An example of how the columns are before I click on another picture is (Order#, Description, Price, Add To Cart) and after I click on a picture (Color, Degrees, Description, Price). It's like it is still running the Gridview Pre Render even when it shows that it skips that block of code during debugging. Here is the code...

 Protected Sub GridView1_PreRender(ByVal sender As Object, ByVal e As EventArgs)

    Dim GridView1 As GridView = FormView1.FindControl("GridView1")
    If Not IsPostBack Then
        For Each row As GridViewRow In GridView1.Controls(0).Controls

            Dim tc As TableCell = row.Cells(0)
            row.Cells.Add(tc)

        Next
    Else

    End If

End Sub


Protected Sub ImageButton3_Click1(sender As Object, e As System.Web.UI.ImageClickEventArgs)
    Dim img As System.Web.UI.WebControls.Image = FormView1.FindControl("Image1")
    Dim img1 As ImageButton = FormView1.FindControl("ImageButton3")
    Dim url As String = img.ImageUrl
    img.ImageUrl = img1.ImageUrl
    img1.ImageUrl = url
End Sub

I know it is this code that is causing it because I removed the code and everything worked as it should however this code is the only solution I could come up with to move the cart button column to the very last column in the gridview. Sorry for the extremely long Question but any help as to how to prevent my columns from changing or shifting when an image button is clicked would be appreciated.


Solution

  • Thought I would just go ahead and post what I did to make this problem stop. I never ended up figuring out exactly what was causing this problem but I ended up adding an update panel around all of my images and this seemed to fix the problem.