如何检查用户是否已填充页面中的所有元素

本文关键字:元素 填充 何检查 检查 是否 用户 | 更新日期: 2023-09-27 17:56:32

<textarea questn-id="123" />
<td questn-id="234"/>

我在字符串列表中有所有文本区域和 td 的任务 ID。如何检查文本区域和 td 是否在 c# 中填写。

我已经使用 htmlagility 包获得了所有元素的 quest-id 并将其存储在字符串列表中,现在我想查看所有字段是满还是空。有人可以帮助我吗?

如何检查用户是否已填充页面中的所有元素

正如他们所说,您必须将runat="server"添加到元素中您可以使用linq在服务器端搜索它们:

var controls = Page.Controls.OfType<WebControl>().Where(x => string.IsNullOrEmpty(x.Attributes["questn-id"]));

然后,您将拥有未填充任务ID的控件

如果你的标记是这样的:

<textarea questn-id="123" id="txtarea" runat="server" />
<td questn-id="234" id="tdId" runat="server" />

您可以这样做:

if(txtarea.Value == "")
{
  // it is empty
}
else{
 // it is not empty. It has some value.
}

如果要签入代码隐藏,请为元素文本区域添加runat="server"并通过 id 访问它

<textarea questn-id="123" id="txtarea" runat="server" />
if (string.IsNullOrEmpty(txtarea.Text))
{
   Console.WriteLine("textarea is empty");
}

前言:我通过转换器运行了一些 VB 代码,并根据适用情况进行了更改。 因此,这在语法上可能不正确,但您应该了解如何使用HtmlAgilityPack。

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(stringContainingYourHtml);
foreach (HtmlAgilityPack.HtmlNode txt in doc.DocumentNode.SelectNodes("//textarea")) {
    if ((txt.Attributes("questn-id") != null) && ! String.IsNullOrEmpty(txt.Attributes("questn-id").Value) {
        string txtVal = txt.Attributes("questn-id").Value.ToLower();
        // do whatever you want to do for validation here.
}

}