如何在asp.net(UnobtrusiveValidationMode)错误

时间:2017-04-06 07:04:42

标签: c# jquery asp.net

我有一个从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>

EDIT1:

@Tetsuya Yamamoto

1-首先,我在web.config中添加<appSettings>

2-秒,我创建了file-&gt; new-&gt; file-&gt;全局应用程序类“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不包含defenition   LoadSuccessExpression

EDIT2:

实施所有@Tetsuya Yamamoto笔记之后,我没有收到任何错误,但是当我离开下拉列表时仍然没有出现验证错误(选择开发人员)!!!!!

1 个答案:

答案 0 :(得分:2)

首先,确保这些行已在web.config中提供(不仅仅是web.debug.config):

<appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>

然后,尝试在ScriptManager.ScriptResourceMapping.AddDefinition内的Application_Start上添加Global.asax方法:

protected void Application_Start(object sender, EventArgs e)
{
    string jqversion = "1.12.4"; // match this to available jQuery version you have
    ScriptManager.ScriptResourceMapping.AddDefinition("jquery", new ScriptResourceDefinition
    {
        Path = "~/Scripts/jquery-" + jqversion + ".min.js",
        DebugPath = "~/Scripts/jquery-" + jqversion + ".js",
        CdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-" + jqversion + ".min.js",
        CdnDebugPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-" + jqversion + ".js",
        CdnSupportsSecureConnection = true,
        LoadSuccessExpression = "window.jQuery"
    });
}

下一个重要的事情是,在WebUIValidation.js元素中引用jQuery之后放置asp:ScriptReference

<asp:ScriptManager runat="server" EnableScriptGlobalization="True" EnableCdn="True">
    <Scripts>
        <asp:ScriptReference Name="jquery" />
        <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" />
    </Scripts>
</asp:ScriptManager>

要解决有关无效名称的第二个问题,请使用Manage NuGet Packages菜单同时安装AspNet.ScriptManager.jQuery&amp; AspNet.ScriptManager.jQuery.UI.Combined个软件包,或在软件包管理器控制台中输入以下行(请记得事后将Copy Local设置为True):

Install-Package AspNet.ScriptManager.jQuery

Install-Package AspNet.ScriptManager.jQuery.UI.Combined

MSDN documentationValidationSettings:UnobtrusiveValidationMode设置备注:

  

如果此键值设置为&#34;无&#34; [默认],ASP.NET应用程序   将使用4.5之前的行为(页面中的JavaScript内联)   客户端验证逻辑。如果此键值设置为&#34; WebForms&#34;,   ASP.NET使用HTML5数据属性和后期绑定的JavaScript   为客户端验证逻辑添加了脚本引用。

更新注意:

只有在项目UnobtrusiveValidationMode设置为.NET 4.5或更高版本(MSDN documentation)时,才会遇到有关LoadSuccessExpressiontargetFramework方法用法的错误,当targetFramework设置为早期版本(4.0)时,它们可能不存在。如果要在4.0版模式下运行项目,请尝试跳过上述所有步骤,但安装包并使用此配置:

<system.web>
    <compilation debug="false" targetFramework="4.0" />
    <httpRuntime targetFramework="4.0" />
</system.web>

参考:

ASP.NET 4.5 ScriptManager Improvements in WebForms (MSDN Blog)

类似问题:

ASP.Net 2012 Unobtrusive Validation with jQuery

WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive)

'jquery' is not a valid script name. The name must end in '.js'