English 中文(简体)
显示选定的下拉列表
原标题:display the selected dropdown
  • 时间:2010-10-21 16:52:44
  •  标签:
  • c#
  • asp.net

我正在从数据库中检索信用卡类型,并必须在下拉菜单中显示商家使用的信用卡类型。下拉列表有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. 

请帮帮我!非常感谢。

最佳回答

看起来您的CCType是一个枚举。

以下是您要执行的操作:

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

cardtype是一个int,您可以将其强制转换为枚举类型CCType。然后将其转换为字符串,该字符串返回“Mastercard”或其他内容,而不是以前的“1”。你的下拉列表可能有名称作为它的datatext和datavalue bc,但它没有定义它。如果您的下拉菜单.DataText=“CardTypeID”或类似的内容,您会希望将所选值设置为“1”。

问题回答

ddCreditCardType.SelectedIndex允许您设置索引。

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

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

假设你只得到了所有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) 
    });
}

对不起,我已经有一段时间没有做网络表单了(或者Winforms?)

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

Good Luck, Dave

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签