How do you set href attribute of anchor tag that is within a repeater in code behind?

后端 未结 4 1483
庸人自扰
庸人自扰 2021-01-25 06:23

normally on would use the following :-

aspx page:-

Link1 

code behind:-

相关标签:
4条回答
  • 2021-01-25 06:57

    You can do it in the event ItemDatabound of your repeater:

    ((HtmlAnchor)e.Item.FindControl("a1")).HRef = "www.mySite.com/mypage.aspx";
    
    0 讨论(0)
  • 2021-01-25 07:12

    For example, in the ItemDatabound event:

    protected void rptData_ItemDataBound(object source, RepeaterCommandEventArgs e)
    {
        HtmlAnchor a1 = (HtmlAnchor)e.Item.FindControl("a1");
        a1.HRef = "www.mySite.com/mypage.aspx";
    }
    

    Also, don't forget to put runat="server" on that anchor

    <a ID="a1" runat="server" href="javascript:void(0);">Link1 </a>
    
    0 讨论(0)
  • 2021-01-25 07:17

    You can do this in the ItemDatabound event.

    Check out: http://www.codeguru.com/csharp/.net/net_asp/tutorials/article.php/c12065

    0 讨论(0)
  • 2021-01-25 07:19

    First you need to make your control server side by putting runat="Server"

        <a runat="Server" ID="a1" href="javascript:void(0);">Link1 </a>
    
    protected void rptOuter_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
           // Find your anchor here
        }
    }
    
    0 讨论(0)
提交回复
热议问题