如何在列表视图中找到控件的 ClientId?

发布于 2024-12-27 12:58:46 字数 768 浏览 5 评论 0原文

这个问题非常类似于 如何查找ASP.NET GridView 中控件的客户端 ID?

但是,我使用的是列表视图和标签:

<ItemTemplate>
     <asp:ImageButton ImageUrl="Resources/info.png" ToolTip="info" OnClientClick="toggle('<%#((label)Container).FindControl( "PresetUploadDescription").ClientID %>');"  ID="Description" runat="server"/>
     <asp:Label ID="UploadDescription"  BorderStyle="Solid" BorderColor="Goldenrod" BorderWidth="1" runat="server" Width="40em" CssClass="sc-Upload-description" Text='<%# Eval("Description") %>'></asp:Label>
....  

我在 findcontrol() 函数中收到“服务器标记格式不正确”...有什么想法吗?我尝试过“标签”和“控制”演员......

This question is very similar to
How do I find the Client ID of control within an ASP.NET GridView?

However I'm using a listview and a label:

<ItemTemplate>
     <asp:ImageButton ImageUrl="Resources/info.png" ToolTip="info" OnClientClick="toggle('<%#((label)Container).FindControl( "PresetUploadDescription").ClientID %>');"  ID="Description" runat="server"/>
     <asp:Label ID="UploadDescription"  BorderStyle="Solid" BorderColor="Goldenrod" BorderWidth="1" runat="server" Width="40em" CssClass="sc-Upload-description" Text='<%# Eval("Description") %>'></asp:Label>
....  

I'm getting a "The server tag is not well formed" at the findcontrol() function...Any ideas why? I've tried both 'label' and 'control' casts...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想挽留 2025-01-03 12:58:46

据我所知,有两种方法可以完成您想要做的事情。使用 asp:ImageButton 服务器控件并使用 OnItemDataBound 事件连接 onclick 客户端事件,或者仅使用 <输入 type="image" /> 控件并内联连接 ClientID。以下示例显示了这两种方法:

<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"><title>OnClick Test</title></head>
<body>
<form id="form1" runat="server">
<div>
    <asp:ListView ID="lv1" OnItemDataBound="lv1_ItemDataBound" runat="server">
        <ItemTemplate>
            <asp:Label ID="label1" Text="<%# Container.DataItem %>" runat="server" />
            <asp:ImageButton ID="btn1" 
                             ImageUrl="myimage.jpg" 
                             AlternateText="Show Text"
                             runat="server" />
            <input type="image" src="myimage.jpg" alt="Show Text"
                   onclick="alert(document.getElementById('<%# Container.FindControl("label1").ClientID %>').innerText);"
            />
            <br />
        </ItemTemplate>
    </asp:ListView>
</div>
</form>
</body>
</html>
<script runat="server">
public void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack) return;
    lv1.DataSource = new[] {"Manny", "Moe", "Jack"};
    lv1.DataBind();
}

protected void lv1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    var label1 = e.Item.FindControl("label1") as Label;
    var btn1 = e.Item.FindControl("btn1") as ImageButton;
    if (label1 == null || btn1 == null) return;
    btn1.Attributes.Add("onclick", "alert(document.getElementById('" + label1.ClientID + "').innerText);");
}
</script>

As far as I can tell, there are two ways to accomplish what you are looking to do. Either use an asp:ImageButton server control and wire up the onclick client event using the OnItemDataBound event, or simply use an <input type="image" /> control and wire up the ClientID inline. The following example shows both approaches:

<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"><title>OnClick Test</title></head>
<body>
<form id="form1" runat="server">
<div>
    <asp:ListView ID="lv1" OnItemDataBound="lv1_ItemDataBound" runat="server">
        <ItemTemplate>
            <asp:Label ID="label1" Text="<%# Container.DataItem %>" runat="server" />
            <asp:ImageButton ID="btn1" 
                             ImageUrl="myimage.jpg" 
                             AlternateText="Show Text"
                             runat="server" />
            <input type="image" src="myimage.jpg" alt="Show Text"
                   onclick="alert(document.getElementById('<%# Container.FindControl("label1").ClientID %>').innerText);"
            />
            <br />
        </ItemTemplate>
    </asp:ListView>
</div>
</form>
</body>
</html>
<script runat="server">
public void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack) return;
    lv1.DataSource = new[] {"Manny", "Moe", "Jack"};
    lv1.DataBind();
}

protected void lv1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    var label1 = e.Item.FindControl("label1") as Label;
    var btn1 = e.Item.FindControl("btn1") as ImageButton;
    if (label1 == null || btn1 == null) return;
    btn1.Attributes.Add("onclick", "alert(document.getElementById('" + label1.ClientID + "').innerText);");
}
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文