如果文本框为空,则从回发停止页面 [英] stop page from postback if text box is empty

查看:120
本文介绍了如果文本框为空,则从回发停止页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net页面中使用了createuserwizard控件。实际上页面是回发,即使文本框在createuserwizard中是空的,当我按下创建用户按钮。



我不希望页面回发如果有任何文本框在createuserwizard中是空的。



你能告诉我怎么做吗?我用Google搜索但没有找到..

i have taken createuserwizard control in my asp.net page. Actually the page is postback even the textboxes are empty in createuserwizard, when i press "create user" button.

I don't want the page to postback if any textbox is empty in createuserwizard..

Can you please tell me how to do this?? I googled but nothing found..

推荐答案

如果您从客户端点击功能返回false,则不会发回回。



If you return false from your client click function then postback will not happen.

<asp:button text="Create User" ID="btnUser" runat="server" onclick="btnUser_Click"" onclientclick="validateForm()" />







function validateForm() {
            var x = document.getElementById("txtUserName").value;

            document.getElementById("txtUserName").value="hello";

            if (x == null || x == "") {
             
                  //Code for error message what do you want.
                return false;
            }
        }


添加RequiredFieldValidator

Add RequiredFieldValidator
<asp:TextBox runat="server" id="txtName" />
  <asp:RequiredFieldValidator runat="server" id="reqName" controltovalidate="txtName" errormessage="Please enter your name!" />





参考:

http://asp.net-tutorials.com/validation/required-field-validator/ [ ^ ]

http://msdn.microsoft.com/en-us/library/sy stem.web.ui.webcontrols.requiredfieldvalidator(v = vs.110).aspx [ ^ ]



更新1:

添加显示=动态并尝试删除验证组(如果有)。如果您添加了验证组,那么您需要为回发控制设置相同的验证组。



reference:
http://asp.net-tutorials.com/validation/required-field-validator/[^]
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.requiredfieldvalidator(v=vs.110).aspx[^]

UPDATE 1:
add Display="Dynamic" and try by removing Validation Group if you have any. if you added validation group then you need to have the same validation group for the post back control.

<asp:RequiredFieldValidator ID="reqName" ControlToValidate="txtName" Display="Dynamic" ErrorMessage="Please enter your name!" runat="server"></asp:RequiredFieldValidator>



更新2:

我必须回到你以前的答案并找到你的代码。问题是您没有为文本框提供ValidationGroup。如果您使用ValidationGroup,则需要为需要验证的所有控件和验证器设置它。进行更改并测试您的应用程序,我在下面的一个文本框中添加了示例代码。


UPDATE 2:
I have to go back to your previous answer and find your code. The issue is you haven't give the ValidationGroup for the text boxes. if you using ValidationGroup you need to set it for the all the controls and validators you need to validate. do the changes and test your application, I have added sample code for one text box in below.

<asp:TextBox ID="UserName" runat="server" ValidationGroup="CreateUserWizard2"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate= "UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="CreateUserWizard2" Display="Dynamic" >*</asp:RequiredFieldValidator>


这篇关于如果文本框为空,则从回发停止页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆