简体   繁体   English

必填字段验证器和动态下拉列表

[英]Required field validator and dynamic drop down lists

I'm creating a data capturing page with drop down lists that are dynamically filled from the database. 我正在创建一个数据捕获页面,其中包含从数据库动态填充的下拉列表。 I have added required field validators but they do not fire, presumably because the validator thinks the first field is selected. 我添加了必填字段验证器,但未触发,原因可能是验证器认为已选择第一个字段。 Is there way around this issue? 有没有解决这个问题的方法?

Iv tried the following but I get an error: iv尝试了以下操作,但出现错误:

Object not set to instance of an object (something like that) 对象未设置为对象的实例(类似的东西)

Here is my code (at the the top of the page load event) 这是我的代码(在页面加载事件的顶部)

if (!IsPostBack)
{
    dropList.SelectedItem.Value = "";
}

Thanks! 谢谢!

Without seeing your code, my first guess would be that your required field validations may be missing the IntitialValue field, for example if the initial value of your Drop Down were something like "Please Choose an option" then your required field validator might look like: 在没有看到您的代码的情况下,我的第一个猜测是您所需的字段验证可能缺少IntitialValue字段,例如,如果下拉列表的初始值类似于“请选择一个选项”,则所需的字段验证器可能如下所示:

  <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" Display="Dynamic" ControlToValidate="ddlProvince" InitialValue="Please Choose an option"></asp:RequiredFieldValidator>

If you have already done this, then I think more information - perhaps some more code, might be helpful in working out what is going on. 如果您已经这样做了,那么我认为更多的信息-也许更多的代码,可能会有助于确定正在发生的事情。

将初始值设为0,效果很好。

Before do this :dropList.SelectedItem.Value = ""; 在此之前:dropList.SelectedItem.Value = ""; should check if ddl is empty or not. 应该检查ddl是否为空。 Otherwise the error 'Object not set to instance of an object' will come out 否则会出现错误'Object not set to instance of an object'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM