[SOLVED] How to get Value from DropDownList inside a ListView?

Issue

I have a DropDownList inside in ListView..
I wanted to get a data when command clicked.
this is my code..

protected void ListView2_ItemCommand(object sender, ListViewCommandEventArgs e){

string shipmethod = ((DropDownList)e.Item.FindControl("ShippingComapnyDDL")).SelectedValue;

}

but it always return null value..
I’ve googling about 3 hours, and try many function..
but still cant solve this bug..

please help me guys,

UPDATE
here’s my aspx page

 <asp:DropDownList ID="ShippingComapnyDDL"  runat="server" SelectedValue='<%# Eval("ShippingCompany") %>'>
<asp:ListItem Text="" Value=""></asp:ListItem>
<asp:ListItem Text="FedEx" Value="FedEx"></asp:ListItem>
<asp:ListItem Text="UPS" Value="UPS"></asp:ListItem>
<asp:ListItem Text="Other" Value="Other"></asp:ListItem>
</asp:DropDownList>

Solution

DO you have headers set?try

    if(e.Item.ItemIndex!=-1)
{
     string shipmethod = ((DropDownList)e.Item.FindControl("ShippingComapnyDDL")).SelectedValue; 
}

if not working then try     
string shipmethod = (e.Item.FindControl("ShippingComapnyDDL") as DropDownList).SelectedValue; 

Answered By – Krishnanunni Jeevan

Answer Checked By – Senaida (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *