DataGrid Runtime DropDown - ViewState Issue

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)
 
G

Guest

Hi Steve,

I think you might misunderstand something. In a datagrid, a datagrid item
becomes an so-called EditItem only when you assign it as EditItem, e.g. in
Edit event:

DataGrid.EditItemIndex = e.Item.ItemIndex
Datagrid.DataSource = data_source_object
Datagrid.DataBind()

Then the item, index as e.Item.ItemIndex becomes EditItem.

In ItemDataBound event, there are only two kinds of data-bound items,
ListItemType.Item and ListItemType.AlternatingItem.


HTH

Elton Wang
(e-mail address removed)


Steve Pierce said:
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)
 
S

Steve Pierce

Daniel Roth responded to my message, the selected value in viewstate can be
pulled with the addition of the following line:
ddl.SelectedValue = Request.Form.Get((string)ViewState[controlName]);

below is the corrected code called from ItemCommand e.CommandName ==
"Update":

foreach (LookupProperties lp in GetLookupCollection().Items)
{
int verId = Convert.ToInt32(DataGrid1.DataKeys[e.Item.ItemIndex]);
DataRow dr = PageDataSet.Version.FindByVerId(verId);
string controlName = "ddl_" + verId + "_" + lp.ColumnIndex;
// create and populate dropdownlist
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()));
else if (lov.LovLolId == lp.LolId && lov.LovId < 0)
ddl.Items.Add(new ListItem("", lov.LovId.ToString()));
// replace default textbox control with new dropdownlist
e.Item.Cells[lp.ColumnIndex].Controls.Clear();
e.Item.Cells[lp.ColumnIndex].Controls.AddAt(0, ddl);
// select current value based on viewstate
ddl.SelectedValue = Request.Form.Get((string)ViewState[controlName]);
// compare dropdownlist selectedvalue to dataset, update if changed
if (Convert.ToInt32(ddl.SelectedValue) < 0)
{
if (dr[lp.ColumnName].GetType() != typeof(DBNull))
dr[lp.ColumnName] = DBNull.Value;
}
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;
}
}

Steve Pierce said:
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top