tags:

views:

366

answers:

4

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?

A: 

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
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: <asp:Button runat="server" id="Button1" />and
Ahmed
Yes, I think it is only for server controls. But you can try like <div runat="server" id='<%# Eval("IsVisible") %>' visible="false"></div>, atleast it is not displaying design time errors.
Anuraj
<div runat="server" id='<%# Eval("IsVisible") %>' visible="false"> </div>this also won't work. runat="server" generates the same error for panel.
Ahmed
Yes it is also giving runtime error.
Anuraj
+1  A: 

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
That panel is to be rendered for each item in GridView to be later expanded/collapsed.
Ahmed
If you are doing the same but for div element, it works properly.<div id="<%# Eval("CustomerID") %>" style="display: none; position: relative">
Ahmed
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: <script>alert("<%# FooControl.ClientID %>");).
Noon Silk
Please explain using ClientID more? How to use it to retrieve div of that item?
Ahmed
I have added an example. I hope it is clear.
Noon Silk
Works properly, thanks, but what in case if I need to get that Panel ID for selected item in code behind?
Ahmed
What do you mean? You do the same thing: Determine the row, search for it using the ID `pnlFool` and `FindControl`?
Noon Silk
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
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
A: 

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

<div id="<%# Eval("RENTER_ID") %>" style="display:none"></div>
naspinski
Either asp:panel or div; its visible property should be set server side.So, we need that id? Any ideas?
Ahmed
+1  A: 

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
return "get from my datasource"; means?
Ahmed
Whatever your 'RENTER_ID' is from. You'd have to give your Panel a property to grab this from.
Chris S