使用jQuery和.NET Framework 4访问HiddenField值
我很难访问一些动态创建的控件的值,这些控件包含图像、标签和隐藏字段 这就是我正在做的:使用jQuery和.NET Framework 4访问HiddenField值,jquery,.net,visual-studio-2010,Jquery,.net,Visual Studio 2010,我很难访问一些动态创建的控件的值,这些控件包含图像、标签和隐藏字段 这就是我正在做的: 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
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
代码隐藏
公共部分类报告图标:System.Web.UI.UserControl
{
公共字符串reportName{get;set;}
公共字符串文件路径{get;set;}
受保护的无效页面加载(对象发送方、事件参数e)
{
reportnamelab.Text=this.reportName;
FilePathHiddenField.Value=this.filePath;
}
}
JQuery
函数setupMouseEvents(){
$('.report icons')。悬停(
函数(){$(this).css({'background':''\B8B8B8})},
函数(){$(this.css({'background':'white'})}
);
$('.report icons')。单击(
函数(){
$(this.sibles().removeClass(“选定”);
$(此).addClass(“选定”);
警报($('#').val());
}
);
};
函数init(){
setupMouseEvents();
};
$(文档).ready(函数(){
init();
});
我认为问题在于,我的html包含在一个内容占位符中,因为我正在使用母版页
在浏览器中查看HTML时,隐藏字段的ID为:
ContentPlaceholder 1\u ReportListView\u ctl00\u FilePathHiddenField
ContentPlaceholder 1\u ReportListView\u ctl01\u FilePathHiddenField
ContentPlaceholder 1\u ReportListView\u ctl02\u FilePathHiddenField
ContentPlaceholder 1_ReportListView_ctl03_FilePathHiddenField
有人知道如何在jQuery中访问这些值吗?您需要为jQuery的id选择器添加#签名
alert($('#' + '<%=FilePathHiddenField.ClientID%>').val());
alert($('#'+'').val();
或
varid='';
警报($('#'+id.val());
对于Asp.net 4.0
如果您使用的是Asp.net 4.0版本,请注意:
Control.ClientID属性-获取或设置用于生成ClientID属性值的算法
AutoID
ClientID值是通过将每个父命名容器的ID值与控件的ID值连接起来生成的。在呈现控件的多个实例的数据绑定场景中,在控件的ID值前面插入递增值。每个段由下划线字符(\)分隔。此算法用于早于ASP.NET 4的ASP.NET版本
静态
ClientID值设置为ID属性的值。如果控件是命名容器,则该控件将用作其包含的任何控件的命名容器层次结构的顶部
可预测的
此算法用于数据绑定控件中的控件。ClientID值是通过将父命名容器的ClientID值与控件的ID值连接起来生成的。如果控件是生成多行的数据绑定控件,则在末尾添加ClientIDRowSuffix属性中指定的数据字段的值。对于GridView控件,可以指定多个数据字段。如果ClientIDRowSuffix属性为空,则会在末尾添加序号,而不是数据字段值。该数字从零开始,每行递增1。每个段由下划线字符(\)分隔
继承
该控件继承其NamingContainer控件的ClientdMode设置。我的jQuery中确实有一个#:)至于ClientdMode属性,我不知道该使用什么。如果它只是一个控件,那么静态将是合适的。但是这些控件是动态创建的,页面可以有1个或100个。你有什么建议吗?还有其他建议吗?不幸的是,在我弄明白这一点之前,我一直被困在水里。这一切都与动态创建这些控件(一次可以创建一个或多个)、在运行时分配它们的ID以及它们包含在内容占位符中有关。
var id = '<%=FilePathHiddenField.ClientID%>';
alert($('#' + id).val());