我正在使用下面的代码从gridview中提取数据并将其填充到日期的文本框中,以及项目和类别的两个下拉列表。
对于gridview中的某些行,除了类别ddl之外的所有行都正确填充。如果我再次单击该行,则类别ddl将显示正确的类别。
任何人都可以告诉我为什么我要为某些行点击两次?我该如何解决这个问题?
谢谢
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//// Get the currently selected row using the SelectedRow property.
GridViewRow row = GridView1.SelectedRow;
txtSunday.Text = (row.Cells[6].Controls[0] as DataBoundLiteralControl).Text.Trim();
txtMonday.Text = (row.Cells[7].Controls[0] as DataBoundLiteralControl).Text.Trim();
txtTuesday.Text = (row.Cells[8].Controls[0] as DataBoundLiteralControl).Text.Trim();
txtWednesday.Text = (row.Cells[9].Controls[0] as DataBoundLiteralControl).Text.Trim();
txtThursday.Text = (row.Cells[10].Controls[0] as DataBoundLiteralControl).Text.Trim();
txtFriday.Text = (row.Cells[11].Controls[0] as DataBoundLiteralControl).Text.Trim();
txtSaturday.Text = (row.Cells[12].Controls[0] as DataBoundLiteralControl).Text.Trim();
// Set ProjectList ddl to Project in selected row
if (ProjectList.Items.FindByText(row.Cells[2].Text.Trim()) != null)
{
ProjectList.ClearSelection();
ProjectList.Items.FindByText(row.Cells[2].Text.Trim()).Selected = true;
}
/// This is the ddl that doesn't always populate correctly unless you click the
/// gridview row selector twice
// Set CategoryList ddl to Category in selected row
if (CategoryList.Items.FindByText(row.Cells[4].Text.Trim()) != null)
{
CategoryList.ClearSelection();
CategoryList.Items.FindByText(row.Cells[4].Text.Trim()).Selected = true;
}
}
答案 0 :(得分:0)
我不确定为什么要点击两次才能让你的下拉列表正确选择,但这可能与回发事件排序/ ViewState问题有关。您可能想要考虑的一件事是使用绑定网格的数据而不是网格中控件的文本。 IOW,假设您绑定到这样的对象集合:
public class ProjectSchedule
{
public string Project {get;set;}
public int CategoryId {get;set;}
public string Category {get;set;}
public string Sunday {get;set;}
public string Monday {get;set;}
public string Tuesday {get;set;}
public string Wednesday {get;set;}
public string Thursday {get;set;}
public string Friday {get;set;}
public string Saturday {get;set;}
}
然后,在SelectedIndexChanged
事件处理程序中,获取如下数据:
GridViewRow row = GridView1.SelectedRow;
ProjectSchedule ps = row.DataItem as ProjectSchedule;
if (ps != null)
{
txtSunday.Text = ps.Sunday;
// the rest of the days...
ListItem categoryItem = CategoryList.Items.FindByText(ps.Category);
if (categoryItem != null)
{
CategoryList.ClearSelection();
categoryItem.Selected = true;
}
// same with ProjectList
}
假设您的控件每次都限制可维护性,将在同一列中着陆。例如,假设需求发生变化,表示带有日期的列位于“项目”列之前。这是很多要改变的指数。
如果你有你的类别和什么索引的东西会更好(例如,我偷偷进入上面的CategoryId
对象的ProjectSchedule
属性),那么你可以按值查找项目而不是通过文本,减轻另一个失败点。
答案 1 :(得分:0)
我想我想出来了。我需要在设置项目后重新绑定类别ddl
// Set ProjectList ddl to Project in selected row
if (ProjectList.Items.FindByText(row.Cells[2].Text.Trim()) != null)
{
ProjectList.ClearSelection();
ProjectList.Items.FindByText(row.Cells[2].Text.Trim()).Selected = true;
}
// Set CategoryList ddl to Category in selected row
// I added this line and it seems to work now
CategoryList.DataBind();
if (CategoryList.Items.FindByText(row.Cells[4].Text.Trim()) != null)
{
CategoryList.ClearSelection();
CategoryList.Items.FindByText(row.Cells[4].Text.Trim()).Selected = true;
}