|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
DataGrid Runtime DropDown - ViewState Issueissue 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 ste***@sterlingmi.com 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 elton_w***@hotmail.com Show quoteHide quote "Steve Pierce" wrote: > > 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 > ste***@sterlingmi.com > > > 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; } } Show quoteHide quote "Steve Pierce" <ste***@sterlingmi.com> wrote in message e.Item.Cells[lp.ColumnIndex].Controls[0].UniqueID;news:OwhRnBhbFHA.580@TK2MSFTNGP15.phx.gbl... > > 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] = Show quoteHide quote > } (DropDownList)e.Item.Cells[lp.ColumnIndex].Controls[0];> } > } > 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 = Show quoteHide quote > 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 > ste***@sterlingmi.com > >
Other interesting topics
Code-Behind Pain in the Behind!
Best way to disable a site? Is there server control that you load with HTML from a URL Evaluation Application How to get network user name with FORMS authentication Reading values from a Word document... Two DropDownLists getting crossed Getting more detailed errors validating How to hide aspx from URL in address bar |
|||||||||||||||||||||||