在页面上按钮按下的时候,需要检验几个输入框的内容是否为空,如果为空,就
弹出对话框提示,页面不跳转,如何实现呢?

解决方案 »

  1.   

    .net 自带有这个控件,叫 RequiredFieldValidator 吧
    帮你实现了这个功能,实际上就是帮你实现 java script ,你打开 .net 2003 看看,服务器工具
    2003 就开始有了
      

  2.   

    <script>
    function checkNum()
    {
         var value1 = document.getElementById("txtNum1").value;
         var value2 = document.getElementById("txtNum2").value;
         var value3 = document.getElementById("txtNum3").value;
         var value4 = document.getElementById("txtNum4").value;
         if(value1 != "" && value2 != "" && value3 != "" && value4 != "")
         {
              window.Open("页面2.aspx");
         }
         else
         {
              alert("请将数据输入完整!");
         }
    }
    </script>
    <body>
        <input type = "text" id = "txtNum1" /><br>
        <input type = "text" id = "txtNum2" /><br>
        <input type = "text" id = "txtNum3" /><br>
        <input type = "text" id = "txtNum4" /><br>
        <input type = "button" onclick = "checkNum()" />
    </body>
    以上是JS写法,一下是C#写法
    public void checkNum()
    {
        string num1 = txtNum1.Text.Trim();
        string num2 = txtNum2.Text.Trim();
        string num3 = txtNum3.Text.Trim();
        string num4 = txtNum4.Text.Trim();
        if(num1.Equals("") || num2.Equals("") || num3.Equals("")) || num4.Equals(""))
        {
             Response.Write("<script>alert('请将数据输入完整!');</script>");
        }
        else
        {
             Response.Redirect("页面2.aspx");
        }
    }
      

  3.   

    page_load 中btn.Attributes.Add("onclick","return checkform();");js:
    function checkform(){
    if(为空) return false;
    return true;
    }