This message was discovered on ASPFriends.com 'aspngcontrolscs' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
| Ray Houston |
(If there is a better list to post this problem, please let me know.) I'm building a control that contains a DataGrid control which has a Template column which contains a DropDownList. The DropDownList renders fine with it's DataSource, but I would like to add a blank row to the items and select a default item.
He is a stripped down code of my control:
protected override void OnInit(EventArgs E) { myTemplateColumn = new TemplateColumn(); myTemplateColumn.HeaderText = "Role"; myTemplateColumn.ItemTemplate = new RoleColumn(); SourceDataGrid.Columns.Add(myTemplateColumn); base.OnInit(E); } protected override void CreateChildControls() { SourceDataGrid.SortCommand += new DataGridSortCommandEventHandler(this.Sort); SourceDataGrid.ItemDataBound += new DataGridItemEventHandler(this.ItemDataBound);
this.Controls.Add(SourceDataGrid); }
Here is my ITemplate class that builds the DropDown in the DataGrid:
public class RoleColumn : ITemplate { public void InstantiateIn(Control container) { DropDownList myDropDownList = new DropDownList(); myDropDownList.ID = "RoleID"; myDropDownList.DataSource = BaseDB.GetList(0,"Role"); myDropDownList.DataTextField = "Text"; myDropDownList.DataValueField = "Value"; myDropDownList.CssClass = "formTB"; myDropDownList.DataBinding += new EventHandler(this.BindRoleColumn);
container.Controls.Add(new LiteralControl("<center>")); container.Controls.Add(myDropDownList); container.Controls.Add(new LiteralControl("</center>"));
} public void BindRoleColumn(object sender, EventArgs e) { int SelectedRoleID; DropDownList dropdownlist = (DropDownList)sender;
dropdownlist.Items.Insert(0, " "); dropdownlist.Items[0].Value = "0";
DataGridItem container = (DataGridItem)dropdownlist.NamingContainer; SelectedRoleID = Convert.ToInt32(((DataRowView)container.DataItem)["RoleID"]);
if (dropdownlist.Items.FindByValue(SelectedRoleID.ToString()) != null) dropdownlist.Items.FindByValue(SelectedRoleID.ToString()).Selected = true; } }
It doesn't even add the new Item to the DropDownList. I had though that my 'BindRoleColumn' wasing even being executed, but I force an exception there and it throws it. Thanks for the help.
-Ray
|
|
| |
| | |
| |
| Andrea Williams |
I'm having the same issue, however I've discovered that the Item is being added, but not showing up in the list. The reason I think it's added is that if my DataSet value that is to be bound with the control is equal to "", then I don't get an error. Howver, if I take out the line:
cboSender.Items.Insert(0, new ListItem("[Select One]", ""));
then the page errors b/c there's not a value with which to match an item in the DropDownList.
I don't see a date on this post so I'm not sure when it was posted, but I really need this to work!
My ITemplate code is as follows: /// <summary> Instantiation of the Item Template</summary> public override void InstantiateIn(Control container) { // Bind a string to the event DropDownList cboResult = new DropDownList();
if (this.ControlID != null) cboResult.ID = this.ControlID;
cboResult.DataSource = this.mobjDataSource; cboResult.DataTextField = this.mstrDataTextField; cboResult.DataValueField = this.mstrDataValueField;
cboResult.DataBinding += new EventHandler(this.BindData);
container.Controls.Add(cboResult); }
/// <summary> Binds the DataField to the specified Label</summary> protected void BindData(object sender, System.EventArgs e) { if (!Common.Global.isNothing(this.DataField)) { DropDownList cboSender = (DropDownList) sender;
DataGridItem container = (DataGridItem) cboSender.NamingContainer; string strDataFieldValue = ((System.Data.DataRowView)container.DataItem)[this.DataField].ToString();
cboSender.Items.Insert(0, new ListItem("[Select One]", ""));//This is the line that isn't working // BTW, if I leave this line out and the value of strDataFieldValue = "", I get an error b/c the value "" doesn't exist in the DataSet.
cboSender.SelectedValue = strDataFieldValue;
cboSender = null; } }
-------------------------------- From: Andrea Williams
|
|
| |
|
| | |
|
|
|
|