I want to check xml before displaying it .I am using XPath not xsl for it. For e.g.
<title></title>
<url></url>
<submit></submit>
i wanna check that if xml data is not there for it . Then don't display it. because I m putting these values in <a href=<%#container.dataitem,url%>>new link</a>.
So i want that if url is empty then don't display new link otherwise display it and similarly for title that if title is not empty display it otherwise don't display it.
Main problem is I can check like in ascx.cs file
if(iterator.current.value="") don't display it but the problem is in ascx file i m givin
new link
i want that new link should not come if url is empty...
Any idea how to check this condition?
I've seen this handled using an asp:Literal control.
In the web form, you'd have <asp:Literal id='literal' runat='server' text='<%# GetAnchorTag(container.dataitem) %>' />
And in the code behind, you'd have:
protected string GetAnchorTag(object dataItem) {
if(dataItem != null) {
string url = Convert.ToString(DataBinder.Eval(dataItem, "url"));
if(!string.IsNullOrEmpty(url)) {
string anchor = /* build your anchor tag */
return anchor;
}
}
return string.Empty;
}
this way, you either output a full anchor tag or an empty string. I don't know how this would fit in with your title and submit nodes, but it solves the anchor display issue.
Personally, I don't like this approach, but I've seen it quite a bit.
Use XPath. Assuming that the elements are enclosed in an element named link:
link[title != '' and url !='']
will find you the link elements whose title and url child elements contain no descendant text nodes. To make it a little more bulletproof,
link[normalize-space(title) != '' and normalize-space(url) !='']
will keep the expression from matching link elements whose title or url children contain whitespace.
If you don't have access to the .cs file for this then you can still embed the code right in the .ascx file. Remember, you don't HAVE to put all your code in the code behind file, it can go inline right inside the .ascx file.
<%
if(iterator.current.value!="") {
%>
<a href=<%#container.dataitem,url%>>new link</a>
<%
}
%>
what about //a[not(./#href) or not(text()='']
Related
Below is the sample html source
<div id="page2" dir="ltr">
<p>This text I dont want to extract</p>
This is the text which I want to extract
</div>
Irrespective of the attributes of div tag, I want to extract only the div tag text ignoring the other tags text that come inside div tag.
In the above example i do not want to extract text within <p></p> tag, but i want to extract text within <div></div> tag, i.e "This is the text which I want to extract"
XmlNodeList DivNodeList = xDoc.GetElementsByTagName("div");
string DivInnerText;
for (int i = 0; i < DivNodeList.Count; i++)
{
if (!DivNodeList[i].InnerXml.Contains("p"))
{
DivInnerText = DivNodeList[i].InnerText.Trim();
Div_List.Add(DivInnerText);
}
}
But the above code is not working as expected, since I am checking whether p tag is present or not, then only extracting the text. Obviously if p tag is present, it would not go inside and more over the inner text of the div tag contains all the text combined whatever the tags inside it.
Any help on this is greatly appreciated.
For HTML processing, you should try the HtmlAgilityPack library.
Your requirement should be easy to do.
Take a look : http://www.c-sharpcorner.com/UploadFile/9b86d4/getting-started-with-html-agility-pack/
Using JQuery you can achieve this by doing that:
$("#page2").clone().children().remove().end().text();
Example
The credit should go to "DotNetWala" -
check his answer here
I'm trying to render an element's mark-up using asp controls while avoiding using code-behind. So I want to dynamically generate the href property to include what is rendered from a FieldValue control (SharePointWebControls).
So for example this control I have:
<SharePointWebControls:FieldValue id="PageTitle" FieldName="Title" runat="server"/>
Appears as:
"TestPage"
And I have a link on that same page looking like this:
CLICK HERE!
But above in the <a> element - I need TestPage to be there as a result of what's rendered by my FieldValue control; so I basically need a way of 'embedding' the output of this control within the <a> element's href property.
There's no messy bits of markup to accompany the rendered version of FieldValue - it's literally just text - so I'm assuming this isn't complicated.
Not familiar with SP controls, but I guess the compiler stops on the double quotes when you try to embed you control in the href.
Maybe you can try replacing you href's double quotes with single quotes like this :
<a href='http://www.mysite.com/mypage.aspx?title=<SharePointWebControls:FieldValue id="PageTitle" FieldName="Title" runat="server"/>'>CLICK HERE!</a>
( be aware that it will fail if there are quotes in your text )
another solution I see is using some js/jquery (almost the same solution, in fact) :
$('selectorForYourA').attr("href", 'http://www.mysite.com/mypage.aspx?title=<SharePointWebControls:FieldValue id="PageTitle" FieldName="Title" runat="server"/>');
I need some help figuring out how to do something.
I got this gallery (galleriffic) and some images that are store in Flicker.com so I used the flicker api to get the images but still add them manually to test the gallery.
Now I'm looking for a good way to insert the images into the html after I get them with the flicker api.
I found this htmltextwriter and used the function
Response.Write(GetDivElements());
but this is adding the div's on the top of the html and not inside the body tag.
my qustions is:
is HtmlTextWriter writer = new HtmlTextWriter(stringWriter) a good way to build html tags on the server side?
Is there a better way to add elements to the html other then Response.Write(""); ?
Here is what I do when I need to add mark-up.
in my page
<asp:PlaceHolder ID="MyPlaceholder" runat="server"></asp:PlaceHolder>
in my code behind
MyPlaceholder.Controls.Add(new Literal() { Text="<div>some markup</div>"});
I do it this way because:
1) you can put the PlaceHolder where you need it in the structure of your page
2) by adding a Literal at runtime to the Controls collection prevents ViewState getting bloated with it's contents.
If you are using the older style of asp.net, and not asp.net MVC, then you can just create a div with an id and runat="server". Then you can just write directly to the html.
aspx page
<div id = "DivINeedToAddStuffTo" runat="server" />
aspx.cs
DivINeedToAddStuffTo.InnerHtml = GetDivElements();
Also, I do not see anything wrong with using HtmlTextWriter to create your Html markup
You might try looking into Placeholders. That way you can create an instance of an image control and then add it your your placeholder.
Image myImg = new Image();
myImg.ImageUrl = "MyPicture.jpg";
myPlaceholder.Controls.Add(myImg);
You should be able to use the ASP literal control:
foreach (var item in items)
{
Literal literal = new Literal();
literal.text = item.html; //Assuming the item contains the html.
MyPlaceholder.Controls.Add(literal);
}
You could have that code before the page has rendered.
Hope that helps
Paul
EDIT
Sorry, I think I was mistaken, I thought you had the html with the link to the image(s) and not the actual image itself, Justin's answer would suit you if that's the case.
var ctrl = new WebControl(HtmlTextWriterTag.Div) { CssClass = "SomeClass" };
ctrl.Attributes["style"] = "float:left;display:inline-block;margin:3px;";
ctrl.Controls.Add(new Image
{
ImageUrl =
Page.ResolveUrl("image path here")
});
this.Controls.Add(ctrl);
I have made an application in javascript using HTML fields in asp.net, as asp text boxes were disturbing the ids, so i used html fields, which are working fine with javascript, now i want to fetch database table columns on page load, and want to assign to html fields, what is the best way to do so? Help me!!!!
You could go back to using the ASP TextBoxes and access the ids in JavaScript as follows:
<%= IDofTextBox.ClientID %>
It's probably the easiest as naturally they can then be accessed in the code behind very easily.
you can use asp text boxes fine if you grab a reference in your javascript to their asp.net generated ID via <%= textboxname.ClientId %>
This is not the right way to do it (I wouldn't recommending it), but if its what you need, then it will work.
Add method="post" action="<your path here>" to your form element and when the submit button posts, you will be able to access all the form variables like so:
string txtName = Request["TextBox1"] ?? string.Empty; //replace textbox 1 with text box name
Just be sure to replace the action in form to your page etc..
But really, going back to <asp:TextBox... will save you a lot more time and as Ian suggested, you can access them with javascript by the server tags <%= TextBox1.ClientId %>
ps: also, the ?? is a null coalesce character. its a short form of saying
if(Request["textbox1"] != null)
txtName = Request["textbox1"];
else
txtName = "";
If I understand you correctly. You just need to add runat="server" and id="someName" to the html fields and access them in the code behind by its given id.
I need to extract Text from webpages mostly related to business news.
say the HTML page is as follows..
<html>
<body>
<div>
<p> <span>Desired Content - 1</span></p>
<p> <span>Desired Content - 2</span></p>
<p> <span>Desired Content - 3</span></p>
</div>
</body>
</html>"
I have a sample stored in a string that can take me to Desired Content -1 directly, so i can collect that content. But i need to collect Desired Content -2 and 3.
For that what i tried is from the current location i.e from with in span node of desired Content -1 i used parentof and moved to the external node i.e Para node and got the content but actually i need to get the entire desired content in div. How to do it? You might ask me to go to div directly using parentof.parentof.span. But that would be specific to this example, i need a general idea.
Mostly news articles will have desired content in a division and i will go directly to some nested inner node of that division. I need to come out of those inner nodes only till i encounter a division and then get the innerText.
I am using XPath and HTMLagilitypack.
Xpath i am using is -
variable = doc.DocumentNode.SelectSingleNode("//*[contains(text(),'" + searchData + "')]").ParentNode.ParentNode.InnerText;
Here "searchData" is a variable that is holding a sample of Desired Content -1 used for searching the node having news in the entire body of the webpage.
What i am thinking is clean up the webpages and have only main tags like HTML, BODY, Tables, Division and Paragraphs but no spans and other formating elements. But some other website might use Spans only instead of divs so i am not sure how to implement this requirement.
Basic requirement is to extract the News content from different webpages(almost 250 different websites). So i can not code specific to each webpage..i need a generic method.
Any ideas appreciated. Thank you.
This XPath expression selects the innermost div element with $searchData variable reference value as part of its string value.
//div[contains(.,$searchData)]
[not(.//div[contains(.,$searchData)])]
Found out the answer myself...
Using a while loop till i find a div parent and then getting innertext is working.
`{ //Select the desired node, move up till you find a div and then get the inner text.
node = hd.DocumentNode.SelectSingleNode("//*[contains(text(),'" + searchData + "')]"); //Find the desired Node.
while (node.ParentNode.Name != "div") //Move up till you find a encapsulating Div node.
{
node = node.ParentNode;
Console.WriteLine(node.InnerText);
}
Body = node.InnerText;
}`