数据绑定下拉列表 - 初始值 [英] Databound drop down list - initial value

查看:117
本文介绍了数据绑定下拉列表 - 初始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置一个数据绑定的初始值下拉列表中的ASP.NET?

How do I set the initial value of a databound drop down list in ASP.NET?

比如说,我要的值,但显示的第一个值应该是 - 选择一个---与空值

For instance, I want the values, but the first value to display should be -- Select One ---, with a null value.

推荐答案

我想你想做的事是这样的:

I think what you want to do is this:

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
    <asp:ListItem Text="--Select One--" Value="" />   
</asp:DropDownList>

确认了AppendDataBoundItems设置为真正或否则当你绑定你的数据,你会清除 - 选择One--列表项。

Make sure the 'AppendDataBoundItems' is set to true or else you will clear the '--Select One--' list item when you bind your data.

如果您有下拉设置列表的AutoPostBack属性的真实你必须还设置了'的CausesValidation属性的真实然后用'的RequiredFieldValidator,以确保 - 选择One--选项不会导致回发。

If you have the 'AutoPostBack' property of the drop down list set to true you will have to also set the 'CausesValidation' property to true then use a 'RequiredFieldValidator' to make sure the '--Select One--' option doesn't cause a postback.

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1"></asp:RequiredFieldValidator>

这篇关于数据绑定下拉列表 - 初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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