S
Steve Pierce
I am having some issues with a runtime dropdownlist in a datagrid. The
issue is that I cannot get ViewState to fill the selected index of a runtime
dropdown properly on postback. I do not want to use template columns as they
seem to be a little difficult to create at runtime. Any assistance would be
very greatly appreciated.
private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
// display all LooId values with related value in LookupValue for display
foreach (LookupProperties lp in GetLookupCollection().Items)
{
// set the text of the cell based on the dataset
DataRow dr = ((DataRowView)e.Item.DataItem).Row;
int lovId = Convert.ToInt32(dr[lp.ColumnName]);
e.Item.Cells[lp.ColumnIndex].Text = GetLookupValue(lovId);
}
}
else if (e.Item.ItemType == ListItemType.EditItem)
{
// change all LooId columns from textbox to dropdownlist for edit
foreach (LookupProperties lp in GetLookupCollection().Items)
{
// create and populate dropdownlist
DataRow dr = ((DataRowView)e.Item.DataItem).Row;
string controlName = "ddl_" + dr[dr.Table.PrimaryKey[0]] + "_" +
lp.ColumnIndex;
DropDownList ddl = new DropDownList();
ddl.ID = controlName;
ddl.CssClass = "DropDownList";
ddl.EnableViewState = true;
foreach (LookupValueRow lov in FrameworkDataSet.LookupValue)
if (lov.LovLolId == lp.LolId && lov.LovId > 0)
ddl.Items.Add(new ListItem(lov.LovDisplayValue, lov.LovId.ToString()));
// select current value based on dataset
ddl.SelectedValue = dr[lp.ColumnName].ToString();
// replace default textbox control with new dropdownlist
e.Item.Cells[lp.ColumnIndex].Controls.Clear();
e.Item.Cells[lp.ColumnIndex].Controls.AddAt(0, ddl);
ViewState[controlName] = e.Item.Cells[lp.ColumnIndex].Controls[0].UniqueID;
}
}
}
private void DataGrid1_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Header) ||
(e.Item.ItemType == ListItemType.Footer) ||
(e.Item.ItemType == ListItemType.Separator))
return;
if (e.CommandName != "Update")
return;
// update dataset for all LooId columns with dropdownlist selectedvalue
foreach (LookupProperties lp in GetLookupCollection().Items)
{
// compare dropdownlist selectedvalue to dataset, update if changed
int verId = Convert.ToInt32(DataGrid1.DataKeys[e.Item.ItemIndex]);
DataRow dr = PageDataSet.Version.FindByVerId(verId);
string controlName = "ddl_" + verId + "_" + lp.ColumnIndex;
string uniqueName2 = ViewState[controlName].ToString();
// error: selecteditems are not correct from viewstate
DropDownList ddl = new DropDownList();
ddl.ID = controlName;
ddl.CssClass = "DropDownList";
ddl.EnableViewState = true;
foreach (LookupValueRow lov in FrameworkDataSet.LookupValue)
if (lov.LovLolId == lp.LolId && lov.LovId > 0)
ddl.Items.Add(new ListItem(lov.LovDisplayValue, lov.LovId.ToString()));
e.Item.Cells[lp.ColumnIndex].Controls.Clear();
e.Item.Cells[lp.ColumnIndex].Controls.AddAt(0, ddl);
DropDownList ctl1 = (DropDownList)e.Item.Cells[lp.ColumnIndex].Controls[0];
DropDownList ctl2 = (DropDownList)e.Item.FindControl(controlName);
DropDownList ctl3 = (DropDownList)this.FindControl(uniqueName2);
if (ddl.SelectedItem == null)
{
if (dr[lp.ColumnName].GetType() != typeof(DBNull))
dr[lp.ColumnName] = DBNull.Value;
else
continue;
}
else
{
if (dr[lp.ColumnName].GetType() == typeof(DBNull))
dr[lp.ColumnName] = ddl.SelectedValue;
else if (dr[lp.ColumnName].ToString() != ddl.SelectedValue)
dr[lp.ColumnName] = ddl.SelectedValue;
else
continue;
}
}
}
Steve Pierce
Sterling Computer Consultants
(e-mail address removed)
issue is that I cannot get ViewState to fill the selected index of a runtime
dropdown properly on postback. I do not want to use template columns as they
seem to be a little difficult to create at runtime. Any assistance would be
very greatly appreciated.
private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
// display all LooId values with related value in LookupValue for display
foreach (LookupProperties lp in GetLookupCollection().Items)
{
// set the text of the cell based on the dataset
DataRow dr = ((DataRowView)e.Item.DataItem).Row;
int lovId = Convert.ToInt32(dr[lp.ColumnName]);
e.Item.Cells[lp.ColumnIndex].Text = GetLookupValue(lovId);
}
}
else if (e.Item.ItemType == ListItemType.EditItem)
{
// change all LooId columns from textbox to dropdownlist for edit
foreach (LookupProperties lp in GetLookupCollection().Items)
{
// create and populate dropdownlist
DataRow dr = ((DataRowView)e.Item.DataItem).Row;
string controlName = "ddl_" + dr[dr.Table.PrimaryKey[0]] + "_" +
lp.ColumnIndex;
DropDownList ddl = new DropDownList();
ddl.ID = controlName;
ddl.CssClass = "DropDownList";
ddl.EnableViewState = true;
foreach (LookupValueRow lov in FrameworkDataSet.LookupValue)
if (lov.LovLolId == lp.LolId && lov.LovId > 0)
ddl.Items.Add(new ListItem(lov.LovDisplayValue, lov.LovId.ToString()));
// select current value based on dataset
ddl.SelectedValue = dr[lp.ColumnName].ToString();
// replace default textbox control with new dropdownlist
e.Item.Cells[lp.ColumnIndex].Controls.Clear();
e.Item.Cells[lp.ColumnIndex].Controls.AddAt(0, ddl);
ViewState[controlName] = e.Item.Cells[lp.ColumnIndex].Controls[0].UniqueID;
}
}
}
private void DataGrid1_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Header) ||
(e.Item.ItemType == ListItemType.Footer) ||
(e.Item.ItemType == ListItemType.Separator))
return;
if (e.CommandName != "Update")
return;
// update dataset for all LooId columns with dropdownlist selectedvalue
foreach (LookupProperties lp in GetLookupCollection().Items)
{
// compare dropdownlist selectedvalue to dataset, update if changed
int verId = Convert.ToInt32(DataGrid1.DataKeys[e.Item.ItemIndex]);
DataRow dr = PageDataSet.Version.FindByVerId(verId);
string controlName = "ddl_" + verId + "_" + lp.ColumnIndex;
string uniqueName2 = ViewState[controlName].ToString();
// error: selecteditems are not correct from viewstate
DropDownList ddl = new DropDownList();
ddl.ID = controlName;
ddl.CssClass = "DropDownList";
ddl.EnableViewState = true;
foreach (LookupValueRow lov in FrameworkDataSet.LookupValue)
if (lov.LovLolId == lp.LolId && lov.LovId > 0)
ddl.Items.Add(new ListItem(lov.LovDisplayValue, lov.LovId.ToString()));
e.Item.Cells[lp.ColumnIndex].Controls.Clear();
e.Item.Cells[lp.ColumnIndex].Controls.AddAt(0, ddl);
DropDownList ctl1 = (DropDownList)e.Item.Cells[lp.ColumnIndex].Controls[0];
DropDownList ctl2 = (DropDownList)e.Item.FindControl(controlName);
DropDownList ctl3 = (DropDownList)this.FindControl(uniqueName2);
if (ddl.SelectedItem == null)
{
if (dr[lp.ColumnName].GetType() != typeof(DBNull))
dr[lp.ColumnName] = DBNull.Value;
else
continue;
}
else
{
if (dr[lp.ColumnName].GetType() == typeof(DBNull))
dr[lp.ColumnName] = ddl.SelectedValue;
else if (dr[lp.ColumnName].ToString() != ddl.SelectedValue)
dr[lp.ColumnName] = ddl.SelectedValue;
else
continue;
}
}
}
Steve Pierce
Sterling Computer Consultants
(e-mail address removed)