Solved

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

Posted on 2012-03-22
10
997 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
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!

 

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Please explain purpose of GZIP 4 82
using CK editor in iframes 5 35
How to repeat the data 4 43
Javascript function 3 20
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

734 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