2011-05-13 84 views
0

我有一種情況,我有一些在代碼後面動態創建的對象。每個對象都有一些隱藏的字段和一個圖像按鈕。只保留客戶端控件ID

當圖像按鈕翻轉時,我有一個js文件,應該得到該對象的相關領域,並做一些東西。

我遇到的麻煩是,當我滾動按鈕時,我得到一個null引用,因爲getEmelementById不知道我正在引用哪個對象。

這裏是ASCX頁:

<div style="position:relative;float:right;" visible="true"> 
    <asp:ImageButton ID="iconNarrative" visible="true" ImageUrl="/_layouts/images/osys/NarrativeIcon.gif" runat="server" Style="position:absolute; top:-28px; left:-25px; display:block;" onmouseout="hideNarrative()" onmouseover="showNarrative(this, document.getElementById(id))" /> 
    <asp:HiddenField ID="hfNarrative" runat="server" Visible="true" />   
</div>  

下面是它的外觀渲染時。在這種情況下有2個:

 <div style="position:relative;float:right;" visible="true"> 
    <input type="image" name="ctl00$m$g_90cae966_b430_4d11_8992_816553676250$ctl00$iconNarrative" id="ctl00_m_g_90cae966_b430_4d11_8992_816553676250_ctl00_iconNarrative" onmouseout="hideNarrative()" onmouseover="showNarrative(this, document.getElementById(id))" src="/_layouts/images/osys/NarrativeIcon.gif" style="border-width:0px;position:absolute; top:-28px; left:-25px; display:block;" /> 
    <input type="hidden" name="ctl00$m$g_90cae966_b430_4d11_8992_816553676250$ctl00$hfNarrative" id="ctl00_m_g_90cae966_b430_4d11_8992_816553676250_ctl00_hfNarrative" />   
    <input type="hidden" name="ctl00$m$g_90cae966_b430_4d11_8992_816553676250$ctl00$hfNarrativeDate" id="ctl00_m_g_90cae966_b430_4d11_8992_816553676250_ctl00_hfNarrativeDate" /> 
    <input type="hidden" name="ctl00$m$g_90cae966_b430_4d11_8992_816553676250$ctl00$hfNarrativeBy" id="ctl00_m_g_90cae966_b430_4d11_8992_816553676250_ctl00_hfNarrativeBy" /> 
</div> 

這裏是我的javascript

var narrativeContainer = document.getElementById('narrative_container'); 
var narrative = document.getElementById('<%=hfNarrative.ClientID%>'); 
var narrativeBy = document.getElementById('<%=hfNarrativeBy.ClientID%>'); 
var narrativeDate = document.getElementById('<%=hfNarrativeDate.ClientID%>'); 

narrative = document.getElementById('<%=hfNarrative.ClientID%>'); 

那我怎麼現在它設置隱藏字段的地方叫什麼名字? 我唯一能想到的就是獲取Id並將其添加到各個字段中,然後使用getElementById?

+1

只是一些有用的提示:1)不要使用內聯樣式。也許這只是說明,但它似乎與你的問題無關,所以要掩蓋你的恥辱。:) 2)你發佈到'jquery',所以我假設你熟悉不顯眼的JavaScript。但是,你似乎根本沒有使用它。你真的使用jQuery庫,或只是JavaScript?最好的答案取決於你要走哪條路線(無論如何,就代碼細節而言)。 – 2011-05-13 14:26:12

+0

當你的控件沒有設置爲runat =「server」時,呵呵,'visible =「true」'什麼也不做。 – 2011-05-13 14:27:21

+0

不錯的打字 - 'getEmelementById'不存在。嘗試'getElephantById'來代替;)但是,真正的@Adam Terlson說。如果您只是想在展開另一個元素時顯示某些元素,那麼jQuery就非常簡單。 – Town 2011-05-13 14:37:39

回答

1

這是我的通用建議。我假設你也使用jQuery。

首先,如果您打算從JavaScript訪問服務器端控件時設置ClientIDs' mode to static,它總是很有幫助。

二,爲什麼你的數據被傳遞到隱藏字段?它被修改並傳回?如果不是,則用於存儲數據的HTML5標準具有格式爲data-attributeName="value"的屬性。

下面是你的.ASCX頁面的HTML:

<div id="hfNarrative" class="narratives" runat="server"> 
    <!-- Stuff specific to this object, image, buttons, whatever --> 
</div> 

在.NET方面,您可以撥打:

hfNarrative.Attributes.Add("data-narrativeDate", SOME_DATE); 
hfNarrative.Attributes.Add("data-narrativeBy", SOME_GUY); 

然後,使用jQuery你可以訪問這些簡單:

$('div.narratives').each(function() { 
    var $this = $(this); 
    var narrBy = $this.attr('data-narrativeBy'); 
    var narrDat = $this.attr('data-narrativeDate'); 
    //Do stuff here with the data. 
}); 

我希望這會幫助你走上一條更乾淨的道路。

+0

這看起來不錯。謝謝。 – hoakey 2011-05-16 19:56:27

0

使用jQuery:

$('input[type=image]).hover(function() { 
    var myElemID = $(this).attr('id'), inputJSON = {}; 
    $(this).parent().children('input').each(function(i) { 
    inputJSON[$(this).attr('name')] = $(this).attr('id'); 
    }); 
}); 

這會給你一個變量myElemID與圖像輸入的ID,並inputJSON = {'ctl00$m$g_90cae966_b430_4d11_8992_816553676250$ctl00$hfNarrativeBy':'ctl00_m_g_90cae966_b430_4d11_8992_816553676250_ctl00_hfNarrativeBy'}

JSON數組它必須雖然說,這些名稱和標識的很糟糕。如果可能的話,你應該讓它們更具語境,語義和相關性。