Make dynamic ID in hyperlink

Hi.

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?
Jesper ChristensenProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Miguel OzSoftware EngineerCommented:
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.
0
yogsoftCommented:
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.
0
Jesper ChristensenProgrammerAuthor Commented:
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?
0
yogsoftCommented:
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">
        <ItemTemplate>
            <asp:Label Text='<%# Eval("Value") %>' runat="server" />
            <asp:PlaceHolder ID="phLink" runat="server" />
        </ItemTemplate>
    </asp:ListView>

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;
                ListView1.DataBind();
            }
        }

        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";
                    phLink.Controls.Add(hypControl);
                }
            }
        }

Open in new window


Hope this will solve your problem.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.