如果TextBox为空或使用C#为空,则显示消息框

时间:2014-01-08 17:58:05

标签: c# asp.net

我试图首先检查用户是否输入用户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
        }
     }

4 个答案:

答案 0 :(得分:3)

在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
    }
 }

答案 1 :(得分:0)

 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
    }
}

答案 2 :(得分:0)

一种方法是做客户端。您可以将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?');
}

现在你可以摆脱服务器端代码。

答案 3 :(得分:0)

我创建了常见的.js文件,其中包含所有客户端验证

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

在Page_Load事件中调用函数as txtName.Attributes.Add(&#34; onFocusout&#34;,&#34; checkMandatory(this);&#34;);

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