开发者

$(selector).text() equivalent in c# (Revised)

I am trying check if the inner html of the element is empty but I wanted to do the validation on the server side, I'm treating the html as a string. Here is my code

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){
    //  
    // }
    // <-----------开发者_Go百科-------
    // I wanted to convert the jquery statement above into c# code. 

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

using this line

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

I will know if the .spanArea does really have something to display in the ui or not. I wanted to do the checking on the server side. No need to worry about how to I managed to access the DOM I have already taken cared of it. Consider the htmlString as the Html string.

My question is if there is any equivalent for this jquery line in C#?

Thanks in advance! :)


If you really need to get that data from the HTML in the ServerSide then I would recommend you to use a Html-Parser for that job.

If you check other SO posts you will find that Html Agility Pack was recommended many times.


Tag the SpanArea with runat="server" and you can then access it in the code behind:

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

You can then:

string spanContent = mySpan.InnerText;


Your code-behind for the page that includes this AJAX call will have already have executed (in presenting the page to the browser) before the AJAX call is ever executed so your question doesn't appear correct.

The code-behind that is delivering the HTML fragment you indicated is probably constructing that using a StringBuilder or similar so you should be able to verify in that code whether there is any data.

The fragment you provided only includes a DIV, a SPAN and a STYLE tag. This is all likely to collapse to a zero width element and display nothing.

Have a look at this article which will help you understand the ASP.NET page life cycle.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜