山本一樹
山本一樹

Reputation: 157

How to convert text in a column to hyperlink in gridview

I have deserialized a xml file and bind the data to a gridview. One of the column has a url data displayed as text and I want to change it to hyperlink so that I can access the link. How do I go about doing it without manually editing the gridview template?

protected void Button1_Click(object sender, EventArgs e)
    {
        string searchTerm = TextBox1.Text;
        string mediaTerm = DropDownList1.SelectedItem.ToString();
        var webRequest = (HttpWebRequest)WebRequest.Create("http://itunes.apple.com/search?term=" + Server.UrlEncode(searchTerm) + " &media=" + Server.UrlEncode(mediaTerm));

        var webResponse = (HttpWebResponse)webRequest.GetResponse();
        if (webResponse.StatusCode == HttpStatusCode.OK)
        {
            JavaScriptSerializer json = new JavaScriptSerializer();
            StreamReader sr = new StreamReader(webResponse.GetResponseStream());
            string resString = sr.ReadToEnd();

            SearchList list = json.Deserialize<SearchList>(resString);

            GridView1.DataSource = list.results;
            GridView1.DataBind();

        }
        else
        {
            Label1.Text = "Invalid Response";
        }
    }

Upvotes: 0

Views: 2371

Answers (1)

Punching Worms
Punching Worms

Reputation: 113

Edit the cell text on RowDataBind event:

void GridView1_RowDataBound(Object sender, GridViewRowEvenArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        e.Row.Cells["UrlData"].Text = "<a href=\"" + e.Row.Cells["UrlData"].Text + "\>" + e.Row.Cells["UrlData"].Text + "</a>";

    }

}

Upvotes: 0

Related Questions