2012-05-07 48 views
2

我想在我的ASP.NET網站來計算學生的總加權平均(GWA)asp.net ListView,在腳註中顯示計算得出的摘要?

我的公式是(在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"); 
} 

回答

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(); 
} 
相關問題