Thanks. This was fantastic - I can now access the DropDownList from within
code.
However still don't seem to be able to populate the List from a database
source.
I know this source works, because it is populating a DropDownList that is
NOT in a DataGrid, but refuses to work for the one inside the data list.
I can successfully populate the list manually but not from datasource.
Is there some trick for databinding inside a databind event ?
Examples
1) works fine, 2) fails (empty drop down list)
1) FINE
private void dgMachines_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{DataSet dsMachineDDL = machine.GetMachineDataList();
DropDownList ddlDom = (DropDownList) e.Item.FindControl("ddlDomain");
ddlDom.Items.Add("hooja");
ddlDom.Items.Add("wotsit");}
}
2) EMPTY DROPDOWNLIST
private void dgMachines_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{DataSet dsMachineDDL = machine.GetMachineDataList();
DropDownList ddlDom = (DropDownList) e.Item.FindControl("ddlDomain");
ddlDom.DataSource = dsMachineDDL;
ddlDom.DataMember = "Table1";
ddlDom.DataTextField = "mc_domain";
ddlDom.DataValueField = "mc_domain";
ddlDom.DataBind();}
}
Earl Teigrob said:
You must create an event handler like this:
this.DataGrid1.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBo
und);
for the binding event and then you can use code like this to populate your
Drop Down. In this example, once I have ManagerId2 you can do whatever with
it.
Earl
private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.EditItem)
{
//GET THE DATAROW
System.Data.Common.DbDataRecord DataRow =
(System.Data.Common.DbDataRecord)e.Item.DataItem;
//POPULATE AND SET SELECTED INDEX FOR MANAGER DROPDOWN LIST
String pkManager = Common.MakeStringOr(DataRow["pkManager"], null);
DropDownList ManagerId2 = (DropDownList) e.Item.FindControl("ManagerId2");
CreateManagerDropdownList(ref ManagerId2);
if (pkManager!=null)
ManagerId2.Items.FindByValue(pkManager).Selected=true;
//SET SELECTED VALUE FOR ISMANAGER
CheckBox IsManager2 = (CheckBox) e.Item.FindControl("IsManager2");
String IsManager = DataRow["IsManager"].ToString();
IsManager2.Checked=Common.MakeBoolOr(IsManager,false);
}
}
Richard said:
yes that's exactly what I'm looking for, although I don't see a databind
event.
I've got events for CancelCommand, EditCommand, SortCommand and
UpdateCommand and there are databind commands in a few of these
places.
Also
since last message I've discovered that my ddlTest2 code does not
cause
this
error in CancelCommand and UpdateCommand - but does in EditCommand.
(By the way I have no problem populating a normal DropDownList - it's only
in the Template column that the problem arises)
Here's a simplified version of the EditCommand - you say to populate the
drop down menu in the the databind event - how would it fit into here ?
private void dgMachinesItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
strOrderBy = (string)ViewState["OrderBy"];
this.dgMachines.EditItemIndex = e.Item.ItemIndex;
this.dgMachines.DataSource =
machine.GetMachines(strFilter, strOrderBy);
this.dgMachines.DataBind();
}
Thanks
I normally populate the dropdownlist in a edit template during the
databind
event so that when I click on edit, it is populated and the selected index
is already set to the current value. Is this what you are looking
for