I have the following line in my code:
cmd.Parameters.Add("@priority", SqlDbType.VarChar).Value = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).SelectedValue;
But when I run this, I get the following error:
Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.DropDownList'.
Any idea why this might be?
Relevant part of the GridView would be this:
<asp:TemplateField HeaderText="Priority">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("priority") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
SelectedValue='<%# Bind("priority") %>'>
<asp:ListItem Value="0">Low</asp:ListItem>
<asp:ListItem Value="1">Normal</asp:ListItem>
<asp:ListItem Value="2">High</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
Since your
DropDownListmost likely is in aTemplateFieldyou can get it’s reference viaFindControl("ID"):