我很难访问一些动态创建的包含图像、标签和隐藏字段的控件的值。
这就是我正在做的事情:
HTML
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ReportIcon.ascx.cs" Inherits="ReportIcon" %>
<div class="report-icons" style="background:white; color:#014886; border-radius:5px 10px 5px 10px / 10px 5px 10px 5px; width:255px; position:relative; text-align:Left; visibility:visible; padding:5px; margin:5px; overflow: hidden">
<asp:Image ID="PDFImage" runat="server" ImageUrl="~/Images/pdfIcon.png"/>
<div style="position:absolute; left:64px; top:27px; max-width:196px; overflow: auto">
<asp:Label ID="ReportNameLabel" style="font-size:12pt; padding-left:15px" runat="server" Text="Report"></asp:Label>
<br />
<asp:HiddenField ID="FilePathHiddenField" runat="server" />
</div>
</div>
Code Behind
public partial class ReportIcon : System.Web.UI.UserControl
{
public string reportName { get; set; }
public string filePath { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
ReportNameLabel.Text = this.reportName;
FilePathHiddenField.Value = this.filePath;
}
}
JQuery
function setupMouseEvents() {
$('.report-icons').hover(
function () { $(this).css({ 'background': '#B8B8B8' }) },
function () { $(this).css({'background': 'white' })}
);
$('.report-icons').click(
function () {
$(this).siblings().removeClass("selected");
$(this).addClass("selected");
alert($('#<%=FilePathHiddenField.ClientID%>').val());
}
);
};
function init() {
setupMouseEvents();
};
$(document).ready(function () {
init();
});
我认为问题在于我的 html 包含在内容占位符中,因为我正在使用母版页。
当我在浏览器中查看 HTML 时,隐藏字段的 ID 为: ContentPlaceHolder1_ReportListView_ctl00_FilePathHiddenField ContentPlaceHolder1_ReportListView_ctl01_FilePathHiddenField ContentPlaceHolder1_ReportListView_ctl02_FilePathHiddenField ContentPlaceHolder1_ReportListView_ctl03_FilePathHiddenField
任何人都知道如何在 jQuery 中访问这些属性?