【问题标题】:Display range when user clicks or enter textbox and validate in asp.net用户单击或输入文本框时显示范围并在 asp.net 中验证
【发布时间】:2013-02-17 02:48:41
【问题描述】:

如果用户在 asp.net 中输入非范围值,如何在用户选择文本框或单击文本框时显示带有文本框范围的弹出窗口并验证它?目前我正在使用 js 和 css 使用 z-index 显示面板,但没有验证。有没有更好的办法?

我尝试了 Ajax Validator 控件,但似乎它们仅在用户离开文本框时才会弹出。而且,文本框是动态创建的。

【问题讨论】:

    标签: asp.net ajax web-applications


    【解决方案1】:

    如果文本框的范围是固定的,则只能使用 javascript。如果您需要验证动态内容,则必须使用 ajax 调用 Web 方法或 Web 服务。 标记将是这样的

    <asp:ScriptManager ID="ScriptManager1" runat="server" 
      EnablePageMethods="true" />
    <script language="javascript">
     function UpdateTime() {
       PageMethods.GetCurrentDate(OnSucceeded, OnFailed); 
     }
    
     function OnSucceeded(result, userContext, methodName) {
       $get('Label1').innerHTML = result; 
     }
    
     function OnFailed(error, userContext, methodName) {
       $get('Label1').innerHTML = "An error occured.";
     }
    </script>
    <asp:Label runat="server" ID="Label1" Text="Update Me!" /><br />
    <input type="button" id="Button2" value="Web Method Update" 
      onclick="UpdateTime();" />
    

    您必须启用脚本管理器的页面方法,并将页面中编写的方法调用为PageMethods.YourMethodName(JavascriptMethodIfCallingSuccess,JavascriptMethodOnError); 您可以在 c# 中编写页面返回代码,如下所示

    [WebMethod]
    public static string GetCurrentDate()
    {
      return DateTime.Now.ToLongDateString();
    }
    

    现在您将在您的 javascript 中从服务器获取返回值。如果您的验证始终是固定的并且不需要服务器端验证,请不要使用此方法。这可能会给您的页面和服务器带来不必要的负载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      • 1970-01-01
      • 2011-12-19
      相关资源
      最近更新 更多