views:

1850

answers:

2

I have a repeater inside a user control.User control in on page shoppingcart.aspx.I want to access all lblPrice from javascript from a function inside shoppingcart.aspx.How to access all of these labels.

<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>
A: 

It would be very easy for you if you are using jquery. Please check the below example for accessing asp.net controls on Master Page. You can check original article here.

You can also use ClientId property of control in Java script.

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

But the problem with above approach is you have to make the Javascript inline and it can not be external.

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"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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>
Mahin
sorry not using jquery and not using master page.
Rohit
then you can use ClientId property.
Mahin
No, you can't use ClientID for controls inside a Repeater.
Dan Dumitru
A: 

First you take the clientID of your user control:

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

Then, you can build on that the clientID of the repeater:

var repeaterId = userControlId + '_rptShoppingCart';

And then you can access all the labels lblPrice inside the repeater, like this:

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;
}

It looks a bit hacky, and it actually is a bit hacky, but it does the thing and, if you have no other option, it sure does help. And also, it's really fast, if you have a lot of elements.

That "for" going to 500 can be made nicer, but it's more code and I wanted to keep the answer short.

Dan Dumitru