ASP.net Adding link - c#

I'm making a Car dealership website, and in my webpage to do an advanced search i'd like to make it possible to display some details of the car then a link to the actual page of the car. Right now i have a
StringBuilder tableBuilder = new StringBuilder();
if(reader.HasRows)
While (reader.Read())
{
string col0 = reader["ID"].ToString();
string col1 = reader["Make"].ToString();
string col2 = "LINK..."
I'd like to replace "LINK..." with a redirect to a different page+reader["ID"].ToString();, and I can't seem to find any decent material that says how to incorporate it with this. The way that it would be nice would be so that for all the cars that match the criteria, there is a link to each details page.

The easiest way is to simply emit the href directly:
string col2 = "<a href='somepage.aspx?someparam=someval'>Link Text</a>

Related

HTML Agility Pack foreach loop not iterating for data grid (C#)

I'm a beginner programmer working on a small webscraper in C#. The purpose is to take a hospital's public website, grab the data for each doctor, their department, phone and diploma info, and display it in a Data Grid View. It's a public website, and as far as I'm concerned, the website's robots.txt allows this, so I left everything in the code as it is.
I am able to grab each data (name, department, phone, diploma) separately, and can successfully display them in a text box.
// THIS WORKS:
string text = "";
foreach (var nodes in full)
{
text += nodes.InnerText + "\r\n";
}
textBox1.Text = text;
However, when I try to pass the data on to the data grid view using a class, the foreach loop only goes through the first name and fills the data grid with that.
foreach (var nodes in full)
{
var Doctor = new Doctor
{
Col1 = full[0].InnerText,
Col2 = full[1].InnerText,
Col3 = full[2].InnerText,
Col4 = full[3].InnerText,
};
Doctors.Add(Doctor);
}
I spent a good few hours looking for solutions but none of what I've found have been working, and I'm at the point where I can't decide if I messed up the foreach loop somehow, or if I'm not doing something according to HTML Agility Pack's rules. It lets me iterate through for the textbox, but not the foreach. Changing full[0] to nodes[0] or nodes.InnerText doesn't seem to solve it either.
link to public gist file (where you can see my whole code)
screenshot
Thank you for the help in advance!
The problem is how you're selecting the nodes from the page. full contains all individual names, departments etc. in a flat list, which means full[0] is the name of the first doctor while full[4] is the name of the next. Your for-loop doesn't take that into account, as you (for every node) always access full[0] to full[3] - so, only the properties of the first doctor.
To make your code more readable I'd split it up a bit to first make a list of all the card-elements for each doctor and then select the individual parts within the loop:
HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc = web.Load("https://klinikaikozpont.unideb.hu/doctor_finder");
const string doctorListItem = "div[contains(#class, 'doctor-list-item-model')]";
const string cardContent = "div[contains(#class, 'card-content')]";
var doctorCards = doc.DocumentNode.SelectNodes($"//{doctorListItem}/{cardContent}");
var doctors = new List<Doctor>();
foreach (var card in doctorCards)
{
var name = card.SelectSingleNode("./h3")?.InnerText;
const string departmentNode = "div[contains(#class, 'department-name')]";
var department = card.SelectSingleNode($"./{departmentNode}/p")?.InnerText;
// other proprties...
doctors.Add(new Doctor{NameAndTitle = name, Department = department});
}
// I took the liberty to make this class easier to understand
public class Doctor
{
public string NameAndTitle { get; set; }
public string Department { get; set; }
// Add other properties
}
Check out the code in action.

How to get the query string from the URL to my scraper

i'm currently building a scraper that gets data from an airlines website.
https://www.norwegian.com/uk/booking/flight-tickets/farecalendar/?D_City=OSL&A_City=RIX&TripType=1&D_Day=17&D_Month=201910&dFare=57&IncludeTransit=false&CurrencyCode=GBP&mode=ab#/?origin=OSL&destination=RIX&outbound=2019-10&adults=1&direct=true&oneWay=true&currency=GBP
My objective is to get a link from each of these calendar days (from 1 to 31)
I am using a HTTP Analyser and if I pass a query it returns this in the Query String window :
/pixel;r:1875159210;labels=_fp.event.Default;rf=0;a=p-Sne09sHM2G2M2;url=https://www.norwegian.com/uk/ipc/availability/avaday?AdultCount=1&A_City=RIX&D_City=OSL&D_Month=201910&D_Day=17&IncludeTransit=false&TripType=1&CurrencyCode=GBP&dFare=57&mode=ab;ref=https://www.norwegian.com/uk/booking/flight-tickets/farecalendar/?D_City=OSL&A_City=RIX&TripType=1&D_SelectedDay=01&D_Day=01&D_Month=201910&IncludeTransit=false&CurrencyCode=GBP&mode=ab;fpan=0;fpa=P0-2049656399-1568351608065;ns=0;ce=1;qjs=1;qv=4c19192-20180628134937;cm=;je=0;sr=1920x1080x24;enc=n;dst=1;et=1568366731754;tzo=-60;ogl=
How do I pass each of these queries to a scraper?
EDIT: I should've probably said that I need the program to loop through each flight and change the day (in this case from 1 to 31) in the URL.
My scraper is pretty basic, it can do basic websites that have links and it can show things like Titles, Articles, etc..
I should probably add that my aim is to display the destination, prices, time for travel, etc... which are something that I would know how to do.
Hope you can understand this. Thanks!
This is what I currently have and I will modify it to suit my needs.
public void ScrapeData(string page)
{
var web = new HtmlWeb();
var doc = web.Load(page);
var Articles = doc.DocumentNode.SelectNodes("//*[#class = 'article-single']");
foreach (var article in Articles)
{
var header = HttpUtility.HtmlDecode(article.SelectSingleNode(".//li[#class = 'article-header']").InnerText);
var description = HttpUtility.HtmlDecode(article.SelectSingleNode(".//li[#class = 'article-copy']").InnerText);
Debug.Print($"Title: {header} \n + Description: {description}");
_entries.Add(new EntryModel { Title = header, Description = description });
}
}
That URL returns a calendar comprised of buttons with the fare info and day on them, so you'll have to parse the returned HTML to find the individual day and then the fare from that cell.
So it seems easy to hit the URL, then loop through each table cell in the calendar section for the sub-divs in the DOM that contain the relevant day and fare info. Fortunately they have an aria-label for both these items so they are easy to locate.

address an object by retrieving the ID name of the object from a variable

Good evening,
I am trying to get the following done. I have seen a similar post but it was related with Unity.
Anyway, I am on web forms in asp.net and I have a radiobuttonList with ID="id001"
so on my code behind, I would normally be able to get the selected value by just doing:
string value = id001.SelectedValue
However, in this situation, I don't know the exact ID name, so I have a function that retrieves it. So now I have the variable with the name of the ID. So I want to be able to now, convert the value of that variable in something like this:
string foundid = "id001"
string foundidvalue = id001.SelectedValue
I hope this makes sense.
Thanks in advance for the help.
I am assuming this one is related to your previous question. So, when you found the control, instead of using function to get the fullname, you can do like this:
foreach (Control c in Page.Form.Controls.OfType<RadioButtonList>())
{
if (c.ID.Contains("id"))
{
// string FullID = c.ID.ToString();
var radioButtonList = c as RadioButtonList;
var selectedValue = radioButtonList.SelectedValue;
}
}
You want to use FindControl.
string foundid = "id001";
var foundCtrl = (RadiobuttonList)FindControl(foundid);
var result = foundCtrl.SelectedValue;

Modify List<string> to convert contents to hyperlinks

I have a List<string> that get's populated with URLs. What I'd like to do is convert the contents of the List to hyperlinks that the user can click on. I've seen a bunch of examples of how to do this, but most of them were to insert in to an email, or switch the word here to a hyperlink. I just don't know what I'm looking at, so it's a little confusing. Here's what I have:
List<string> lstUrls = new List<string>();
//PROGRAM GETS URLS FROM ELEMENTS IN HERE....
foreach (string s in lstUrls)
{
s = ""; //THIS DOESN'T WORK...
}
I don't want to change the content of the string - just to be able to display as a hyperlink. For example, one string value will be https://www.tyco-fire.com/TD_TFP/TFP/TFP172_02_2014.pdf; and how Stack Overflow displays it as a link, that's what I would like to accomplish.
I know I'm obviously botching the syntax. Any help is appreciated.
You canĀ“t change the content of a List<T> while iterating it using foreach. But you can using for:
for(int i = 0; i < lstUrls.Count; i++)
{
var s = lstUrls[i];
lstUrls[i] = "" + s + "";
}
A bit easier to read was this:
lstUrls[i] = String.Format("{0}", s);
You could use linq for it:
lstUrls = lstUrls.Select(s => $"").ToList();
Or rather displaying the url in it:
lstUrls = lstUrls.Select(s => $"{s}").ToList();

editing href in Orchard.ContentManagement.ContentItem

I am trying to integrate disqus comment counts in bloq summary.
#{
Orchard.ContentManagement.ContentItem contentItem = Model.ContentPart.ContentItem;
string bodyHtml = Model.Html.ToString();
var body = new HtmlString(Html.Excerpt(bodyHtml, 8000).ToString().Replace(Environment.NewLine, "</p>" + Environment.NewLine + "<p>"));
}
<p>#body #Html.ItemDisplayLink(T("more").ToString(), contentItem)</p>
so i need to concatinate #disqus_thread to the href of the contentItem link.
I cant use any plugin for implementing disqus. How can i edit the href?
If you are looking to get the display url of a content item, us the Url helper, e.g.:
#T("more")
Now you have full control over the href, allowing you to append whatever querystring parameter you need.

Categories

Resources