I am retrieving the credit card type from Database and have to show what credit card type the merchant have us开发者_如何学Pythoned, in the dropdowm. The dropdown has 4 types like Master, Visa, American Express and Discover along with select.
I retrieve it well, but I am not sure how to bind it such that it has all the 4 types along with select but should show the creditcard that has been used.
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.
Please help me out! Thank you!
it looks like your CCType is an enum.
here's what you want to do:
ddCreditCardType.SelectedValue = ((CommonHelper.CCType) cardtype ).ToString();
cardtype is an int, you cast it to your enum type CCType. then convert it to string which returns "Mastercard" or whatever instead of "1" as before. your dropdown probably had the name as its datatext and datavalue bc it didnt have it defined. you would want to set selected value to "1" if your dropdown.DataText = "CardTypeID" or something like that.
The ddCreditCardType.SelectedIndex allows you to set the index.
string TypeOfCard = "Mastercard"; // Replace with your retrieval code
ddCreditCardType.SelectedIndex = ddCreditCardType.Items.IndexOf("Mastercard");
Note that you really must provide error checking because you could get nulls...
Assuming you've just got constants for all the CC types, I'd probably just do something like:
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)
});
}
I'm sorry, it's been a while since I've done webforms (Or Winforms?)
You'll have to double-check the properties of the list-item thing's.
Good Luck, Dave
When you build the dropdown, what is the Value in the dropdown. You can choose a text to display and the value behind each item. If your value is CommonHelper.CCType.Master), it should work.
精彩评论