简体   繁体   English

使用C#编辑中继器中的超链接

[英]Edit hyperlink inside repeater using c#

I'm trying to change Url of an asp hyperlink NavigateUrl inside Repeater control. 我正在尝试更改Repeater控件内的ASP超链接NavigateUrl的Url。 I filled out my repeater from database using below code: 我使用以下代码从数据库中填写了转发器:

in .cs file 在.cs文件中

        SqlConnection con3 = new SqlConnection("my connection");
        string strcon = "select * from companydata";
        cmd = new SqlCommand(strcon, con3);
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds, "company");
        Repeater1.DataSource = ds;
        Repeater1.DataBind();
        con3.Close();
        con3.Dispose();

in .aspx file <asp:Repeater ID="Repeater1" runat="server"><ItemTemplate> <asp:HyperLink ID="companyid" runat="server"><%#Eval("companyname") %></asp:HyperLink> //i need to add NavigateUrl for HyperLink using loop. </ItemTemplate></asp:Repeater> 在.aspx文件中<asp:Repeater ID="Repeater1" runat="server"><ItemTemplate> <asp:HyperLink ID="companyid" runat="server"><%#Eval("companyname") %></asp:HyperLink> //i need to add NavigateUrl for HyperLink using loop. </ItemTemplate></asp:Repeater> <asp:Repeater ID="Repeater1" runat="server"><ItemTemplate> <asp:HyperLink ID="companyid" runat="server"><%#Eval("companyname") %></asp:HyperLink> //i need to add NavigateUrl for HyperLink using loop. </ItemTemplate></asp:Repeater>

My output (using repeater) looking like this 我的输出(使用中继器)如下所示

  • Microsoft 微软
  • Google 谷歌
  • Yahoo 雅虎
  • Facebook Facebook的
  • Adobe 土砖

Now i add some extra code to fill NavigateUrl in Hyperlink inside repeater control 现在,我在中继器控件内的超链接中添加了一些额外的代码来填充NavigateUrl

foreach (RepeaterItem item in Repeater1.Items)
        {
            HyperLink hplink = item.FindControl("companyid") as HyperLink;
            hplink.NavigateUrl = "https://www.example.com/company/"; 
        }

Now my question is how to add HyperLink's text inside NavigateUrl like www.example.com/company/Microsoft or www.example.com/company/Google, Here Microsoft and Google are link's text. 现在我的问题是如何在NavigateUrl中添加HyperLink的文本,例如www.example.com/company/Microsoft或www.example.com/company/Google,此处MicrosoftGoogle是链接的文本。 Thanks in advance, sorry for my bad English. 在此先感谢您,我的英语不好。

You just need to concat the text of hyperlink with the navigateurl: 您只需要使用navigationurl链接超链接的文本:

foreach (RepeaterItem item in Repeater1.Items)
    {
        HyperLink hplink = item.FindControl("companyid") as HyperLink;
        hplink.NavigateUrl = "https://www.example.com/company/" + hplink.Text; 
    }

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

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