asp.net ListView,在页脚中显示计算摘要?

时间:2012-05-07 06:19:56

标签: asp.net listview summary

我正在尝试在我的ASP.NET网站中计算学生的通用加权平均值(GWA)

我的公式是(在SQL中)

ROUND(SUM((UnitsAcademic*Grade))/SUM(UnitsAcademic),3)

到目前为止,这是我的ASP.NET。我想用ASP.NET计算GWA。现在我有点卡住了。

ASPX

<asp:ListView OnItemDataBound="listviewGrade_ItemDataBound" ID="listviewGrade" runat="server" DataSourceID="sqlGrade" DataKeyNames="GradebookDetailId">
                    <LayoutTemplate>
                                <table class="Container8" >
                                    <tbody>
                                    <tr>
                                        <th>Subject Code</th>
                                        <th>Description</th>
                                        <th>Units</th>
                                        <th>Grade</th>
                                        <th>Remarks</th>
                                        <th>Faculty Name</th>
                                    </tr>
                                    <tr id="itemPlaceholder" runat="server" />
                                    </tbody>
                                </table>

                            </LayoutTemplate>
                            <ItemTemplate>
                                <tr>
                                    <td><%# Eval("SubjectCode") %></td>
                                    <td><%# Eval("Description") %></td>
                                    <td><asp:Label ID="lblUnitsAcademic" runat="server" Text='<%# Eval("UnitsAcademic") %>' /></td>
                                    <td><asp:Label ID="lblGrade" runat="server" Text='<%# Eval("Grade") %>' /></td>
                                    <td><%# Eval("Remarks") %></td>
                                    <td><%# Eval("FacultyName") %></td>
                                </tr>
                            </ItemTemplate>

                        </asp:ListView>
<br/>
<asp:Label ID="lblGWA" runat="server" />

C#

protected void listviewGrade_ItemDataBound(object sender, ListViewItemEventArgs e)
{   
    Label lblGrade = (Label)e.Item.FindControl("lblGrade");
    Label lblUnitsAcademic = (Label)e.Item.FindControl("lblUnitsAcademic");
}

1 个答案:

答案 0 :(得分:4)

试试这个:
ASPX:

<asp:ListView OnItemDataBound="listviewGrade_ItemDataBound" ID="listviewGrade" runat="server"
        DataSourceID="sqlGrade" DataKeyNames="GradebookDetailId">
        <LayoutTemplate>
            <table class="Container8">
                <tbody>
                    <tr>
                        <th>
                            Subject Code
                        </th>
                        <th>
                            Description
                        </th>
                        <th>
                            Units
                        </th>
                        <th>
                            Grade
                        </th>
                        <th>
                            Remarks
                        </th>
                        <th>
                            Faculty Name
                        </th>
                    </tr>
                    <tr id="itemPlaceholder" runat="server" />
                </tbody>
                <tfoot>
                    <asp:Label ID="lblSum" runat="server" Text="Label"></asp:Label>
                </tfoot>
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <%# Eval("SubjectCode") %>
                </td>
                <td>
                    <%# Eval("Description") %>
                </td>
                <td>
                    <asp:Label ID="lblUnitsAcademic" runat="server" Text='<%# Eval("UnitsAcademic") %>' />
                </td>
                <td>
                    <asp:Label ID="lblGrade" runat="server" Text='<%# Eval("Grade") %>' />
                </td>
                <td>
                    <%# Eval("Remarks") %>
                </td>
                <td>
                    <%# Eval("FacultyName") %>
                </td>
            </tr>
        </ItemTemplate>
    </asp:ListView>

CS:

int sum = 0;
protected void listviewGrade_ItemDataBound(object sender, ListViewItemEventArgs e)
{       
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        Label lblGrade = (Label)e.Item.FindControl("lblGrade");
        Label lblUnitsAcademic = (Label)e.Item.FindControl("lblUnitsAcademic");
        sum += Convert.ToInt32(lblUnitsAcademic.Text) *
            Convert.ToInt32(lblGrade.Text) / Convert.ToInt32(lblUnitsAcademic.Text);

    }       
}



protected void listviewGrade_LayoutCreated(object sender, EventArgs e)
{

    Label lblSum = (Label)listviewGrade.FindControl("lblSum");
    lblSum.Text = sum.ToString();
}