how to extract hyperllinks from the gridview

mmalik15Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kiran SonawaneProject LeadCommented:
You can do it easily with jquery.
Like below


$(document).ready(function(){
  $(".searchresults a").each(function(){
  alert($(this).text());
  alert($(this).attr('href'));
});
});
0
Cenjoy100Commented:
You can  set allowpaging option to false.
0
mmalik15Author Commented:
thanks for the comments

I can't see any option to set allowpaging to false. Could you point exactly where this option is available.  Its a third party website which I need to crawl.
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Cenjoy100Commented:
Sorry
I misunderstood the question.
0
mmalik15Author Commented:
sonawanekiran: I know just the basics of jQuery. Can you please give me an example where to write that JQuery code to get all the links
0
nishant joshiTechnology Development ConsultantCommented:
you have to add row command event for specific 1 row using below code..

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridViewRow dr = (GridViewRow)sender;
        HyperLink h=(HyperLink) dr.FindControl("Hyperlink1");
    }

Open in new window



and for all rows in grid view


 
foreach (GridViewRow row in GridView1.Rows)
        {
            HyperLink hr=(HyperLink)row.FindControl("HyperLInk1");
        }

Open in new window


am i in correct direction or not???

Regards,
nishant
0
mmalik15Author Commented:
sorry nishantcomp2512: its a thrid party website and I need hyperlinks from that website.
0
nishant joshiTechnology Development ConsultantCommented:
sry mmalik i am having 4.0 so not able to help wait for other experts to raise


thanks,
nishant
0
nishant joshiTechnology Development ConsultantCommented:
few questions:
1)want to store links i database of just want to dispaly it on client side.
2)and if website contains iframe of our link then it will be fine or not?


0
nishant joshiTechnology Development ConsultantCommented:
37383550 is wrong comment please ignore it.thanks.
0
mmalik15Author Commented:
Thanks for the comments. I manage to extract the link using htmlagaility pack
HtmlAgilityPack.HtmlWeb web = new HtmlWeb();
        List<string> hrefTags = new List<string>();

        for (int i = 0; i <= 1240; i = i + 10)
        {

            HtmlAgilityPack.HtmlDocument doc = web.Load("http://cancerhelp.cancerresearchuk.org/trials/search-results/%20?SearchQuery=and%28crdtype%3astring%28%22ClinicalTrial%22%29%29and%28or%28crtrialstatus%3astring%28%22Closed%22%29%2ccrtrialstatus%3astring%28%22Open%22%29%2ccrtrialstatus%3astring%28%22Results%22%29%29%29&AdvancedSearchFormType=research_and_trials_adv_search_form&OffsetValue=" + i);



            foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//ol[@class=\"searchresults\"]//a[@href]"))
            {
                HtmlAttribute att = link.Attributes["href"];
                hrefTags.Add(att.Value);
            }
        }

        if (File.Exists(@"C:\inetpub\wwwroot\Demo\TextFile.txt"))
        {
            try
            {
                StreamWriter output = new StreamWriter(@"C:\inetpub\wwwroot\Demo\TextFile.txt");
                foreach (string Txt in hrefTags)
                {
                    output.WriteLine(Txt.ToString());
                }
                output.Close();
            }
            catch (System.IO.IOException exc)
            {
              string st= exc.Message;
            }

        }

Open in new window

0
nishant joshiTechnology Development ConsultantCommented:
i have chaged code using getElementById method please check it is working fine or not.


Regards,
nishant
HtmlDocument ht = new HtmlDocument();
        HtmlAgilityPack.HtmlWeb web = new HtmlWeb();
        List<string> hrefTags = new List<string>();

        for (int i = 0; i <= 1240; i = i + 10)
        {

            HtmlAgilityPack.HtmlDocument doc = web.Load("http://cancerhelp.cancerresearchuk.org/trials/search-results/%20?SearchQuery=and%28crdtype%3astring%28%22ClinicalTrial%22%29%29and%28or%28crtrialstatus%3astring%28%22Closed%22%29%2ccrtrialstatus%3astring%28%22Open%22%29%2ccrtrialstatus%3astring%28%22Results%22%29%29%29&AdvancedSearchFormType=research_and_trials_adv_search_form&OffsetValue=" + i);
            HtmlAgilityPack.HtmlNode div = doc.GetElementbyId("content");
            foreach (HtmlNode link in div.SelectNodes("//ol[@class=\"searchresults\"]//a[@href]"))
            {
                HtmlAttribute att = link.Attributes["href"];
                hrefTags.Add(att.Value);
            }
        }

        if (File.Exists(@"C:\inetpub\wwwroot\Demo\TextFile.txt"))
        {
            try
            {
                StreamWriter output = new StreamWriter(@"C:\inetpub\wwwroot\Demo\TextFile.txt");
                foreach (string Txt in hrefTags)
                {
                    output.WriteLine(Txt.ToString());
                }
                output.Close();
            }
            catch (System.IO.IOException exc)
            {
                string st = exc.Message;
            }

        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.