开发者

Embedded code in repeater

I am using a databound repeater component with some click-sensitive panel inside.

<ItemTemplate>
        <asp:Panel ID="PanelContent" runat="server">
            <asp:Panel ID="PanelMenuTitle" runat="server" 
    开发者_如何学运维            ondblclick="EditMenu(<%# Eval("ID") %>)">

As you can see I want to pass the ID of the current data item to a javascript function called EditMenu().

However, this code breaks due to "The server tag is not well formed.". I also tried everything I can think of: Using <%= instead of <%#, Bind() instead of Eval(), ' instead of " without success.


Use single quotes around the ondblclick function. That should fix it. See below.

ondblclick='EditMenu(<%# Eval("ID") %>)'

My second suggestion would be to use another control, like a ListView or a DataList, so that you can assign data keys to hold the ID. Then you can assign the ondblclick event in the ItemDataBound event like this.

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        Panel pnlCtrl = (Panel)e.Item.FindControl("Panel1");
        if (pnlCtrl != null)
        {
            pnlCtrl.Attributes["ondblclick"] = String.Format("EditMenu({0})", ListView1.DataKeys[((ListViewDataItem)e.Item).DisplayIndex]["ID"]);
        }
    }
}


Alright, I got it working. Not the original approach, but in the end it's what I wanted:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Panel pnlCtrl = (Panel)e.Item.FindControl("PanelMenuTitle");
            if (pnlCtrl != null)
            {
                myMenu menu = (e.Item.DataItem as myMenu);
                pnlCtrl.Attributes["ondblclick"] = String.Format("EditMenu('{0}')", menu.ID);
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜