如何添加一个空的第一个条目做一个< ASP:DropDownList的&GT ;? [英] How do I add an empty first entry do an <asp:DropDownList>?

查看:102
本文介绍了如何添加一个空的第一个条目做一个< ASP:DropDownList的&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉列表中,我有一个数据源,我有的AutoPostBack 设置为真正

我想补充的第一入口,以说像数据源---选择国家---,然后选择该条目不会导致回发。

这感觉就像它应该是很容易做到的,但我不能似乎能够找到一个很好的解决方案。

谢谢你。

I have a dropdown, I have a datasource, I have AutoPostBack set to true.
I want to add a first entry to the datasource that says something like "--- select country ---" and selecting this entry won't cause postback.
This feels like it should be easy to do, yet I can't seem to be able to find a good solution.
Thanks.

推荐答案

在你的aspx页面(的重要组成部分,是处理数据绑定事件和设置的CausesValidation =true以强制一个下拉列表的验证):

In your aspx page (the important part is handling the DataBound event and setting CausesValidation="true" to force validation of a drop down list):

<asp:DropDownList ID="ddlCountries" runat="server" DataSourceID="dsCountries" AutoPostBack="true" OnDataBound="ddlCountries_DataBound" CausesValidation="true" />
<asp:RequiredFieldValidator ID="rfvCountries" runat="server" ControlToValidate="ddlCountries" Display="Dynamic" ErrorMessage="Please select a country." />

在您的codebehind(重要的是,插入项的值的String.Empty为必填字段校验工作!)

In your codebehind (it is important that the value of the inserted item is String.Empty for the required field validator to work!):

protected void ddlCountries_DataBound(Object sender, EventArgs e)
{
ddlCountries.Items.Insert(0, new ListItem("--- select country ---", String.Empty));
}

注意:如果您不希望验证的消息显示,设置显示属性设置为无

Note: If you don't want the validator's message to display, set the "Display" property to "None".

这篇关于如何添加一个空的第一个条目做一个&LT; ASP:DropDownList的&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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