$(selector).text()等效于c#(修订版)

时间:2011-02-15 11:40:30

标签: c# jquery asp.net sharepoint sharepoint-2007

我正在尝试检查元素的内部html是否为空但我想在服务器端进行验证,我将html视为字符串。这是我的代码

public string HasContent(string htmlString){
    // this is the expected value of the htmlString

    //  <span class="spanArea">
    //      <STYLE>.ExternalClass234B6D3CB6ED46EEB13945B1427AA47{;}</STYLE>
    //  </span>

    // From this jquery code-------------->
    // if($('.spanArea').text().length>0){
    //  
    // }
    // <------------------
    // I wanted to convert the jquery statement above into c# code. 

    /// c# code goes here
    return htmlSTring;
}

使用此行

$('.spanArea').text() // what is the equivalent of this line in c#

我会知道.spanArea是否确实在ui中显示了某些内容。我想在服务器端进行检查。无需担心如何设法访问我已经关注过的DOM。将htmlString视为Html字符串。

我的问题是,如果在C#中有这个jquery行的等价物?

提前致谢! :)

3 个答案:

答案 0 :(得分:2)

使用runat =“server”标记SpanArea,然后您可以在后面的代码中访问它:

<span id="mySpan" class="spanArea" runat="server />

然后你可以:

string spanContent = mySpan.InnerText;

答案 1 :(得分:2)

如果您确实需要从ServerSide中的HTML获取该数据,那么我建议您使用 Html-Parser 来完成该任务。

如果您检查 other SO posts ,您会发现多次推荐 Html Agility Pack

答案 2 :(得分:0)

在执行AJAX调用之前,包含此AJAX调用的页面的代码隐藏已经执行(在向浏览器显示页面时),因此您的问题看起来不正确。

提供您指示的HTML片段的代码隐藏可能是使用StringBuilder或类似代码构建的,因此您应该能够在该代码中验证是否有任何数据。

您提供的片段仅包含DIVSPANSTYLE标记。这很可能会折叠为零宽度元素并且不显示任何内容。

看看这篇文章,它将帮助您理解ASP.NET page life cycle