在asp.net中下拉列表中值 [英] drop down list value in asp.net

查看:349
本文介绍了在asp.net中下拉列表中值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要一个值添加到下拉列表中无法选择的,就像一个冠军。
例如:我有一个月的下拉列表中。的第一项应该是选择月这不应该被选中。第二个为1〜12月。我该怎么做?

这是我目前的code。

 字符串selectmonth =SELECT * FROM tblmonth
的SqlCommand scmselect =新的SqlCommand(selectmonth,scnbuboy);SqlDataReader的sdrselect = scmselect.ExecuteReader();drmonth.DataTextField =月;
drmonth.DataValueField =monthID;
drmonth.DataSource = sdrselect;drmonth.DataBind();


解决方案

 < ASP:DropDownList的ID =DdlMonths=服务器>
    < ASP:列表项启用=真正的文本=选择月VALUE = - 1>< / ASP:ListItem的>
    < ASP:ListItem的文本=一月VALUE =1>< / ASP:ListItem的>
    < ASP:ListItem的文本=二月VALUE =2>< / ASP:ListItem的>
    ....
    < ASP:ListItem的文本=十二月VALUE =12>< / ASP:ListItem的>
< / ASP:DropDownList的>

您甚至可以使用的RequiredFieldValidator 这忽略该项,则认为它是不选。

 < ASP:的RequiredFieldValidator ID =ReqMonth=服务器的ControlToValidate =DdlMonths
    与InitialValue = - 1>
< / ASP:&的RequiredFieldValidator GT;

I want to add a value to the drop down list that cannot be selected, like a title. Ex: I have a month drop down list. The very first item should be "select month" this should not be selected. And next is from January to december. How can I do that?

And this is my current code.

string selectmonth = "select * from tblmonth";
SqlCommand scmselect = new SqlCommand(selectmonth, scnbuboy);

SqlDataReader sdrselect = scmselect.ExecuteReader();

drmonth.DataTextField = "month";
drmonth.DataValueField = "monthID";
drmonth.DataSource = sdrselect;

drmonth.DataBind();

解决方案

<asp:DropDownList ID="DdlMonths" runat="server">
    <asp:ListItem Enabled="true" Text="Select Month" Value="-1"></asp:ListItem>
    <asp:ListItem Text="January" Value="1"></asp:ListItem>
    <asp:ListItem Text="February" Value="2"></asp:ListItem>
    ....
    <asp:ListItem Text="December" Value="12"></asp:ListItem>
</asp:DropDownList>

You can even use a RequiredFieldValidator which ignore this item, it considers it as unselected.

<asp:RequiredFieldValidator ID="ReqMonth" runat="server" ControlToValidate="DdlMonths"
    InitialValue="-1">
</asp:RequiredFieldValidator>

这篇关于在asp.net中下拉列表中值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆