Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 显示所选下拉列表_C#_Asp.net - Fatal编程技术网

C# 显示所选下拉列表

C# 显示所选下拉列表,c#,asp.net,C#,Asp.net,我正在从数据库中检索信用卡类型,并且必须在dropdowm中显示商户使用的信用卡类型。下拉列表有4种类型,如Master、Visa、American Express和Discover以及select 我很好地检索了它,但我不确定如何绑定它,使它具有所有4种类型以及select,但应该显示已使用的信用卡 if (cardtype == 1) { ddCreditCardType.SelectedValue = ((int)Comm

我正在从数据库中检索信用卡类型,并且必须在dropdowm中显示商户使用的信用卡类型。下拉列表有4种类型,如Master、Visa、American Express和Discover以及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. 

请帮帮我!谢谢大家!

构建下拉列表时,下拉列表中的值是多少。您可以选择要显示的文本以及每个项目后面的值。如果您的值是CommonHelper.CCType.Master),它应该可以工作。

ddCreditCardType.SelectedIndex允许您设置索引

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

请注意,您确实必须提供错误检查,因为您可能会得到null…

看起来您的CCType是一个枚举

以下是您想要做的:

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

cardtype是一个int,您可以将其强制转换为枚举类型CCType。然后将其转换为字符串,返回“Mastercard”或其他内容,而不是像以前一样返回“1”。您的下拉列表可能将名称作为其datatext,而datavalue没有定义。如果您的dropdown.DataText=“CardTypeID”或类似的内容,您可能希望将所选值设置为“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?)

你必须仔细检查列表项的属性

祝你好运,
戴夫

你们都太棒了!我真的很感激你们。多谢各位!