1

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?

Ahmed Atia
  • 17,848
  • 25
  • 91
  • 133

6 Answers6

3

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>
Noon Silk
  • 54,084
  • 6
  • 88
  • 105
  • That panel is to be rendered for each item in GridView to be later expanded/collapsed. – Ahmed Atia Dec 07 '09 at 10:46
  • If you are doing the same but for div element, it works properly.
    " style="display: none; position: relative">
    – Ahmed Atia Dec 07 '09 at 10:46
  • Yes, that's because it's not a server control. Effectively you are trying to `name a variable` at run time; and that's just not possible. To get a unique ID, for expanding/collapsing, use the `ClientID`, like I suggest. (Something like: – Noon Silk Dec 07 '09 at 10:50
  • Please explain using ClientID more? How to use it to retrieve div of that item? – Ahmed Atia Dec 07 '09 at 10:59
  • Works properly, thanks, but what in case if I need to get that Panel ID for selected item in code behind? – Ahmed Atia Dec 07 '09 at 11:19
  • What do you mean? You do the same thing: Determine the row, search for it using the ID `pnlFool` and `FindControl`? – Noon Silk Dec 07 '09 at 11:22
  • Determine row is done using SelectedIndex for GridView. I need to use that index to find panel in code behind? Something like this : ((Panel)gvCustomers.Rows[rowIndex].FindControl("pnlDocuments")).Visible = true; If you are going to run this, panel reference is null as nothing found? So, I just asking how to use ClientID for panel to set its properities in code behind. – Ahmed Atia Dec 07 '09 at 11:26
  • And where are you executing that code? In OnDataBound? That's where I would do it; and in that case you don't use the `ClientID`, you just use what you write in the "ID" (i.e. 'pnlDocuments'). Take some time to work up a small example of this, and test it yourself. I leave it with you. – Noon Silk Dec 07 '09 at 11:28
1

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
        }
    }
}
Chris S
  • 64,770
  • 52
  • 221
  • 239
0

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

0

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">
Anuraj
  • 18,859
  • 7
  • 53
  • 79
  • This also raises an error. Error: The ID property of a control can only be set using the ID attribute in the tag and a simple value. Example: and – Ahmed Atia Dec 07 '09 at 10:43
  • Yes, I think it is only for server controls. But you can try like
    , atleast it is not displaying design time errors.
    – Anuraj Dec 07 '09 at 10:51
  • this also won't work. runat="server" generates the same error for panel. – Ahmed Atia Dec 07 '09 at 11:02
  • @Anuraj - how did you get over 8,000 reputation points with a poorly researched answer as this? I am dumbfounded – Fandango68 Jun 14 '16 at 00:25
0

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

<div id="<%# Eval("RENTER_ID") %>" style="display:none"></div> 
naspinski
  • 34,020
  • 36
  • 111
  • 167
0

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');
     });    
Nani
  • 11
  • 2