【问题标题】:Show Message Box if TextBox is Blank or Empty using C#如果 TextBox 为空或为空,则使用 C# 显示消息框
【发布时间】:2014-01-08 17:58:05
【问题描述】:

我首先尝试检查用户是否没有输入用户 ID 或密码,如果任何一个文本框为空白或为空,那么我想显示消息框,如果他们同时输入用户 ID 和密码,那么我想执行一些其他代码。我遇到的代码问题是消息根本没有显示,但好处是它不执行其他代码。因此,如果两个文本框都为空,我希望看到消息框出现。我不确定为什么消息框没有显示,即使我在其他函数中使用了相同的代码并且它有效。谢谢 这是我的 ASPX 按钮代码

 <asp:Panel ID="Panel1" runat="server" BorderStyle="Groove" Height="109px" Visible="false"
            Width="870px" BackColor="#FFFFE1">
            <asp:Label ID="Label2" runat="server" Text="DIGITAL SIGNATURE" Font-Bold="True" 
                ForeColor="#FF3300"></asp:Label>
            <br />
            <br />

            <asp:Label ID="lblUID" runat="server" Text="User ID:"></asp:Label>
            <asp:TextBox ID="txtUID" runat="server" Height="22px" Width="145px"></asp:TextBox> &nbsp;&nbsp;
            &nbsp;&nbsp;

            <asp:Label ID="lblPass" runat="server" Text="Password:"></asp:Label>
            <asp:TextBox ID="txtPass" runat="server" Height="23px" TextMode="Password" style="margin-top: 0px"></asp:TextBox>
            <br />
            <br />
            &nbsp;<asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="183px"
                onclick="btnSubmit_Click" 
                OnClientClick="return confirm('Are you sure you want to submit?');" 
                Font-Bold="False" Font-Size="Medium" Height="30px" 
                style="margin-right: 1px" />
            <br />
        </asp:Panel>

后面的代码:

protected void btnSubmit_Click(object sender, EventArgs e)
    {

        if (txtUID.Text.Length == 0 || txtPass.Text.Length == 0)
        {

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please type your User ID and Password correctly and click Submit button again.  Thanks", true);

        }

        else
        {

              //execute some code here
        }
     }

【问题讨论】:

  • 您是否尝试通过在 if 语句中设置断点来查看脚本是否实际运行?

标签: c# asp.net


【解决方案1】:

在 btnSubmit_Click 方法的代码中,当您调用 RegisterClientScriptBlock 时,您在警报结束时错过了 '); 符号。因此,您的 javascript 不正确,并且浏览器在开发人员工具中显示错误。应该是这样的:

protected void btnSubmit_Click(object sender, EventArgs e)
{

    if (txtUID.Text.Length == 0 || txtPass.Text.Length == 0)
    {

        ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
            "alertMessage", 
            "alert('Please type your User ID and Password correctly and click Submit button again.  Thanks');", true);
    }
    else
    {
          //execute some code here
    }
 }

【讨论】:

    【解决方案2】:
     protected void btnSubmit_Click(object sender, EventArgs e)
    {
    
        if (txtUID.Text.Length == 0 || txtPass.Text.Length == 0)
        {
    
             Page.ClientScript.RegisterStartupScript(this.GetType(), "alertMessage", "alert('Please type your User ID and Password correctly and click Submit button again.  Thanks');", true);
    
        }
    
        else
        {
    
            //execute some code here
        }
    }
    

    【讨论】:

      【解决方案3】:

      一种方法是在客户端执行此操作。您可以将您的 OnClientClick 更改为:

      return login();
      

      然后构建login函数:

      function login() {
          if ($('#txtUID').val() === '' ||
              $('#txtPass').val() === '') {
              alert('Please type your User ID and Password correctly and click Submit button again.  Thanks');
              return false;
          }
      
          return confirm('Are you sure you want to submit?');
      }
      

      所以现在您可以摆脱服务器端代码了。

      【讨论】:

        【解决方案4】:

        我创建了包含所有客户端验证的通用 .js 文件

        function checkMandatory(ele)
         {
             var text;
             text = ele.value;
             if(text.length == 0)
             {
                 alert("Please enter value ...");
                 ele.focus();
                 return;
             }
         }
        

        Page_Load 事件中的调用函数为 txtName.Attributes.Add("onFocusout", "checkMandatory(this);");

        注意:不要忘记将 .js 文件包含在 HTML 标记内的页面中。

        【讨论】:

          猜你喜欢
          • 2017-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-10
          • 1970-01-01
          • 2015-12-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多