最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How do I find a ClientId of control in a Listview? - Stack Overflow

matteradmin6PV0评论

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...

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...

Share Improve this question edited May 23, 2017 at 12:33 CommunityBot 11 silver badge asked Jan 18, 2012 at 23:27 GioGio 4,2293 gold badges32 silver badges33 bronze badges 4
  • maybe so but it didn't help... – Gio Commented Jan 18, 2012 at 23:34
  • Try #<%=Control.ClientID%> and the error defines that if you dont give any spaces between each property it gives you that error. – coder Commented Jan 18, 2012 at 23:40
  • Are you sure that syntax is correct? I tried <%# control.clientID %> and that got rid of the 'badly formed..' message but now all I have is the actual &lt;#control.clientid # which doesn't work. – Gio Commented Jan 18, 2012 at 23:51
  • I'm not using events i'm using javascript tied to onclick – Gio Commented Jan 19, 2012 at 0:00
Add a ment  | 

1 Answer 1

Reset to default 3

As far as I can tell, there are two ways to acplish 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/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/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>
Post a comment

comment list (0)

  1. No comments so far