views:

42

answers:

1

Hi Guys,

I have a dropdownlist that controls the contents of 3 gridview controls. These are used in a webpart. However, every time the selectedindexchanged method of the dropdownlist fires an event, the contents of the dependent gridviews adds another rendering of a gridview. Hence, it doubles and even triples the contents of those gridviews.

I have the following codes:

->for the CreatChildControls method:

ProfileGrid(_gridProf, _dtProf, _infoObj, _column, _imgColumn, _ddl, _strConn, _id);
Controls.Add(_gridProf);
Controls.Add(new LiteralControl("<br />"));

PhysicalGrid(_gridPhys, _dtPhys, _infoObj, _column, _ddl, _strConn, _id);
Controls.Add(_gridPhys);
Controls.Add(new LiteralControl("<br />"));

LabGrid(_gridLab, _dtLab, _infoObj, _column, _ddl, _strConn, _id);
Controls.Add(_gridLab);
Controls.Add(new LiteralControl("<br />"));

->for the SelectedIndexChanged method:

private void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
   ProfileGrid(_gridProf, _dtProf, _infoObj, _column, _imgColumn, _ddl, _strConn, _id);
   PhysicalGrid(_gridPhys, _dtPhys, _infoObj, _column, _ddl, _strConn, _id);
   LabGrid(_gridLab, _dtLab, _infoObj, _column, _ddl, _strConn, _id);
}

->for one of the GridView Controls:

private void ProfileGrid(GridView grid, DataTable dt, InfoAccess infoObj, BoundField column, ImageField imgColumn, DropDownList ddl, string strConn, string id)
{
   string query = "exec spr_VITALITY_SCORE '" + id + "', '" +  (ddl.SelectedValue == "" ? DateTime.Now.Year.ToString() : ddl.SelectedValue.ToString()) + "'";

   infoObj.StrConn = strConn;
   dt = infoObj.SQLResult(query);
   grid.DataSource = dt;

   column.DataField = "SCORE";
   column.HeaderText = "Score";
   grid.Columns.Add(column);

   imgColumn.DataImageUrlField = "VITALITY_COLOR";
   imgColumn.DataImageUrlFormatString = "../../Style%20Library/OHImages/{0}";
   imgColumn.HeaderText = "Vitality Color";
   grid.Columns.Add(imgColumn);

   column = new BoundField();
   column.DataField = "VITALITY_DEFINITION";
   column.HeaderText = "Vitality Definition";
   grid.Columns.Add(column);

   column = new BoundField();
   column.DataField = "REMARKS";
   column.HeaderText = "Remarks";
   grid.Columns.Add(column);

   DesignGrid(_gridProf);
   _gridProf.Attributes.Add("Style", "text-align:center;");


 }

The DesignGrid method only defines the attributes of the gridviews. Can anyone please assist me on this incorrect rendering problem? Thanks.

A: 

CreateChildControls and ddl_SelectedIndexChanged will both be run when an item is selected from the dropdown list, and so the ProfileGrid method will add all the child controls to each GridView twice when this happens. I am not sure why this would ever result in things being tripled, though.

You don't need to recreate the grid layout in the SelectedIndexChanged event, it is already created in CreateChildControls. You only need to set the data source. Break out the first four lines of code in ProfileGrid(..) and call only that when the pick list item is selected.

Ideally you would not databind twice either, e.g. you should not databind in CreateChildControls unless you are sure that it won't happen in SelectedIndexChanged. For example you could skip the data binding there if it's a postback, assuming that there is no other reason the page would be posted to. But it would still work either way, it's just redundant/inefficient.

jamietre
Hi jamietre, thanks for the reply. I got it working just before I read your answer. And like what you have explained, I finally figured out where things should be.
janejanejane