Datagrid TemplateColumn and Select list

Datagrid TemplateColumn and Select list

Post by John Lehman » Fri, 28 Feb 2003 09:27:23



I have created a datagrid where I basically load a
<Select> control (which is rendered from a user control).
I want to be able to get the value of what is selected in
the dropdown when I post back.
When I get back to the server and try to get the value I
can not seem to access the Select control to get the
selected value. In the documentaion they convert a
TextBox control [e.g. TextBox priceText = (TextBox)
e.Item.Cells[3].Controls[0];]. I have tried building the
control using everytype of list box (
HtmlControls.HtmlSelect , asp:ListBoX ).
When I try and do this with Listbox.. I get that it is an
invalid cast. The imediate window shows it as a
LiteralControl, but If I change the cast to
LiteralControl, I can't get access to the selected item:
Any ideas?

public void the_UpdateCmd(Object sender,
DataGridCommandEventArgs e)
{
ListBox StatusCtl = (ListBox)e.Item.Cells[12].Controls[0];
string selectedval = StatusCtl.SelectedItem.Value;

//------- My template column -----
<asp:TemplateColumn>
<HeaderTemplate><b>Status</b></HeaderTemplate>
<ItemTemplate><uc1:ShrinkStatus id="ShrinkStatusdd"
SelectText='<%# DataBinder.Eval
(Container.DataItem, "ShrinkStatus") %>' SelectValue='<%#
DataBinder.Eval(Container.DataItem, "ShrinkStatusID") %>'
runat="server"></uc1:ShrinkStatus>
</ItemTemplate>
</asp:TemplateColumn>