如何在我的代码隐藏文件中格式化超链接列gridview?

时间:2016-02-10 17:40:27

标签: c# asp.net gridview hyperlink umbraco

我正在尝试在网格视图中将列设为超链接。我使用Umbraco 6子页作为数据源。我有一个链接,但目前它将在/ space / masterpages / url中占用%20%

我的观点是这样的:

<asp:hyperlinkfield datatextfield="title" datanavigateurlfields="title" headertext="Title" />

和背后的代码如下:

protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {

            HyperLinkField title = new HyperLinkField();
            string[] dataNavigateUrlFields = { "title" };

            title.DataTextField = "title";
            title.DataNavigateUrlFields = dataNavigateUrlFields;
           title.HeaderText = "Title";
           title.DataNavigateUrlFormatString = "item.Url";

            // Create a BoundField object to display the company's city.


            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[3] { new DataColumn("title", typeof(String)),


                            new DataColumn("lastUpdated", typeof(string)),
                            new DataColumn("theme",typeof(string)) });


            int i = 0;
            foreach (var item in uQuery.GetCurrentNode().ChildrenAsList)


            {
                var dateTimeString = item.GetProperty("lastUpdated").Value.ToString();
                var dateTime = System.Xml.XmlConvert.ToDateTime(dateTimeString);
               dt.Rows.Add(item.Name, dateTime.ToString("dd.MM.yyyy"), item.GetProperty("theme").Value.ToString());


            }

            Session["data"] = dt;
            Cache["Data"] = dt;
            Practice.DataSource = dt;
            Practice.DataBind();

        }

任何人都可以告诉我我做错了什么 - 谢谢

1 个答案:

答案 0 :(得分:0)

我想通了,但是在名为name的表中添加了一个额外的列(因为除非你在输出中引用它,否则不会显示,删除:

 HyperLinkField title = new HyperLinkField();
            string[] dataNavigateUrlFields = { "title" };

            title.DataTextField = "title";
            title.DataNavigateUrlFields = dataNavigateUrlFields;
           title.HeaderText = "Title";
           title.DataNavigateUrlFormatString = "item.Url";

            // Create a BoundField object to display the company's city.

并将datatextfield更改为&#34; name&#34;即DataTextField =&#34; name&#34;。

如果有其他人被卡住了:)