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.

6
  • Sound like your webpart is not handling Postbacks very well.. Not easy to help you without the relevant source code Commented Jun 9, 2016 at 13:52
  • Are you adding the columns programmatically every time the page reloads?
    – Erin L
    Commented Jun 9, 2016 at 13:52
  • @RobertLindgren You can Check my update question to show code Commented Jun 9, 2016 at 13:56
  • @ErinL Yes I am creating columns programatically Commented Jun 9, 2016 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 Commented Jun 9, 2016 at 13:58

1 Answer 1

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

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

Your Answer

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