3

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 ?

4
  • codeproject.com/Questions/659792/… Commented Dec 9, 2013 at 8:21
  • Share more ASPX markup and Code Behind (where you are binding the GridView and assigning a Template). Commented Dec 9, 2013 at 19:42
  • do the number of models change on the postback? Commented Dec 12, 2013 at 20:25
  • 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 Commented Aug 1, 2017 at 5:20

5 Answers 5

2
+100

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.
Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

Had no effect. Still the same crash. It doesn't even get to PageLoad before the issue occurs.
or call this in inside of OnInit event
0

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.

Comments

0

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();

        }

    }

4 Comments

The issue is absolutely nothing to do with CSS.
When there's an error generally the templates break down, that's what i meant by CSS. In this case the template breaks down when there's already a row with content. databind() clears and rebinds the gridview, therefore even if you postback there will be no more problem's.
@geedubb please take your time to carefully read the answer. The poster clearly said you've a logical issue in your code that breaks the page presentation.
@TCB13 If you carefully read the question you will see that I did not ask it, and it is not my code
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.