2013-04-01 43 views
0

Iam在asp.net中使用jquery如何獲得jQuery中的動態跨度ID?

我有一個用戶控件,其中我有div和div表和tr表tr,tr tr和td我有lables。

ASCX:

  <div ID="Container" runat="server" class="dvContainer"> 
<table ID="Table" class = "Tablecs"> 
    <thead><tr> 
     <td colspan="2"> 
      <asp:Label ID="lbl1" Text="Swiss" runat="server" /></td> 
    </tr> 
     </thead> 
    <tr> 
     <td>ABC</td> 
     <td>DEF</td> 
    </tr> 
    <tr> 
     <td><asp:Label ID="lblPA" Text="SUN 12/21 05:04" runat="server" /></td> 
     <td ><asp:Label ID="lblPD" Text="SUN 12/21 19:00" runat="server" /></td> 
    </tr> 
     <tr> 
     <td><asp:Label ID="lblAA" Text="SUN 12/21 05:04" runat="server" /></td> 
     <td ><asp:Label ID="lblAD" Text="SUN 12/21 19:00" runat="server" /></td> 
    </tr> 
</table>  

我想要動態地將數據綁定到這些用戶控制。即將數據綁定到用戶控件中的標籤。

我的Jquery

  $div.find(".Table").text(oPorts[iCount].Name); // my result is an array of oPorts and i have filed as Name 

但這不是工作的罰款。 當我檢查到動態代碼它爲每個標籤生成SPAN 如何在循環中找到SPAN ID dynamiccaly並將數據綁定到標籤?

任何幫助表示讚賞。提前致謝。

+0

如果您有任何獨特這是一個從數據庫返回,將它添加到您的跨度的id屬性。 – Manoj

+0

但我沒有任何跨度在我的頁面,我應該創建任何tr或td的一個跨度? –

+0

asp:標籤被渲染爲跨度。您在usercontrol中的所有元素(包括標籤)都是runat =「server」,這意味着它們的id將在運行時修改。當你運行你的頁面時,轉到viewsource並尋找分配給你的標籤的id模式,即跨度,即ucId_ctrl_lblPA_1,ucId_ctrl_lblPA_2,然後你可以通過這樣的標識循環 –

回答

1

假設你有一個標記一個用戶控件等給出如下

<%@ Control Language="C#" AutoEventWireup="true" 
CodeBehind="WebUserControl1.ascx.cs" Inherits="DOTNET_FORMS.WebUserControl1" %> 

<div id="Container" runat="server" class="dvContainer"> 
    <table id="Table" class="Tablecs"> 
     <tr> 
      <td> 
       <asp:Label ID="lblPA" Text="SUN 12/21 05:04" runat="server" /> 
      </td> 
      <td> 
       <asp:Label ID="lblPD" Text="SUN 12/21 19:00" runat="server" /> 
      </td> 
     </tr> 
    </table> 
</div> 

並且您已經註冊了該用戶控件您.aspx頁面上是這樣的:

<%@ Register TagPrefix="UC" TagName="Test" Src="~/WebUserControl1.ascx" %> 

,並用它這樣的:

<UC:Test ID="uc1" runat="server" /> 

然後,當你運行你的頁面時,你的元素得到渲染omething像

<div id="uc1_Container" class="dvContainer"> 
    <table id="Table" class="Tablecs"> 
     <tr> 
      <td> 
       <span id="uc1_lblPA">SUN 12/21 05:04</span> 
      </td> 
      <td> 
       <span id="uc1_lblPD">SUN 12/21 19:00</span> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <span id="uc1_lblAA">SUN 12/21 05:04</span> 
      </td> 
      <td> 
       <span id="uc1_lblAD">SUN 12/21 19:00</span> 
      </td> 
     </tr> 
    </table> 
</div> 

查看標籤和其他元素(與runat="server"屬性的元素)的IDS如何得到即改變

lblPA > uc1_lblPA 
lblPD > uc1_lblPD 

Container > uc1_Container 

如此,你也看出來的這些變化,因爲只有這樣你才能使用jQuery抓住這些元素,因爲jQuery是一種客戶端語言,它在服務器端代碼(runat =「server」)執行後執行。

但是,如果你不想看出來的修改ID,你可以做以下

  1. 刪除runat="server"屬性,並確保您的ID是唯一的,所有的人都
  2. runat="server"屬性是他們的,在你的所有服務器端控件上放置一個屬性clientidmode="static"。和Ids不會改變。
  3. 使用ClientId即在你的jQuery選擇,抓住這樣一個元素:$('#"+'<%= lblPA.ClientID %>');

現在,因爲你的ID都是唯一的,你不需要找,直接抓住這樣的元素:

$('#lblPA').text(); 

,或者您通過與Tablecs類的表的所有TDS要循環,這樣做:

$('.Tablecs tr td').each(function(index, item){ 
      alert($(item).text()); 
}); 
+0

非常感謝...,謝謝你的乾淨的解釋..謝謝,這將幫助我很多 –