如何将列中的文本转换为gridview中的超链接

时间:2013-11-28 02:57:28

标签: asp.net gridview hyperlink

我已经反序列化了一个xml文件并将数据绑定到gridview。其中一个列的url数据显示为文本,我想将其更改为超链接,以便我可以访问该链接。如何在不手动编辑gridview模板的情况下进行此操作?

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";
        }
    }

1 个答案:

答案 0 :(得分:0)

在RowDataBound事件上编辑单元格文本:

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>";

    }

}