2010-10-21 92 views
1

我從數據庫檢索信用卡類型,並且必須在下拉列表中顯示商戶使用的信用卡類型。下拉菜單包括Master,Visa,American Express和Discover等4種類型以及select。顯示選定的下拉列表

我檢索得很好,但我不確定如何綁定它,使其具有所有4種類型以及select,但應顯示已使用的信用卡。

if (cardtype == 1) 
{        
    ddCreditCardType.SelectedValue = ((int)CommonHelper.CCType.Master).ToString();         
} 

((int)CommonHelper.CCType.Master).ToString(); 
//This part gets the type of card used but does not put in the ddCreditCardType. 

請幫幫我!謝謝!

+1

你們都只是真棒!我非常感謝你們。非常感謝! – Ram 2010-10-21 18:35:23

回答

1

它看起來像你的CCType是一個枚舉。

這裏是你想要做什麼:

ddCreditCardType.SelectedValue = ((CommonHelper.CCType) cardtype).ToString();         

cardtype是一個int,你把它投放到您的枚舉類型CCType。然後將其轉換爲返回「萬事達卡」或其他類似字符串的字符串,而不是「1」。您的下拉菜單可能具有名稱作爲其datatext和數據值bc它沒有定義它。如果你的dropdown.DataText =「CardTypeID」或類似的東西,你會想將選定的值設置爲「1」。

0

當您構建下拉列表時,下拉列表中的值是多少。您可以選擇要顯示的文本以及每個項目後面的值。如果你的值是CommonHelper.CCType.Master),它應該工作。

1

ddCreditCardType.SelectedIndex允許您設置索引。

string TypeOfCard = "Mastercard"; // Replace with your retrieval code  
ddCreditCardType.SelectedIndex = ddCreditCardType.Items.IndexOf("Mastercard"); 

注意,你真的必須提供錯誤檢查,因爲你可以得到空...

1

假設你剛剛得到了所有的CC類型的常量,我可能只是這樣做:

var selectedCardId = ??; 

//Make an array of all the card types (this can be a constant) 
var cardTypes = new CommonHelper.CCType[]{CommonHelper.CCType.Master, CommonHelper.CCType.Visa, CommonHelper.CCType.Express, CommonHelper.CCType.Whatever}; 

//Loop through, and build the drop-down 
foreach(var card in cardTypes) 
{ 
    ddCreditCardType.Items.Add(new ListItem 
    { 
     Value = ((int)card).ToString(), 
     Text = card.ToString(), 
     IsSelected = (selectedCardId == (int)card) 
    }); 
} 

對不起,它已經有一段時間,因爲我做的WebForms(或Winforms?)

您必須仔細檢查列表項目的屬性。

好運, 戴夫

相關問題