必填字段验证问题验证文本框的值 [英] Required Field Validator issue validating TextBox value
问题描述
我有一个文本框
,我用它来存储一个URL来施加< ASP:超链接>
控制。我想要做的就是关火时, TextBox.Text
值为空,并在用户点击的RequiredFieldValidator >保存的。据我所知,我的逻辑是确定的,但验证器发射了?
I have a TextBox
which i use to store a url to applied a <asp:HyperLink>
control. What i want to do is fire off the RequiredFieldValidator
when the TextBox.Text
value is empty and the user clicks save. As far as i can tell, my logic is OK, but the validator isn't firing off?
下面的标记:
<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>
下面是code,检查一个有效的绝对URL这是我的 btnSave
事件中:
Here is the code to check a valid absolute URL which is inside my btnSave
event:
Uri url;
if (!string.IsNullOrEmpty(txtImgUrl.Text))
{
txtImgUrl.Text = Uri.TryCreate(txtImgUrl.Text, UriKind.Absolute, out url) ? url.AbsoluteUri : string.Empty;
}
Save按钮标记:
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>
应该不是的RequiredFieldValidator
在 TryCreate
失败的一个狡猾的URL并发射了 txtImgUrl.Text =
?
Shouldn't the RequiredFieldValidator
be fired off when TryCreate
fails on a dodgy URL and txtImgUrl.Text = ""
?
有什么明显地告诉我在这里丢失?
Is there something blatantly obvious that I'm missing here?
任何帮助很多AP preciated
Any help is much appreciated
推荐答案
您应该检查按钮点击
if (Page.IsValid)
{
lblOutput.Text = "Required field is filled!";
}
else
{
lblOutput.Text = "Required field is empty!";
}
在ASPX中的按钮添加
In ASPX in the button add
CausesValidation="true"
这篇关于必填字段验证问题验证文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!