Eval script for server side control’s ID property?

Using the following Eval script for setting ID property causes error.
Error message: the server tag is not well formed.

 <asp:Panel runat="server" ID="<%# Eval("RENTER_ID") %>" Visible="false">

Even replacing “” with ” of ID property generates error.
While using ”, its error message

“The ID property of a control can only be set using the ID attribute in the tag and a simple value. Example: <asp:Button runat="server" id="Button1" />

Any ideas to solve this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can’t do it.

Why do you need to? If it’s so you can reference it at some point, you can access the client-side id via the property ClientID.

Example, as requested:

<asp:Repeater runat="server" ID="repFoo">
    <ItemTemplate>
        <asp:Panel runat="server" ID="pnlFoo">
            <input type = "button"
                onclick = "alert('<%# Container.FindControl("pnlFoo").ClientID %>');"
                value   = "click to get id for <%# Container.ItemIndex %>" />
        </asp:Panel>
    </ItemTemplate>
</asp:Repeater>

Method 2

As Silky said, you can’t do this. The attributes can’t be dynamic in none code behind. One solution is to subclass the Panel:

public class MyPanel : Panel
{
    public override string ID
    {
        get
        {
            return "get from my datasource";
        }
        set
        {
            // noop
        }
    }
}

Method 3

Do you need to use a panel? Could you just use html?

<div id="<%# Eval("RENTER_ID") %>" style="display:none"></div>

Method 4

Try this

 <div runat="server" id='<%# Eval("IsVisible") %>' visible="false"> </div>

Try this – It will not popup any messages if you do formatting, but it will show Design time error.

<asp:Panel runat="server" ID='<%# Eval("RENTER_ID") %>' Visible="false">

Method 5

asp.net controls’ ID doesn’t support the binding. Try to use HTML controls to work around.

Method 6

It almost always when asp.net fails , comes Jquery into the field to set the situation up 😉 . you can do this trick for example :

In HTML:

<div>
<input type="file" id="whatever" class="uploader" runat="server" />
<span id="<%#Eval("ID")%>'"></span>
</div>

Jquery:

    $('.uploader').on('change', function () {
        var id = $(this).next('span').attr('id');
     });


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x