2012-04-10 69 views
1

我在ListView裏面有一個DropDownList .. 我想在點擊命令時得到一個數據。 這是我的代碼..如何從ListDown中的DropDownList獲取值?

protected void ListView2_ItemCommand(object sender, ListViewCommandEventArgs e){ 

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

} 

但它總是返回空值.. 我使用Google約3小時,並嘗試了很多功能.. ,但不能仍然解決這個bug ..

請幫我傢伙,

UPDATE 這裏是我的aspx頁面

<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> 
+2

也許是因爲你拼錯公司.. – 2012-04-10 07:24:38

+0

只是另一個原因是魔術字符串是壞:-) – Bridge 2012-04-10 07:26:26

+0

你肯定列表視圖不會在回發 – ViSu 2012-04-10 07:28:24

回答

0

你有設置頭儘量

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; 
+0

它不工作.. :( – vantian 2012-04-10 08:03:16

+0

應該設置runat(我認爲它應該已經設置,或者migth給出一些編譯錯誤).try將autopostback設置爲true – 2012-04-10 08:09:05

0

試試這個:

DropDownList ddl = (DropDownList)e.Item.FindControl("ShippingComapnyDDL"); 
    if (ddl != null) 
    { 
     //code here 
    } 
相關問題