locked
How to reference to label value inside the list view? RRS feed

  • Question

  • User521171331 posted

    I have a label (lblTotalFee) in my list view which calculates the total amount. I got it works for that.

    Now, in the same page, I have a textbox (txtFinalTotal) which need to do some calculation based on the label value inside the list view. How do I do that?

    My code below got syntax error

            $('#<%=txtFinalTotal.ClientID%>').val($('#<%=lblTotalFee.ClientID%>').val());

    Sunday, July 5, 2015 9:17 AM

Answers

  • User61956409 posted

    Hi ngaisteve1,

    Please refer to the following sample.

    <asp:ListView ID="ListView1" runat="server">
        <LayoutTemplate>
            <table runat="server">
                <tr runat="server">
                    <td runat="server">
                        <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                            <tr runat="server" style="">
                                <th runat="server">Id</th>
                                <th runat="server">Values</th>
                            </tr>
                            <tr id="itemPlaceholder" runat="server">
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr runat="server">
                    <td runat="server" style=""></td>
                </tr>
                <tr>
                    <td>Total:<asp:Label ID="lblTotalFee" ClientIDMode="Static" runat="server" Text=""></asp:Label></td>
                    <td>
                        <asp:TextBox ID="txtFinalTotal" ClientIDMode="Static" runat="server"></asp:TextBox></td>
                </tr>
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr style="">
                <td>
                    <asp:Label ID="IdLabel" runat="server" Text='<%# Bind("Id") %>' />
                </td>
                <td class="tdvalue">
                    <asp:Label ID="ValuesLabel" CssClass="lblval" runat="server" Text='<%# Bind("Values") %>' />
                </td>
            </tr>
        </ItemTemplate>
    </asp:ListView> 
    
    <script>
        $(function () {
            var total = 0;
            $(".lblval").each(function () {
                total += parseInt($(this).text());
            });
            //alert(total);
            $('#lblTotalFee').text(total);
            $('#txtFinalTotal').val(total);
        })
    </script> 
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id");
            dt.Columns.Add("Values");
    
            dt.Rows.Add("1", 10);
            dt.Rows.Add("2", 23);
    
            ListView1.DataSource = dt;
            ListView1.DataBind();
        }
    } 
    

    Best Regards,

    Fei Han




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 5, 2015 10:10 PM

All replies

  • User61956409 posted

    Hi ngaisteve1,

    Please refer to the following sample.

    <asp:ListView ID="ListView1" runat="server">
        <LayoutTemplate>
            <table runat="server">
                <tr runat="server">
                    <td runat="server">
                        <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                            <tr runat="server" style="">
                                <th runat="server">Id</th>
                                <th runat="server">Values</th>
                            </tr>
                            <tr id="itemPlaceholder" runat="server">
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr runat="server">
                    <td runat="server" style=""></td>
                </tr>
                <tr>
                    <td>Total:<asp:Label ID="lblTotalFee" ClientIDMode="Static" runat="server" Text=""></asp:Label></td>
                    <td>
                        <asp:TextBox ID="txtFinalTotal" ClientIDMode="Static" runat="server"></asp:TextBox></td>
                </tr>
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr style="">
                <td>
                    <asp:Label ID="IdLabel" runat="server" Text='<%# Bind("Id") %>' />
                </td>
                <td class="tdvalue">
                    <asp:Label ID="ValuesLabel" CssClass="lblval" runat="server" Text='<%# Bind("Values") %>' />
                </td>
            </tr>
        </ItemTemplate>
    </asp:ListView> 
    
    <script>
        $(function () {
            var total = 0;
            $(".lblval").each(function () {
                total += parseInt($(this).text());
            });
            //alert(total);
            $('#lblTotalFee').text(total);
            $('#txtFinalTotal').val(total);
        })
    </script> 
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id");
            dt.Columns.Add("Values");
    
            dt.Rows.Add("1", 10);
            dt.Rows.Add("2", 23);
    
            ListView1.DataSource = dt;
            ListView1.DataBind();
        }
    } 
    

    Best Regards,

    Fei Han




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 5, 2015 10:10 PM
  • User521171331 posted

    it works well. thanks a lot :)

    Wednesday, July 8, 2015 4:24 AM