0

こんにちは私の問題は、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\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", 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;
        }
4

2 に答える 2

1

既にデータがある場合HyperLinkは、グリッドビューでコントロールだけを使用して を使用しないのはなぜですか?Eval()

<asp:TemplateField>
    <ItemTemplate>
       <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='Default.aspx?ID=<%#Eval("id")%>' Text="TweetText"></asp:HyperLink>
     </ItemTemplate>
 </asp:TemplateField>

また

GridView OnRowDataBound イベント内に HyperLink を見つけることもできます。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if(e.Row.RowType == DataControlRowType.DataRow)
  {
    HyperLink myHyperLink = e.Row.FindControl("HyperLink1") as HyperLink;
    if(myHyperLink !=null)
    {
       //myHyperLink.NavigateUrl="URL"; 
    }
  }

}

于 2012-05-18T05:40:46.073 に答える
0

これを使って

    <asp:HyperLinkField DataNavigateUrlFields="id" Text="Details" ControlStyle-Font-old="true" 
DataNavigateUrlFormatString="~/pages/UserDetails.aspx?id={0}" />

DataNavigateUrlFormatString動的に設定するかHyperlink、ユーザーが使用できますitemtemplet

于 2012-05-18T05:13:07.160 に答える