C# 如何将数据异步加载到gridview
我目前作为谷歌搜索引擎从一个网站获取链接。我必须通过单击一个按钮在文本框中获取一个关键字的链接。我有一些方法来获取数据,这些方法将被循环多次。因此我使用异步任务将这些方法循环多次,并且工作正常。问题我对异步任务的理解是,当我尝试将获取的数据绑定到aspx页面中的gridview时,第一次循环完成时,它没有绑定C# 如何将数据异步加载到gridview,c#,asp.net,gridview,asynchronous,C#,Asp.net,Gridview,Asynchronous,我目前作为谷歌搜索引擎从一个网站获取链接。我必须通过单击一个按钮在文本框中获取一个关键字的链接。我有一些方法来获取数据,这些方法将被循环多次。因此我使用异步任务将这些方法循环多次,并且工作正常。问题我对异步任务的理解是,当我尝试将获取的数据绑定到aspx页面中的gridview时,第一次循环完成时,它没有绑定 string[] cities = { "auburn", "bham", "dothan", "shoals", "huntsville", "mobile", "montgomer
string[] cities = { "auburn", "bham", "dothan", "shoals", "huntsville", "mobile", "montgomery", "tuscaloosa" };
string[] states = { "montgomery", "delaware" };
string[] categories = { "sss", "ccc", "eee", "ggg", "hhh", "jjj", "ppp", "res", "bbb" };
protected async void btnsearch_Click(object sender, EventArgs e)
{
dt.Columns.Add("Links");
dt.Columns.Add("title");
dt.Columns.Add("keyword");
dt.Columns.Add("city");
dt.Columns.Add("state");
dt.Columns.Add("S.no");
dt.Columns.Add("linkid");
for (int s = 0; s < Statename.Count; s++)
{
//looping for state
for (int i = 0; i < dscity.Tables[0].Rows.Count; i++)
{
//looping for categories
for (int m = 0; m < 1; m++)
{
for (int w = 0; w < word.Length; w++)
{
//looping for pages
for (int j = 0; j < pagecount; )
{
if (j == 0)
{
str = "http://" + dscity.Tables[0].Rows[i]["citycode"].ToString() + "website/search/?&areaID=&catAbb=" + categories[m] + "&query=" + word[w] + "&sort=rel";
}
else
{
str = "http://" + dscity.Tables[0].Rows[i]["citycode"].ToString() + "website/search/?s=" + j + "&areaID=&catAbb=" + categories[m] + "&query=" + word[w] + "&sort=rel";
}
// Task taskA = Task.Factory.StartNew(() =>
await TaskEx.Run(() => binddata(str, dscity.Tables[0].Rows[i]["citycode"].ToString(), categories[m], word[w], Statename[s]));
j = j + 100;
if ((pagecount % 100) != 0 || pagecount > 100)
{
pagecount = ((pagecount / 100) + 1) * 100;
}
else
{
}
}
}
}
}
}
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
public void binddata(string str, string city, string category, string keyword, string statename)
{
try
{
string[] result;
WebClient client = new WebClient();
string webhtml = client.DownloadString(str);
WebRequest req = WebRequest.Create(str);
WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string source = sr.ReadToEnd();
string[] location = new string[] { "<span class=\"crumb\">" };
location = source.Split(location, StringSplitOptions.RemoveEmptyEntries);
string[] location1 = new string[] { "<a href=\"/\">", "</a> ></span>" };
location1 = location[2].Split(location1, StringSplitOptions.RemoveEmptyEntries);
string state = location1[0];
string[] title1 = new string[] { "\">", "</a></span>" };
title1 = location[3].Split(title1, StringSplitOptions.RemoveEmptyEntries);
string title = title1[1];
string[] stringSeparators = new string[] { "<div class=\"content\">" };
stringSeparators = source.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
//spliting for pagecount
string[] resultcount = new string[] { "<span class=\"resultcount\">" };
resultcount = stringSeparators[0].Split(resultcount, StringSplitOptions.RemoveEmptyEntries);
if (resultcount.Length > 1)
{
string[] resultcount1 = new string[] { "</span></span>" };
resultcount = resultcount[1].Split(resultcount1, StringSplitOptions.RemoveEmptyEntries);
pagecount = Convert.ToInt32(resultcount[0]);
if (stringSeparators[1].Contains("<div class=\"toc_legend bottom\">"))
{
result = new string[] { "<div class=\"toc_legend bottom\">" };
}
else
{
result = new string[] { "<div id=\"floater\">" };
}
stringSeparators = stringSeparators[1].Split(result, StringSplitOptions.RemoveEmptyEntries);
string data = stringSeparators[0].Replace("<a class=\"gc\" href=", "");
data = data.Replace("href=\"#\"", "");
HtmlAgilityPack.HtmlDocument htmlSnippet = new HtmlAgilityPack.HtmlDocument();
htmlSnippet.LoadHtml("<!DOCTYPE html><html><head></head><body><div><div>" + data + "</body></html>");
List<string> hrefTags = new List<string>();
//bgw.RunWorkerAsync();
ExtractAllAHrefTags(htmlSnippet, city, category, title, keyword, statename);
}
}
catch (Exception ex)
{
throw (ex);
}
}
private void ExtractAllAHrefTags(HtmlAgilityPack.HtmlDocument htmlSnippet, string city, string category, string title, string keyword, string statename)
{
//bgw.RunWorkerAsync();
try
{
string ssss = string.Empty;
string temp = string.Empty;
List<string> hrefTags = new List<string>();
DataRow dr1;
var nodes1 = htmlSnippet.DocumentNode.SelectNodes("//p");
foreach (HtmlNode link in nodes1)
{
if (link.OuterHtml.Contains("=\"row\""))
{
// HtmlAttribute att =
//HtmlAttribute tag = link.ChildNodes[5].InnerHtml;
HtmlAgilityPack.HtmlDocument htmlSnippet1 = new HtmlAgilityPack.HtmlDocument();
htmlSnippet1.LoadHtml("<!DOCTYPE html><html><head></head><body> " + link.ChildNodes[5].InnerHtml + "</body></html>");
foreach (HtmlNode link1 in htmlSnippet1.DocumentNode.SelectNodes("//a[@href]"))
{
string desc = link1.InnerHtml;
HtmlAttribute att = link1.Attributes["href"];
string temp2 = att.Value.ToString();
if (temp2.Contains(".html"))
{
if (desc != "" && temp2 != "")
{
if (temp2.StartsWith("/") && temp2.Contains(".html"))
{
ssss = "http://" + city + ".website.org/" + temp2;
}
else
{
ssss = temp2;
}
dr1 = dt.NewRow();
dr1[0] = ssss;
dr1[1] = desc;
dr1[2] = keyword;
dr1[3] = city;
dr1[4] = statename;
dr1[5] = dt.Rows.Count + 1;
dr1[6] = "";
dt.Rows.Add(dr1);
}
}
}
}
}
}
catch (Exception ex)
{
}
string[]cities={“奥本”、“巴姆”、“多坦”、“浅滩”、“亨茨维尔”、“莫比尔”、“蒙哥马利”、“塔斯卡卢萨”};
string[]states={“蒙哥马利”、“特拉华”};
字符串[]类别={“sss”、“ccc”、“eee”、“ggg”、“hhh”、“jjj”、“ppp”、“res”、“bbb”};
受保护的异步无效BTN搜索单击(对象发送方,事件参数e)
{
dt.列。添加(“链接”);
dt.列。添加(“标题”);
添加(“关键字”);
dt.列。添加(“城市”);
dt.列。添加(“状态”);
dt.列。添加(“序号”);
添加(“linkid”);
对于(int s=0;s
等待TaskEx.Run(()=>binddata(str,dscity.Tables[0]。行[i][“citycode]”。ToString(),类别[m],字[w],状态名[s]);
j=j+100;
如果((页面计数%100)!=0 | |页面计数>100)
{
页面计数=((页面计数/100)+1)*100;
}
其他的
{
}
}
}
}
}
}
如果(dt.Rows.Count>0)
{
GridView1.DataSource=dt;
GridView1.DataBind();
}
}
公共void binddata(字符串str、字符串city、字符串类别、字符串关键字、字符串statename)
{
尝试
{
字符串[]结果;
WebClient客户端=新的WebClient();
string webhtml=client.DownloadString(str);
WebRequest req=WebRequest.Create(str);
WebResponse res=req.GetResponse();
StreamReader sr=新的StreamReader(res.GetResponseStream());
字符串源=sr.ReadToEnd();
字符串[]位置=新字符串[]{”“};
位置=source.Split(位置,StringSplitOptions.RemoveEmptyEntries);
字符串[]位置1=新字符串[]{”“};
location1=位置[2]。拆分(位置1,StringSplitOptions.RemoveEmptyEntries);
字符串状态=位置1[0];
字符串[]title1=新字符串[]{“\”>”,“”};
title1=位置[3]。拆分(title1,StringSplitOptions.RemoveEmptyEntries);
字符串标题=标题1[1];
字符串[]字符串分隔符=新字符串[]{”“};
StringSeparator=source.Split(StringSeparator、StringSplitOptions.RemoveEmptyEntries);
//分页计数拆分
string[]resultcount=新字符串[]{”“};
resultcount=stringSeparators[0]。拆分(resultcount,StringSplitOptions.RemoveEmptyEntries);
如果(resultcount.Length>1)
{
string[]resultcount1=新字符串[]{”“};
resultcount=resultcount[1]。拆分(resultcount1,StringSplitOptions.RemoveEmptyEntries);
pagecount=Convert.ToInt32(结果计数[0]);
if(stringseparator[1]。包含(“”)
{
结果=新字符串[]{”“};
}
其他的
{
结果=新字符串[]{”“};
}
StringSeparator=StringSeparator[1]。拆分(结果,StringSplitOptions.RemoveEmptyEntries);
string data=stringSeparators[0].Replace(“您可以使用Jquery来完成它,或者您确定所有114行core都是有意义的,并且必须出现在问题中吗?”,是的,我只是提供了代码的概要