我想进行服务器端验证,以确保每个具有“必需”类的文本框不为空。我可以在jQuery中做到这一点没有问题但是没有用C#的专家。这就是我得到的:
protected void BtnCatering_Click(object sender, EventArgs e)
{
if (TextBox.CssClass == "required" && TextBox.Text == "")
{
MessageBox.Show("Please fill in required fields");
}
}
<p>
<label for="BookingName">Booking Name <span>*</span></label>
<asp:TextBox class="inputText required" ID="BookingName" runat="server" />
</p>
<p>
<label for="ArrivaltDate">Arrival Date<span>*</span></label>
<asp:TextBox CssClass="myDatepicker inputText required" id="ArrivaltDate" runat="server" />
</p
答案 0 :(得分:1)
出于这个原因,ASP.NET有Validation Controls
可用于验证服务器端的数据(以及可选的客户端数据)。
您可以对每个文本框或RequiredFieldValidator
控件使用CustomValidator
控件。
请参阅MSDN: Types of Validation for ASP.NET Server Controls。
例如,对于标识为TextBox
的{{1}}控件,您可以使用以下内容。
BookingName
如果您使用的是<asp:RequiredFieldValidator EnableClientScript="False" ID="RequiredFieldValidator1"
ControlToValidate="BookingName" runat="server"
ErrorMessage="Please fill in this field"></asp:RequiredFieldValidator>
,则这些控件位于您的工具箱中。
答案 1 :(得分:1)
这样的事情可能是你的解决方案:
var results = this.Controls.OfType<TextBox>().Where(c =>
Convert.ToString(c.Attributes["class"]).Contains("required"));
然后你可以遍历那个结果集。也许是这样的:
foreach (var tb in results)
{
if (string.IsNullOrEmpty(tb.Text))
{
// do something here because this TextBox is empty
}
}
但是,更正确的方法是对每个控件使用RequiredFieldValidator
,这样您就可以在准备好时调用this.Validate()
上的Page
,然后检查this.IsValid
上的Page
并对其做出反应。
答案 2 :(得分:1)
尝试这种方式。
protected void BtnCatering_Click(object sender, EventArgs e)
{
if (BookingName.Attributes["class"].ToString().Contains("required") && BookingName.Text == "")
{
MessageBox.Show("Please fill in required fields");
}
}
// BookingName
是您的文本框ID。
答案 3 :(得分:0)
代码简单如下:
if (CssUtils.HasClass(TextBox.CssClass, "required") && TextBox.Text == "") {
..do stuff..
}
但是,这当然假设您有一个有效的CssUtils
类,可以准确地检测出类的存在。
public static class CssUtils
{
/// <summary>
/// http://dev.w3.org/html5/spec/common-microsyntaxes.html#space-character
/// </summary>
private static readonly char[] WhitespaceCharacters = new[] { ' ', '\t', '\n', '\f', '\r' };
/// <summary>
/// ToggleAggregator toggles the <paramref name="parsedClass"/> within the <paramref name="current"/> classes.
/// </summary>
/// <param name="current">
/// A <see cref="String"/> of whitespace separated classes.
/// </param>
/// <param name="parsedClass">
/// A class to toggle within the <paramref name="current"/> classes.
/// </param>
/// <returns>
/// The <see cref="String"/> of resultant classes.
/// </returns>
private static string ToggleAggregator(string current, string parsedClass)
{
return HasClass(current, parsedClass)
? RemoveClass(current, parsedClass)
: AddClass(current, parsedClass);
}
/// <summary>
/// AddClass adds the provided <paramref name="classes"/> to the <paramref name="source"/> classes.
/// </summary>
/// <param name="source">
/// A <see cref="String"/> of whitespace separated classes.
/// </param>
/// <param name="classes">
/// A <see cref="String"/> of whitespace separated classes to add to the <paramref name="source"/> classes.
/// </param>
/// <returns>
/// The <see cref="String"/> of merged classes.
/// </returns>
public static string AddClass(string source, string classes)
{
var sourceClasses = ParseClasses(source);
var parsedClasses = ParseClasses(classes);
var mergedClasses =
sourceClasses
.Union(parsedClasses)
.ToArray()
.Join(" ");
return mergedClasses;
}
/// <summary>
/// AddClass adds the provided <paramref name="classes"/> to the <paramref name="source"/> classes.
/// </summary>
/// <param name="source">
/// A <see cref="String"/> of whitespace separated classes.
/// </param>
/// <param name="classes">
/// An <see cref="Array.string"/> of classes to add to the <paramref name="source"/> classes.
/// </param>
/// <returns>
/// The <see cref="String"/> of merged classes.
/// </returns>
public static string AddClass(string source, params string[] classes)
{
return AddClass(source, classes.Join(" "));
}
/// <summary>
/// HasClass checks whether all of the provided <paramref name="classes"/> are contained in the <paramref name="source"/> classes.
/// </summary>
/// <param name="source">
/// A <see cref="String"/> of whitespace separated classes.
/// </param>
/// <param name="classes">
/// A <see cref="String"/> of whitespace separated classes to check in the <paramref name="source"/> classes.
/// </param>
/// <returns>
/// <c>True</c> if all of the <paramref name="classes"/> are contained in the <paramref name="source"/> classes, <c>False</c> otherwise.
/// </returns>
public static bool HasClass(string source, string classes)
{
if (source.IsEmpty() || classes.IsEmpty()) return false;
var sourceClasses = ParseClasses(source);
var parsedClasses = ParseClasses(classes);
return parsedClasses.Intersect(sourceClasses).Count() == parsedClasses.Count();
}
/// <summary>
/// RemoveClass removes the provided <paramref name="classes"/> from the <paramref name="source"/> classes.
/// </summary>
/// <param name="source">
/// A <see cref="String"/> of whitespace separated classes.
/// </param>
/// <param name="classes">
/// A <see cref="String"/> of whitespace separated classes to remove from the <paramref name="source"/> classes.
/// </param>
/// <returns>
/// The <see cref="String"/> of remaining classes.
/// </returns>
public static string RemoveClass(string source, string classes)
{
var sourceClasses = ParseClasses(source);
var parsedClasses = ParseClasses(classes);
var resultClasses =
sourceClasses
.Except(parsedClasses)
.ToArray()
.Join(" ");
return resultClasses;
}
/// <summary>
/// RemoveClass removes the provided <paramref name="classes"/> from the <paramref name="source"/> classes.
/// </summary>
/// <param name="source">
/// A <see cref="String"/> of whitespace separated classes.
/// </param>
/// <param name="classes">
/// An <see cref="Array.string"/> of classes to remove from the <paramref name="source"/> classes.
/// </param>
/// <returns>
/// The <see cref="String"/> of remaining classes.
/// </returns>
public static string RemoveClass(string source, params string[] classes)
{
return RemoveClass(source, classes.Join(" "));
}
/// <summary>
/// ToggleClass toggles the provided <paramref name="classes"/> within the <paramref name="source"/> classes.
/// </summary>
/// <param name="source">
/// A <see cref="String"/> of whitespace separated classes.
/// </param>
/// <param name="classes">
/// A <see cref="String"/> of whitespace separated classes to toggle within the <paramref name="source"/> classes.
/// </param>
/// <returns>
/// The <see cref="String"/> of resultant classes.
/// </returns>
public static string ToggleClass(string source, string classes)
{
var parsedClasses = ParseClasses(classes);
return parsedClasses.Aggregate(source ?? "", ToggleAggregator);
}
/// <summary>
/// ToggleClass toggles the provided <paramref name="classes"/> within the <paramref name="source"/> classes.
/// </summary>
/// <param name="source">
/// A <see cref="String"/> of whitespace separated classes.
/// </param>
/// <param name="classes">
/// An <see cref="Array.string"/> of classes to toggle within the <paramref name="source"/> classes.
/// </param>
/// <returns>
/// The <see cref="String"/> of resultant classes.
/// </returns>
public static string ToggleClass(string source, params string[] classes)
{
return ToggleClass(source, classes.Join(" "));
}
/// <summary>
/// ToggleClass toggles the provided <paramref name="classes"/> within the <paramref name="source"/> classes dependant on the state of the <paramref name="switch"/>.
/// </summary>
/// <param name="source">
/// A <see cref="String"/> of whitespace separated classes.
/// </param>
/// <param name="classes">
/// A <see cref="String"/> of whitespace separated classes to toggle within the <paramref name="source"/> classes.
/// </param>
/// <param name="switch">
/// When <c>True</c>, the <paramref name="classes"/> will be added to the <paramref name="source"/> classes.
/// When <c>False</c>, the <paramref name="classes"/> will be removed from the <paramref name="source"/> classes.
/// </param>
/// <returns>
/// The <see cref="String"/> of resultant classes.
/// </returns>
public static string ToggleClass(string source, string classes, bool @switch)
{
return @switch ? AddClass(source, classes) : RemoveClass(source, classes);
}
/// <summary>
/// ParseClasses splits the provided whitespace separated list of <paramref name="classes"/> into an array of distinct classes.
/// </summary>
/// <param name="classes">
/// A <see cref="String"/> of whitespace separated classes.
/// </param>
/// <returns>
/// A <see cref="Array.string"/> of distinct classes.
/// </returns>
public static string[] ParseClasses(string classes)
{
return (classes ?? "")
.Split(WhitespaceCharacters, StringSplitOptions.RemoveEmptyEntries)
.Distinct()
.ToArray();
}
}