如何在aspx页面中的转发器ItemDataBound中传递函数中的Control.ClientID

How to pass Control.ClientID in function in repeater ItemDataBound in aspx page?

本文关键字:转发器 ItemDataBound Control ClientID 传递函数 aspx      更新时间:2023-12-11

我想调用一个JavaScript函数来折叠/展开。

我在跨度上的asp:repeater ItemTemplate中使用此代码

onclick="javascript:funCollExp(this,'<%= P1.ClientID %>');"

如何通过Control.ClientID

它将P1.ClientID替换为页面上的字符串。

你只需要像这个一样

"onclick="javascript:funCollExp(this,'" + P1.ClinetID + "');"

使用类似的itemdatabound事件的完整代码

标记

<asp:Repeater id="myRepeater" 
       OnItemDataBound="myRepeater_ItemDataBound" runat="server">
    <ItemTemplate>
        <asp:button id="myDiv" runat="server">......</asp:button>
    </ItemTemplate>
</asp:Repeater>

代码绑定

protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if(e.Item.ItemType == ListItemType.Item 
           || e.Item.ItemType == ListItemType.AlternatingItem)
    {
      Button mybtn = e.Item.FindControl("mybtn") as bUTTON;
      mybtn.Attributes.Add("ONCLICK", "MYFUNCTION(this,'" + P1.ClientID + "');");
    }
}

您所需要做的就是以这种方式使用

"onclick="javascript:funCollExp(this,'" + P1.ClinetID + "');"