Make dynamic ID in hyperlink

Posted on 2011-10-02
Last Modified: 2013-12-16

I get an error if i try to name the ID from database:

<asp:HyperLink runat="server" ID='<%# Eval("URL").ToString() %>' NavigateUrl='<%# Eval("URL").ToString() %>' Text='<%# Eval("Navn") %>'></asp:HyperLink>

Is there a workaround?
Question by:bongii
    LVL 35

    Expert Comment

    by:Miguel Oz
    URL may contain invalid characters (eg. ?, &), thus you can not use as an ID string.
    Does your table in the database contains a unique ID that you can use?
    Alternative: You can leave the ID blank.
    LVL 14

    Expert Comment

    by:nishant joshi
    LVL 2

    Expert Comment

    If you are using data control like GridView/DataGrid/Data Repeater you can add Hyperlink control dynamically in ItemDataBound/RowDataBound event. Where you can set control id and other properties dynamically.

    Hope this has given you alternative solution.

    Author Comment

    mas_oz2003: I need to find the ID from the codebehind page to add a css class on the id.
    The hyperlinks is categories from the database. When I click a category, the hyperlink need to be underlined or something like this

    So thats why I need to have an ID.
    <asp:HyperLink runat="server" ID='<%# Eval("ID").ToString() %>' NavigateUrl='<%# Eval("URL").ToString() %>' Text='<%# Eval("Navn") %>'></asp:HyperLink>
    dosent work.

    yogsoft: I am using a ListView. Is your solution also the worksround with listview?
    LVL 2

    Accepted Solution

    Here is example for list view..  Use place holder control to place your link dynamically... You can change your complex object that you will be binding in place of Dictionary.

    Asp page:

    <asp:ListView ID="ListView1" runat="server" OnItemDataBound="ListView1_ItemDataBound">
                <asp:Label Text='<%# Eval("Value") %>' runat="server" />
                <asp:PlaceHolder ID="phLink" runat="server" />

    Open in new window

    Code behind:

    protected void Page_Load(object sender, EventArgs e)
                if (!IsPostBack)
                    var testDict = new Dictionary<string, string>();
                    testDict.Add("Test1", "Test 1 Value");
                    testDict.Add("Test2", "Test 2 Value");
                    ListView1.DataSource = testDict;
            protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
                if (e.Item.ItemType == ListViewItemType.DataItem)
                    var phLink = e.Item.FindControl("phLink") as PlaceHolder;
                    if (phLink != null)
                        var hypControl = new HyperLink();
                        var kvp = (KeyValuePair<string, string>)e.Item.DataItem;
                        hypControl.ID = kvp.Key;
                        hypControl.Text = kvp.Value;
                        hypControl.NavigateUrl = "http://yourURhere";

    Open in new window

    Hope this will solve your problem.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now