DropDownList.SelectedIndexChanged 错误地渲染生成的 GridView 代码(双倍、三倍等...)
我有一个下拉列表,控制 3 个 gridview 控件的内容。这些在 Web 部件中使用。但是,每次下拉列表的 selectedindexchanged 方法触发事件时,依赖的 gridview 的内容都会添加另一个 gridview 渲染。因此,它使这些网格视图的内容增加了一倍甚至三倍。
我有以下代码:
-> 对于 CreatChildControls 方法:
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 />"));
-> 对于 SelectedIndexChanged 方法:
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);
}
-> 对于 GridView 控件之一:
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;");
}
DesignGrid 方法仅定义 gridview 的属性。 谁能帮助我解决这个不正确的渲染问题? 谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当从下拉列表中选择一个项目时,CreateChildControls 和 ddl_SelectedIndexChanged 都会运行,因此当发生这种情况时,ProfileGrid 方法会将所有子控件添加到每个 GridView 中两次。不过,我不确定为什么这会导致事情增加两倍。
您无需在 SelectedIndexChanged 事件中重新创建网格布局,它已在 CreateChildControls 中创建。只需要设置数据源即可。分解 ProfileGrid(..) 中的前四行代码,并仅在选择选择列表项时调用该代码。
理想情况下,您也不会进行两次数据绑定,例如,您不应在 CreateChildControls 中进行数据绑定,除非您确定它不会在 SelectedIndexChanged 中发生。例如,如果是回发,您可以跳过那里的数据绑定,假设没有其他原因将页面发布到。但无论如何它仍然可以工作,只是多余/低效。
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.