简体   繁体   English

后面的Href链接代码

[英]Href Link Code Behind

I am trying to add aa link as part of the Product.Text but I've horribly got it wrong here, doing this on the C-Sharp code behind. 我正在尝试将一个链接添加为Product.Text的一部分,但我在这里确实犯错了,请在后面的C-Sharp代码中执行此操作。

tcProduct.Text = "<div class=\"productname\">" + "<a class=\"productnameLink\" href=\"item.Product.Url\">" + item.Product.Name + "</a>" + "</div>";

The backward slashes are beginning to confuse me. 反斜杠开始使我感到困惑。 Instead of a normal hyperlink i guess i should be using the .NET hyperlink instead?? 我想我应该使用.NET超链接而不是普通的超链接?

I've tried this but doesn't add the hyperlink; 我已经尝试过了,但是没有添加超链接;

            HyperLink productNameLink = new HyperLink();
            productNameLink.Text = "<div class=\"productname\">" + item.Product.Name + "</div>"; 
            productNameLink.NavigateUrl = item.Product.Url.ToString();
            productNameLink.CssClass = "prodNameLink";
            tcProduct.Controls.Add(productNameLink);

Plain string formatting should do the trick: 纯字符串格式应该可以解决问题:

tcProduct.Text = string.Format("<div class=\"productname\"><a class=\"productnameLink\" href=\"{0}\">{1}</a></div>", 
    item.Product.Url, item.Product.Name);

However, you really better use a Repeater which is meant exactly for such things. 但是,您确实最好使用一个Repeater,它正是用于此类事情。 Markup: 标记:

<asp:Repeater ID="rptProducts" runat="server">
    <HeaderTemplate><h1>Products</h1></HeaderTemplate>
    <ItemTemplate>
        <div class="productname"><a class="productnameLink" href="<%# Eval("Url") %>"><%# Eval("Name") %></a></div>
    </ItemTemplate>
</asp:Repeater>

And in the code behind: 并在后面的代码中:

rptProducts.DataSource = arrProducts;
rptProducts.DataBind();

Where arrProducts is the collection of your products. 其中arrProducts是您的产品的集合。

What i used to do that is ASP Literal. 我过去经常这样做的是ASP Literal。

<asp:Literal ID="Literal2" runat="server"></asp:Literal> 

Then in the code set to it with 然后在代码中设置为

Literal2.Text = "<a class='productnameLink' href=" + item.Product.Url + ">" + item.Product.Name + "</a>"

You can use Panel control which renders as div . 您可以使用面板控件将其渲染为div

var link = new HyperLink
{
    Text = item.Product.Name,
    NavigateUrl = item.Product.Url.ToString(),
    CssClass = "prodNameLink"
};
var panel = new Panel
{
    CssClass = "productname"
};
panel.Controls.Add(link);
tcProduct.Controls.Add(panel);

Output: 输出:

<div class="productname">
   <a class="prodNameLink" href="http://www.google.com">Google</a>
</div>

The Text property is supposed to be text only. Text属性应该仅是文本。
Many ways to do what you need to, but if you want to stick to using html string you could use: 有很多方法可以满足您的需要,但是如果您要坚持使用html字符串,可以使用:

tcProduct.Controls.Add(new HtmlGenericControl
        {
            InnerHtml = string.Format(@"<div class=""productname""><a class=""productnameLink"" href=""{0}"">{1}</a></div>", item.Product.Url, item.Product.Name )
        });

Bonus, you can get rid of the '\\' escape char with averbatim string literal: @"" (you need to double the quotes). 另外,您可以使用averbatim字符串文字:@“”摆脱'\\'转义字符(您需要将双引号引起来)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM