3

I have a webpart created with visual webpart, it has a GridView and it looks like as following image:

Visual Web Part

For everytime I edit the page containing this webpart it duplicates I.E if I edit this webpart 2time it shows me 4 tables just like the following image: enter image description here

But This thing happens only in editing mode, but sometimes the whole web part disappears!

Thanks in Advice.

  • Sound like your webpart is not handling Postbacks very well.. Not easy to help you without the relevant source code – Robert Lindgren Jun 9 '16 at 13:52
  • Are you adding the columns programmatically every time the page reloads? – Erin L Jun 9 '16 at 13:52
  • @RobertLindgren You can Check my update question to show code – Nderon Hyseni Jun 9 '16 at 13:56
  • @ErinL Yes I am creating columns programatically – Nderon Hyseni Jun 9 '16 at 13:56
  • Not seeing your code, but make sure to do a if(!Page.IsPostBack){ around the code creating your grid. In your case around all code in the Page_Load method – Robert Lindgren Jun 9 '16 at 13:58
4

Your webpart is behaving like this because your are not handling postbacks correctly. Wrap all code in the Page_Load method with the following

if(!Page.IsPostBack){ 
    // Original code here
}

Some info about PostBack here: http://net-informations.com/faq/asp/ispostback.htm

Or, just before your new databinding of the grid do this

GridView1.DataSource = null;
GridView1.DataBind();

so it will be

GridView1.DataSource = null;
GridView1.DataBind();

GridView1.DataSource = dt; 
GridView1.DataBind();

This question is really an ASP.net question and not SharePoint specific to be fair. It's about understanding the lifecycle of ASP.net pages

  • It is not duplicating anymore but unpredictably it disappears – Nderon Hyseni Jun 9 '16 at 14:03
  • It is working perfectly thank you so much you are a life saver thanks – Nderon Hyseni Jun 9 '16 at 14:10
  • What part made it work? Only the post back check? – Robert Lindgren Jun 9 '16 at 14:10
  • the first and the second part of answer! – Nderon Hyseni Jun 9 '16 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.