3
votes

I have a gridview which displays some details about certain files. It has 5 columns including a template field which contains checkboxes.

From the code behind, OnInit, I add a number of columns for additional information which may or may not be needed depending on the page. Code is below:

 for (int i = 0; i < models.Length && i < 3; i++)
            {
                var model = models[i];

                //Add gridview rows
                BoundField bf = new BoundField();
                bf.DataField = "Attribute" + i;
                bf.HeaderText = model.AttributeName;
                bf.Visible = true;

                gvFiles.Columns.Insert(6 + i, bf);

            }

This works well and I get the columns. In the OnLoad event I databind certain data to the gridview and that works as well.

The problem rises when a postback occurs. Whenever the page creates a postback, it performs OnInit, then crashes with this ('on page') error message:

An error has occurred because a control with id 'ctl00$MainContent$gvFiles$ctl02$ctl00' could not be located or a different control is assigned to the same ID after postback. If the ID is not assigned, explicitly set the ID property of controls that raise postback events to avoid this error.

I identified the control which was giving the issue as the checkbox in the TemplateField, and I gave it an id. However now, on postback the following happens:

  1. The checkbox in the TemplateFieldss don't appear

  2. One of the columns is an ImageField and it loses its "Control-Style Width/Height" parameters and I get massive images.

The issue is only happening on postback, and removing the code which adds the columns pro grammatically makes everything work perfectly.

How can I get this to work ?

5
Share more ASPX markup and Code Behind (where you are binding the GridView and assigning a Template). - Mikhail
do the number of models change on the postback? - Clinton
Why are you using OnInit()? That is like so dangerous on so many levels. Define the Columns first in your gridview as HIDDEN and only display them as necessary. You can do all of that inside the DataRowBound() event - Fandango68

5 Answers

2
votes

You need to re-create a dynamically added control on postback.

MSDN recommends creating controls in the PreInit event.

Raised after the start stage is complete and before the initialization stage begins. Use this event for the following:

  • Check the IsPostBack property to determine whether this is the first time the page is being processed. The IsCallback and IsCrossPagePostBack properties have also been set at this time.
  • Create or re-create dynamic controls.
  • Set a master page dynamically.
  • Set the Theme property dynamically.
  • Read or set profile property values.
2
votes

Simply I tell the one line solution

You need to add !IsPostback in your Page Load event within your Gridview bind method

Explain:

See the below code sample

PageLoad()
{
BindGridview();
}

Public void BindGridview()
{
//Binding codes and add extra column codes
for (int i = 0; i < models.Length && i < 3; i++)
            {
                var model = models[i];

                //Add gridview rows
                BoundField bf = new BoundField();
                bf.DataField = "Attribute" + i;
                bf.HeaderText = model.AttributeName;
                bf.Visible = true;

                gvFiles.Columns.Insert(6 + i, bf);

            }
}

You have call the "BindGridview()" method in page load .The same column field was created Whenever your page loaded.

you need to call "BindGridview()" method in page load at first time only.So you surly need !IsPostBack.

See this below code | or call this in inside of Page Init event

PageLoad()
{
if(!IsPostBack)
{
BindGridview();
}
}

Now BindGridview(); method is called the page first time loading.

More details about "IsPosteBack" property

0
votes

If there is an if(!ispostback) in your oninit event then remove it. The dynamic controls need to be created at every postback on the Oninit event only.

If the control is not recreated then it can not be found, similar to what is happening in your case. If the control is created say on PageLoad then the client side values would not be accessible and you will get a different error.

Hope this helps.

0
votes

The checkbox and the images are having problems cause the css failed due to errors. The error you are having is that when you do postback, there is already a row with the data you previously added, so to fix this you need the following code:

protected void Page_Init(object sender, EventArgs e)
    {


        if (Page.IsPostBack)
        {
         gvFiles.DataBind();

        }

    }
0
votes

If the number of columns you are adding on Post Back is different than the initial load, that could be the issue. Specifically when the view state tries to load:

When adding a dynamic control c to some parent control p based on some condition (that is, when not loading them on each and every page visit), you need to make sure that you add c to the end of p's Controls collection. The reason is because the view state for p contains the view state for p's children as well, and, as we'll discuss in the "Parsing the View State" section, p's view state specifies the view state for its children by index. (Figure 5 illustrates how inserting a dynamic control somewhere other than the end of the Controls collection can cause a corrupted view state.)

http://msdn.microsoft.com/en-us/library/ms972976.aspx