Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

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?
0
Jesper Christensen
Asked:
Jesper Christensen
1 Solution
 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now