こんにちは私の問題は、TweetText という名前のグリッド ビューに列があり、常に URL が含まれていることです。その URl をクリック可能なリンクとして作成したいと考えています。ページの読み込み時にこれを行うことができました。しかし、グリッドのページ番号を変更すると、TweetText のテキストは変更されません。ここに私のコードを書いています。私は GridView1_PageIndexChanged でもこのコードを実行しています。しかし、何も役に立ちません。もう1つ、列全体にリンクを設定したくないということです。列の URL をリンクとして作成したいだけです
if (!Page.IsPostBack)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
String Url = SmartyPlants.Classes.TwitterData.GetUrlStrings(row.Cells[5].Text);
bool Check = SmartyPlants.Classes.TwitterData.IsUrlValid(Url);
int Index = Url.IndexOf(" ");
if (Url.Contains(" "))
{
Url = Url.Remove(Index);
}
String link = MakeLink(Url);
row.Cells[5].Text = row.Cells[5].Text.Replace(Url, link);
}
}
public static string MakeLink(string txt)
{
Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
MatchCollection mactches = regx.Matches(txt);
foreach (Match match in mactches)
{
txt = txt.Replace(match.Value, "<a href='" + match.Value + "'>" + match.Value + "</a>");
}
public static bool IsUrlValid(string url)
{
return Regex.IsMatch(url, @"(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
}
public static string GetUrlStrings(string text)
{
MatchCollection ms = Regex.Matches(text, @"(www.+|http.+)([\s]|$)");
string testMatch = ms[0].Value.ToString();
return testMatch;
}
return txt;
}