简体   繁体   English

必需的字段验证程序问题验证TextBox值

[英]Required Field Validator issue validating TextBox value

I have a TextBox which i use to store a url to applied a <asp:HyperLink> control. 我有一个TextBox ,用于存储URL以应用<asp:HyperLink>控件。 What i want to do is fire off the RequiredFieldValidator when the TextBox.Text value is empty and the user clicks save . 我想要做的是当TextBox.Text值为空并且用户单击“ 保存”时触发RequiredFieldValidator As far as i can tell, my logic is OK, but the validator isn't firing off? 据我所知,我的逻辑是正常的,但验证器没有解雇?

Here's the markup: 这是标记:

<div class="frmRow">
        <div class="frmControls">  
            <asp:Label ID="lblLink" AssociatedControlID="txtImgUrl" runat="server" Text="Image URL"></asp:Label>
            <asp:RequiredFieldValidator ID="imgUrlValidator" runat="server" ControlToValidate="txtImgUrl" ErrorMessage="Enter a Valid URL"  />
            <asp:TextBox ID="txtImgUrl" runat="server" />  
        </div>
        <div class="clearBoth"></div>
    </div>

Here is the code to check a valid absolute URL which is inside my btnSave event: 以下是检查我的btnSave事件中有效绝对URL的代码:

Uri url;
if (!string.IsNullOrEmpty(txtImgUrl.Text))
{
    txtImgUrl.Text = Uri.TryCreate(txtImgUrl.Text, UriKind.Absolute, out url) ? url.AbsoluteUri : string.Empty;
}

Save button markup: 保存按钮标记:

<br class="clearBoth" />
<asp:Button ID="btnSave" Text="Save Case study" ImageUrl="~/Assets/Design/buttons/btn-update.gif" CssClass="btn fltr" runat="server" OnClick="btnSave_OnClick" />
<div class="clearBoth"></div>

Shouldn't the RequiredFieldValidator be fired off when TryCreate fails on a dodgy URL and txtImgUrl.Text = "" ? TryCreate在一个狡猾的URL上失败并且txtImgUrl.Text = ""时,不应该触发RequiredFieldValidator吗?

Is there something blatantly obvious that I'm missing here? 有什么明显的东西让我在这里失踪吗?

Any help is much appreciated 任何帮助深表感谢

You should check on button click 你应该点击按钮点击

 if (Page.IsValid) 
         {
            lblOutput.Text = "Required field is filled!";
         }
         else 
         {
            lblOutput.Text = "Required field is empty!";
         }

In ASPX in the button add 在ASPX中添加按钮

CausesValidation="true"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM