0

I have programmed a simple blog page - http://www.3don.net.br/Blog.aspx (another language, here only to show the structure). I want to use hashtags for pointing to the topics. For example, http://www.3don.net.br/Blog.aspx#19/04/16 should scroll the page to the topic created at 19/04/16.

However, I cannot get it!

The topics of the blog are ItemTemplates of a ListView control. When I define an ID="lblDatum" for the label control of the data of each topic (which is the first control of each topic), then this ID is modified by the NET machine to id="ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl01_lblDatum" (you can see it in the source code of the page for the second topic, for example).

So, if I access in the browser www.3don.net.br/Blog.aspx#ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl01_lblDatum the page indeed will scroll correctly. I can also programmatically change the ID for each topic differently and it still works for each topic.

However, the hashtag-name "ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl01_lblDatum" is not nice! Is there a possibility to suppress the ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl01_-part?

Or another idea for getting it?

Jog
  • 1
  • 2
  • Possible duplicate of [How to stop ASP.NET from changing IDs in order to use jQuery](http://stackoverflow.com/questions/497802/how-to-stop-asp-net-from-changing-ids-in-order-to-use-jquery) – Tony Hinkle May 20 '16 at 20:21

6 Answers6

0

Use ClientIdMode="Static" in your ListItem, which removes the autoGenerate ID, you got ID="lblDatum" on client.

Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
  • But he will end up with several elements having the same ID. – ConnorsFan May 20 '16 at 18:58
  • Daniel, thanks. Sounds fine -- but does not work. If have VisualStudio2015 with NET Framework 4.6. installed but the ClientIDMode property does not (auto-)appear. No changes if I enter it manually in ListView control, nor in any listitem-control. – Jog May 20 '16 at 20:40
  • ConnorsFan, I can define an ID for each control in my code behind. Thank you – Jog May 20 '16 at 20:41
0

ok, I added the clientIDMode="static" property to the Label control of the data:

<table id="table_intern" runat="server" >
  <tr id="tr_intern" runat="server">
    <td id="td_intern" runat="server">
      <asp:Label ID="lblDatum" runat="server" Text='<%# Eval("data") %>' CssClass="rotfettschrift" clientidmode="static"/>
    </td>
  </tr>
  <tr> ... </tr>
  <tr> ... </tr>
</table>

... and, in code-behind, set the ID of this control of each topic igual of the text property of this control (the data):

lbl = CType(e.Item.FindControl("lblDatum"), Label)
lbl.ID = lbl.Text

However, the HTML-output is:

<tbody>
  <tr id="ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl00_tr_intern">
    <td id="ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl00_td_intern">
      <span id="ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl00_19/05/16" class="rotfettschrift" clientidmode="static">19/05/16</span>
    </td>
  </tr>
  <tr> ... </tr>
  <tr> ... </tr>
</tbody>

Why is the ID of the span element ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl00_19/05/16 and not just 19/05/16 ???

Jog
  • 1
  • 2
0

Ok, i tested here, using reapeater, but with any other control works. You must set the id individually to each control, to make unique. *I redid, using date.

MARKUP:

     <table id="table_intern">
            <asp:Repeater runat="server" ID="repeater" OnItemDataBound="repeater_ItemDataBound">
                <ItemTemplate>
                    <tr>
                        <td>
                            <asp:Label Text="" runat="server" ID="label" />
                        </td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
        </table>

BACKEND:

 protected void Page_Load(object sender, EventArgs e)
    {

        var list = new List<string>();

        for (int i = 0; i < 10; i++)
        {
            list.Add(string.Format("Item_{0}", i.ToString().PadLeft(2, '0')));
        }
        repeater.DataSource = list;
        repeater.DataBind();

    }



    protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            var label = (Label)e.Item.FindControl("label");
            var item = (DateTime)e.Item.DataItem;
            label.ID = item.ToShortDateString();
            label.Text = item.ToShortDateString();
            label.ClientIDMode = ClientIDMode.Static;
        }
    }

RESULT:

        <table id="table_intern">
            <tbody><tr>
                        <td>
                            <span id="23/05/2016">23/05/2016</span>
                        </td>
                    </tr>

                    <tr>
                        <td>
                            <span id="24/05/2016">24/05/2016</span>
                        </td>
                    </tr>...

Sorry, for bad formatting, i will improve my answers if necessary.

0

Daniel,

thank you for dedicating your time.

You are defining the ClientIDMode property of the Label control in the code behind, ok. I do so, but I see that it does not work here:

screenshot

When I go with the mouse pointer over the ClientIDMode at the left it advices: "'ClientIDMode' is not a member of 'Label'" and over the ClientIDMode at the right, it pops "'ClientIDMode' is not declared. It may be inaccessible due to its protection level."

Is there something undefined in my system?

Jog
  • 1
  • 2
0

Ok, i understand you using Framework2.0 in your project. Can you change Framework to 4.0 or upper? Otherwise you need use a .js approach, to scroll like wish.

ClientIdMode is from .net 4.0, how you have said .net4.6 installed, I assumed that the project was also 4.6.

IF, you can't change the version, you need use something like jquery to scroll. OR maybe a more hard solution creating a item in your itemTemplate like:

<span id='<%# Eval("data") %>'></span>

I don't have tested this solution, later I'll be testing;

  • Daniel, your HTML-idea does the job! So simple ..... Thank you very much, it is already working. – Jog May 25 '16 at 22:16
  • This works, but to verify the version, you need to go on properties from project. – Daniel Silveira May 30 '16 at 12:30
  • Yes, I found it. Framwork 3.5 was active (because I originally started my project in an older VStudio version where only FW3.5 was possible). Obrigado, Daniel Silveira. – Jog May 30 '16 at 21:24
0

Daniel,

I always suspected that, but the "About Visual Studio" window tells another story.

Adicionally, in the registry I can find the v4/Full key and the Version information (at the right side) confirms the (installed and activated?) version 4.6.01055.

???

Or is framework v4 installed but not "activated"? Does this exist? Where can I see this?

Jog
  • 1
  • 2