【发布时间】:2017-09-01 01:27:29
【问题描述】:
我有一个从 SQL 数据库填充的下拉菜单,默认值为:(选择开发人员)。我有一个表单向导,向导的第二步是选择开发人员。
我想验证这个下拉菜单;如果用户将其保留为默认值(选择开发人员),则应出现错误消息。
我收到了这个错误:
WebForms UnobtrusiveValidationMode 需要 ScriptResourceMapping 对于'jquery'。请添加一个名为 ScriptResourceMapping jquery(区分大小写)。
以下是我的代码:
.aspx:
<asp:DropDownList ID="developers_DropDownList" name="developers_DropDownList" class="form-control col-md-7 col-xs-12" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredValidator1" runat="server"
ErrorMessage="Please select a value!"
ControlToValidate="developers_DropDownList"
InitialValue="Select Developer"></asp:RequiredFieldValidator>
.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindDevelopers();
}
}
protected void BindDevelopers()
{
SqlConnection con = new SqlConnection("Data Source=RC-8573\\SQLEXPRESS;Initial Catalog=DataManagment;Integrated Security=SSPI");
con.Open();
SqlCommand com = new SqlCommand("select * from Users WHERE role = 'Designer'", con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds); // fill dataset
developers_DropDownList.DataTextField = ds.Tables[0].Columns["user_name"].ToString();
developers_DropDownList.DataValueField = ds.Tables[0].Columns["user_id"].ToString();
developers_DropDownList.DataSource = ds.Tables[0];
developers_DropDownList.DataBind();
developers_DropDownList.Items.Insert(0, new ListItem("Select Developer", "0"));
}
web.debug.config 和 web.config:
<configuration>
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2">
<assemblies>
<add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
<httpRuntime targetFramework="4.5.2"/>
<customErrors mode="Off"/>
</system.web>
</configuration>
编辑1:
@山本哲也
1- 首先,我在 web.config 中添加 <appSettings>。
2- 其次,我创建 file->new->file->global application class "global.asax" 并将您的代码放在 Application_Start 中。
3- 然后我输入 .aspx
<form .....>
<asp:ScriptManager runat="server" EnableScriptGlobalization="True" EnableCdn="True">
<Scripts>
<asp:ScriptReference Name="jquery" />
<asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" />
</Scripts>
</asp:ScriptManager>
<asp:DropDownList ID="developers_DropDownList" name="developers_DropDownList" class="form-control col-md-7 col-xs-12" runat="server"></asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredValidator1" runat="server"
ErrorMessage="Please select a value!"
ControlToValidate="developers_DropDownList"
InitialValue="Select Developer"></asp:RequiredFieldValidator>
我收到了这个错误:
'jquery' 不是有效的脚本名称。名称必须以“.js”结尾。
在 global.asax 中,我得到了:
ScriptResourceDefenition 不包含对 加载成功表达式
编辑2:
在执行所有@Tetsuya Yamamoto 注释后,我没有收到任何错误,但是当我离开下拉列表时仍然没有出现验证错误(选择开发人员)!!!!!!
【问题讨论】: