Solved

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

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

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now