S
starwiz
I'm trying to use the DataGrid's editing with a DropDownList. I've
tried using every code sample I've seen and none of them seem to be
able to solve my problem.
When I call e.Item.FindControl("name-of-my-DropDownList"), where e is a
DataGridCommandEventArgs passed to the UpdateCommand of the DataGrid, I
get an empty DropDownList. In fact, when I call
e.Item.FindControl("name-of-a-label") in the non-editable portion of my
template, I get an empty label.
ASP.Net doesn't have a problem with my accessing the DropDownList from
methods that it calls, interestingly enough. I can see the contents of
the DropDownList just fine when it calls its PreRender method and I
access the method's sender.
This is really weird...can someone lend me a hand here?
I'd really appreciate it.
-Starwiz
Here's my datagrid code (with appologies in advance to anyone whose
newsreader doesn't format this nicely):
<asp:datagrid id="DataGrid" datakeyfield="ID" (and other parameters)>
<columns>
<asp:templatecolumn headertext="Status">
<itemtemplate>
<asp:label id="lblStatus" enableviewstate="True"
runat="server" text='<%#
((System.Data.DataRowView)Container.DataItem)[StatusField] %>'>
</asp:label>
</itemtemplate>
<edititemtemplate>
<asp:dropdownlist autopostback="False"
enableviewstate="True" runat="server" id="ddlStatus"
onprerender="SetStatusIndex" />
</edititemtemplate>
</asp:templatecolumn>
<asp:editcommandcolumn buttontype="LinkButton"
updatetext="Update" canceltext="Cancel"
edittext="Edit"></asp:editcommandcolumn>
<!--The rest of the columns are auto-populated-->
</columns>
</asp:datagrid>
*********************
And my C# code:
private bool bindDataGrid = false;
private void ViewAll_PreRender(object sender, System.EventArgs e)
{
if(bindDataGrid)
RefreshDataGrid();
}
private void RefreshDataGrid()
{
//...this code doesn't matter much
//but let me know if you want to see it.
}
protected string SelectedStatus = null;
private void DataGrid_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SelectedStatus = ((Label)e.Item.FindControl("lblStatus")).Text;
//SelectedStatus is always an empty string.
DataGrid.EditItemIndex = e.Item.ItemIndex;
bindDataGrid = true;
}
protected void SetStatusIndex(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
DataTable dt = DBAccessor.GetPatientStatuses();
foreach(DataRow dr in dt.Rows)
ddl.Items.Add(new ListItem((string)dr["Status"],
((int)dr["StatusID"]).ToString()));
ddl.SelectedIndex =
ddl.Items.IndexOf(ddl.Items.FindByValue(SelectedStatus));
}
private void DataGrid_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int index = e.Item.DataSetIndex;
DataTable dt = (DataTable)Session[PatientDT];
int id = (int)dt.Rows[index][IDField];
DropDownList ddl = (DropDownList)e.Item.FindControl("ddlStatus");
int newStatus = int.Parse(ddl.SelectedValue);
//ddl.SelectedValue is always an empty string.
//In fact, ddl.Items is empty, too;
//there aren't any meaningful properties
//of the ddl that I can read at all.
DBAccessor.SetPatientStatus(id, newStatus);
DataGrid.EditItemIndex = -1;
bindDataGrid = true;
}
//Yes, this is messy. It was a failed attempt
//at fixing this problem that I just haven't gotten
//rid of yet...
private void DataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.EditItem)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("ddlStatus");
if(ddl != null)
SetStatusIndex(ddl, null);
}
else if(e.Item.ItemType == ListItemType.Item)
{
try
{
Label lbl = (Label)e.Item.FindControl("lblStatus");
lbl.Text =
(string)((System.Data.DataRowView)e.Item.DataItem)[StatusField];
}
finally {}
}
}
tried using every code sample I've seen and none of them seem to be
able to solve my problem.
When I call e.Item.FindControl("name-of-my-DropDownList"), where e is a
DataGridCommandEventArgs passed to the UpdateCommand of the DataGrid, I
get an empty DropDownList. In fact, when I call
e.Item.FindControl("name-of-a-label") in the non-editable portion of my
template, I get an empty label.
ASP.Net doesn't have a problem with my accessing the DropDownList from
methods that it calls, interestingly enough. I can see the contents of
the DropDownList just fine when it calls its PreRender method and I
access the method's sender.
This is really weird...can someone lend me a hand here?
I'd really appreciate it.
-Starwiz
Here's my datagrid code (with appologies in advance to anyone whose
newsreader doesn't format this nicely):
<asp:datagrid id="DataGrid" datakeyfield="ID" (and other parameters)>
<columns>
<asp:templatecolumn headertext="Status">
<itemtemplate>
<asp:label id="lblStatus" enableviewstate="True"
runat="server" text='<%#
((System.Data.DataRowView)Container.DataItem)[StatusField] %>'>
</asp:label>
</itemtemplate>
<edititemtemplate>
<asp:dropdownlist autopostback="False"
enableviewstate="True" runat="server" id="ddlStatus"
onprerender="SetStatusIndex" />
</edititemtemplate>
</asp:templatecolumn>
<asp:editcommandcolumn buttontype="LinkButton"
updatetext="Update" canceltext="Cancel"
edittext="Edit"></asp:editcommandcolumn>
<!--The rest of the columns are auto-populated-->
</columns>
</asp:datagrid>
*********************
And my C# code:
private bool bindDataGrid = false;
private void ViewAll_PreRender(object sender, System.EventArgs e)
{
if(bindDataGrid)
RefreshDataGrid();
}
private void RefreshDataGrid()
{
//...this code doesn't matter much
//but let me know if you want to see it.
}
protected string SelectedStatus = null;
private void DataGrid_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SelectedStatus = ((Label)e.Item.FindControl("lblStatus")).Text;
//SelectedStatus is always an empty string.
DataGrid.EditItemIndex = e.Item.ItemIndex;
bindDataGrid = true;
}
protected void SetStatusIndex(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
DataTable dt = DBAccessor.GetPatientStatuses();
foreach(DataRow dr in dt.Rows)
ddl.Items.Add(new ListItem((string)dr["Status"],
((int)dr["StatusID"]).ToString()));
ddl.SelectedIndex =
ddl.Items.IndexOf(ddl.Items.FindByValue(SelectedStatus));
}
private void DataGrid_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int index = e.Item.DataSetIndex;
DataTable dt = (DataTable)Session[PatientDT];
int id = (int)dt.Rows[index][IDField];
DropDownList ddl = (DropDownList)e.Item.FindControl("ddlStatus");
int newStatus = int.Parse(ddl.SelectedValue);
//ddl.SelectedValue is always an empty string.
//In fact, ddl.Items is empty, too;
//there aren't any meaningful properties
//of the ddl that I can read at all.
DBAccessor.SetPatientStatus(id, newStatus);
DataGrid.EditItemIndex = -1;
bindDataGrid = true;
}
//Yes, this is messy. It was a failed attempt
//at fixing this problem that I just haven't gotten
//rid of yet...
private void DataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.EditItem)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("ddlStatus");
if(ddl != null)
SetStatusIndex(ddl, null);
}
else if(e.Item.ItemType == ListItemType.Item)
{
try
{
Label lbl = (Label)e.Item.FindControl("lblStatus");
lbl.Text =
(string)((System.Data.DataRowView)e.Item.DataItem)[StatusField];
}
finally {}
}
}