[英]Accessing HTML5 Custom Data Attributes for multiple instances of webpart
我真的很感謝一些指導。 這對你們中的某些人可能很簡單,但我無法弄清楚。 感謝您的任何投入。
要求
我有一個多選項卡的控件。 每個選項卡都有一個自定義reportviewer控件。
我在名為“ data-report-param”的代碼中向報表查看器添加了一個自定義屬性。
我需要使用javascript訪問客戶端當前標簽上的自定義屬性“ data-report-param”的值。
我嘗試了幾種方法,包括以下方法, 但無法獲得在DOM中創建的值。
我的密碼
//Attempt 1
var reportparamattribute = $('#ReportViewer1');
var reportparametervalue = reportparamattribute.getAttribute('data-report-param');
//Attempt 2
var reportparamattribute = document.getElementById('<%= ReportViewer1.ClientID %>');
var reportparametervalue = reportparamattribute.getAttribute('data-report-param');
//Also tried accessing the dataset
var reportparametervalue = reportparamattribute.dataset.report-param;
DOM中產生的內容 ('ctl00_m_g_66e41117_8ff5_4650_bf4d_7a4a25e326f3_ctl01_ReportViewer1_ctl04')。control.HideActiveDropDown();“ data-report-param =” 1068“ Interactivedeviceinfos =”(Collection)“>
('ctl00_m_g_9d6a6c3c_11d0_4e03_bbd2_b907172c437d_ctl01_ReportViewer1_ctl04')。control.HideActiveDropDown();“ data-report-param =” 1068“ Interactivedeviceinfos =”(Collection)“>
關鍵是從后面的代碼傳遞自定義數據屬性,然后按如下所示的@popnoodles在$ .cache中訪問它,並將reportviewer的clientID傳遞到javascript函數中,以獲取webpart子控件的當前實例。
<input type="hidden" id="<%= ASP_SSRS.ClientID %>_myDataState"
onchange="compareUnitValues(this.id, this.parentNode.id, '<%= ReportViewer1.ClientID %>', '<%= ASP_SSRS.ClientID %>', '<%= btnSendHiddenField.ClientID %>');" />
<script type ="text/javascript">
function compareUnitValues(elemt, parent, reportviewerID, value1, value2) {
var myDataUnit = $("#" + elemt),
parentObject = $("#" + parent),
reportviewerObject = $("#" + reportviewerID),
ssrs = $("#" + value1),
btnSend = $("#" + value2);
var myDataUnitValue = myDataUnit.val();
var myDataUnitJSON = jQuery.parseJSON(myDataUnitValue);
var currentmyDataUnit = myDataUnitJSON.currentUnit.objectId;
var sessioncurrentObjectId = document.getElementById('<%= hiddenCurrentObjectId.ClientID %>').value;
ssrs.val(myDataUnitValue);
var currentReportViewerParam = $("#" + reportviewerID).attr("data-report-param");
if (currentmyDataUnit != currentReportViewerParam) {
btnSend.trigger("click");
}
}
ReportViewer1.Attributes.Add("data-report-param", parsedObjectId)
getAttribute
僅會為您提供生成或修改的HTML中的值,而不是 DOM中的值。 數據方法從不更新HTML。
jQuery創建一個空對象$.cache
,該對象用於存儲通過data方法設置的值。 您向數據添加的每個DOM元素都分配了一個唯一ID,該ID用作$.cache
對象中的鍵。
設置
$('#ReportViewer1').data('report-param', 1234);
入門
var id = $('#ReportViewer1').data('report-param');
如果可以使用jquery,為什么不這樣做:
$("#reportviewer1").data('report-param');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.