Solved

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

Posted on 2012-03-22
10
1,009 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

631 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