Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-03-22
10
Medium Priority
?
1,019 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
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 …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

715 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