2009-09-15 50 views
1

我有一个用户控件内的中继器。页面上的用户控件shoppingcart.aspx.I想要从javascript中获取所有lblPrice .aspx。如何访问所有这些标签。中继器内的访问标签在asp.net页面内的用户控件内的ItemTemplate

<asp:Repeater ID="rptShoppingCart" runat="server"> 
    <HeaderTemplate> 
     <tr class="big_header_style"> 
      <td> 
       Product(s) 
      </td> 
      <td> 
       Description</td> 
      <td> 
       Quantity</td> 
      <td> 
       Price (INR)</td> 
      <td> 
       Total (INR)</td> 
      <td> 
       Remove?</td> 
     </tr> 
    </HeaderTemplate> 
    <ItemTemplate> 
     <tr class="dg_item_style"> 
      <td align="center"> 
       <img src='<%# Page.ResolveUrl(Convert.ToString(DataBinder.Eval(Container.DataItem,"ProductInfo.thumbnailPath1")))%>' 
        width="90" height="90" /></td> 
      <td> 
       <asp:Label ID="lblProductName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"ProductInfo.productName") %>'></asp:Label></td> 
      <td align="center"> 
       <input id="proQuantity" runat="server" type="text" size="1" value='<%#Eval("Quantity") %>' /></td> 
      <td align="center"> 
       <strong class="redtxt"> 
        <asp:Label ID="lblPrice" runat="server" Text='<%#GetPrice((BAL.ShoppingCartMaster)Container.DataItem)%>' /></strong></td> 
      <td align="center"> 
       <strong class="redtxt"> 
        <asp:Label ID="lblTotal" runat="server" Text='<%#calculatePrice((BAL.ShoppingCartMaster)Container.DataItem)%>'></asp:Label></strong> 
      </td>                       
      <td align="center"> 
       <asp:CheckBox runat="server" ID="cbRemoveFromCart" /> 
       <asp:Label id="lblShoppingCartID" runat="server" visible="false" text='<%#Eval("ShoppingCartID") %>'></asp:Label> 

      </td> 
     </tr> 
    </ItemTemplate> 
</asp:Repeater> 

回答

-1

,如果你使用jQuery这将是对你非常容易。请查看以下示例以访问Master Page上的asp.net控件。您可以查看原始文章here.

您还可以在Java脚本中使用控件的ClientId属性。

document.getElementById("<%=txtFirstName.ClientID %>"); 

但随着上述方法的问题是你必须做的JavaScript内联,它不能是外部。

MasterPage 

<%@ Master Language="C#" AutoEventWireup="true" 
CodeFile="MasterPage.master.cs" Inherits="MasterPage" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>Access Control using jQuery</title> 
    <asp:ContentPlaceHolder id="head" runat="server"> 
    </asp:ContentPlaceHolder> 

    <script src="Scripts/jquery-1.3.2.js" 
    type="text/javascript"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $("input[id$='_txtName']").val("Text Added using jQuery"); 
     }); 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
    <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"> 

    </asp:ContentPlaceHolder> 
    </div> 
    </form> 
</body> 
</html> 




ContentPage 

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" 

AutoEventWireup="true" CodeFile="MasterPage.aspx.cs" Inherits="MasterPage"%> 

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> 

</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" 

Runat="Server"> 

<asp:TextBox ID="txtName" runat="server"></asp:TextBox> 

</asp:Content> 
+0

对不起,不使用jQuery和不使用母版页。 – Rohit 2009-09-15 09:20:48

+0

然后你可以使用ClientId属性。 – Mahin 2009-09-15 09:42:07

+0

不,您不能在Repeater内部使用ClientID作为控件。 – 2009-12-01 08:31:46

1

首先你把你的用户控件的clientID的:

var userControlId = '<%= ShoppingCart1.ClientID %>'; 

然后,您可以建立在该转发器的clientID的:

var repeaterId = userControlId + '_rptShoppingCart'; 

然后你就可以访问所有中继器内的标签lblPrice,如下所示:

for (i = 0; i < 500; i++) { 
    var rowNo = i; 
    if (rowNo < 10) 
     rowNo = "0" + rowNo; 

    var lblPrice = document.getElementById(repeaterId + '_ctl' + rowNo + '_lblPrice'); 

    if (lblPrice != undefined) 
     lblPrice.innerHTML = i * 10; 
} 

它看起来有点冒头,实际上有点不好意思,但它确实有用,如果你没有别的选择,它肯定会有帮助。而且,如果你有很多元素,速度真的很快。

“500”可以变得更好,但它更代码,我想保持答案的简短。

相关问题