Solved

ASP Base.Render GridView Group Row/Headers Postback Not Working?

Posted on 2012-03-22
10
985 Views
Last Modified: 2012-03-23
I'm using a 'Render' method to format a gridivew. It works very well on first page load. Problem arises on page postback. The gridview looses its special formatting that the Render method provides. Help!

1. I would like to either call the Render method on postback
2. or build a Grouped Gridview some other way so postback is not a problem.

This 'Render' code below provides grouped rows under headers like this:

[Gridview Rows/Headers Example]
  - Office Furniture -
           Chairs
           Desks
         Cabinets
  - Sporting Goods -
         Baseball
        Basketball
         Football
          Rugby


[Current Render Code]
         protected override void Render(HtmlTextWriter writer)
        {
            if (GridView1.Rows.Count != 0)
            {
                Table gridTable = (Table)GridView1.Controls[0];
                String lastValue = String.Empty;
                String currentValue = String.Empty;

                foreach (GridViewRow myRow in GridView1.Rows)
                {
                    currentValue = myRow.Cells[1].Text;

                    if (currentValue != lastValue)
                    {
                        int rowIndex = gridTable.Rows.GetRowIndex(myRow);

                        GridViewRow headerRow = new GridViewRow(rowIndex, rowIndex, DataControlRowType.DataRow, DataControlRowState.Normal);
                        TableCell headerCell = new TableCell();
                        headerCell.ColumnSpan = GridView1.Columns.Count;
                        headerCell.Text = currentValue;
                        headerCell.BackColor = System.Drawing.Color.FromArgb(242, 216, 181);
                        //headerCell.CssClass = "SortHeaderRowStyle";
                        headerCell.Font.Bold = true;
                        headerCell.BorderStyle = BorderStyle.Solid;
                        headerCell.HorizontalAlign = HorizontalAlign.Left;
                        headerRow.Cells.Add(headerCell);
                        gridTable.Controls.AddAt(rowIndex, headerRow);
                    }

                    lastValue = currentValue;
                }
            }

            base.Render(writer);
        }
0
Comment
Question by:pointeman
  • 5
  • 5
10 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37756745
I am not sure what you are trying to achieve, but on the surface it looks like you can take a different approach, and get to the same place.

Can you explain why you are using the Render method, and not the GridView's RowCreated/RowDataBound events?
0
 

Author Comment

by:pointeman
ID: 37757315
I found the code example and it looked like a quick fix, however I'm finding it has limitations. Please provide code examples to help me with this problem. It seems the postback is a real problem with most examples I've found so far. As well, Row Editing is also a problem with row gouping gridview design. Help!
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37757383
What do you mean by "row grouping"?
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:pointeman
ID: 37757719
[Gridview Rows/Headers Example]
  - Office Furniture -
           Chairs
           Desks
         Cabinets
  - Sporting Goods -
         Baseball
        Basketball
         Football
          Rugby
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37757790
With a little more thought, I don't think that you should do that work of creating rows in the Render.  It needs to be in the control initialization phase, so that the creation is only done once.  That may be problematic, with the view state processing.

Are you trying to do something more than the ListView can do with groups?
0
 

Author Comment

by:pointeman
ID: 37757901
I'm using a GridView.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37757981
Then, re-evaluate where to create the rows, since the Render is not the correct place.

This example shows creating rows in the Page_Init:

How to Add a Locked Header Row to an ASP.NET GridView Control
http://blogs.iis.net/webtopics/archive/2009/06/23/how-to-add-a-locked-header-row-to-an-asp-net-gridview-control.aspx
0
 

Author Comment

by:pointeman
ID: 37758025
Need multiple header columns with rows beneath each. I think you're correct about creating the gridview one time. Postback is always the issue with other examples.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 37758050
You have to always keep the page life cycle in the back of your mind.  It is always about doing something at just the right time.

Sorry, that was an example showing Page_Init, not creating header rows.
0
 

Author Closing Comment

by:pointeman
ID: 37759353
thanks for your help
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to refer to value 8 62
How come this regular expression allows "e"? 14 45
Where is this file? 3 25
Please explain purpose of GZIP 4 35
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question