---
title: "How to access label control placed in asp datalist footer template using c#"  
description: "How to access label control placed in asp datalist footer template using c#"  
author: "Rashmi S"  
published: 2014-11-16  
updated: 2014-11-16  
canonical: https://www.mindstick.com/forum/12597/how-to-access-label-control-placed-in-asp-datalist-footer-template-using-c-sharp  
category: ".net"  
tags: ["asp.net"]  
reading_time: 5 minutes  

---

# How to access label control placed in asp datalist footer template using c#

Hi... using datalist im adding marks of 6 subjects to a [database table](https://www.mindstick.com/forum/159366/how-to-test-regular-expressions-in-sql-without-using-database-table) on [button click](https://www.mindstick.com/forum/790/form-gets-submiited-twice-on-button-click). i want to display total of all marks entered in textbox [placed inside](https://www.mindstick.com/forum/12672/binding-all-college-names-to-a-college-dropdownlist-based-on-data-selected-in-another-city-dropdownlist-both-dropdownlists-are-placed-inside-the-datalist) item template of datalist on button click. I used [label control](https://www.mindstick.com/articles/301/a-label-control-in-vb-dot-net) in footer template of datalist, on that label i want to display total marks. im [getting error](https://www.mindstick.com/forum/159638/getting-error-microsoft-office-interop-outlook-doesn-t-work) as "Object reference not set to an [instance of an object](https://www.mindstick.com/forum/376/system-nullreferenceexception-object-reference-not-set-to-an-instance-of-an-object)".\
My code is :<%@ Page Title="" Language="C#" MasterPageFile="~/SSLC_Board/SSLCMaster.Master" AutoEventWireup="true" CodeBehind="ManageMarks.aspx.cs" Inherits="PUseatAllot.SSLC_Board.ManageMarks" %><asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> <table style="width:100%;"> <tr> <td align="center"> <strong style="font-size: medium">MANAGE MARKS</strong></td> </tr> <tr> <td> <hr size=3 color=#DA6BB6/></td> </tr> <tr> <td> &nbsp;</td> </tr> <tr> <td align="center"> <table style="width: 85%;"> <tr> <td> School Name</td> <td> <asp:DropDownList ID="DDLschool" runat="server" Width="300px" AutoPostBack="True" onselectedindexchanged="DDLschool_SelectedIndexChanged"> </asp:DropDownList> </td> <td> &nbsp;</td> </tr> </table> </td> </tr> <tr> <td> </td> </tr> <tr> <td> <table style="width: 100%"> <tr> <td style="width: 113px" valign="top"> <asp:Label ID="lblMsg" runat="server" Font-Bold="True" style=" background-color:#FFFFCC; color:Red" Width="200px"></asp:Label> <br /><br /> <asp:GridView ID="GVstudents" runat="server" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" AutoGenerateColumns="False" Width="138px"> <Columns> <asp:TemplateField HeaderText="[Student Name](https://www.mindstick.com/forum/157178/how-to-filter-1000-of-student-name-in-php-in-ascending-order)"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:LinkButton ID="LBstudent" runat="server" CommandArgument='<%# Eval("StudentId") %>' onclick="LBstudent_Click" Text='<%# Eval("StudentName") %>'></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /> <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" /> <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /> <RowStyle BackColor="White" ForeColor="#330099" /> <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /> <SortedAscendingCellStyle BackColor="#FEFCEB" /> <SortedAscendingHeaderStyle BackColor="#AF0101" /> <SortedDescendingCellStyle BackColor="#F6F0C0" /> <SortedDescendingHeaderStyle BackColor="#7E0000" /> </asp:GridView> </td> <td valign="top"> <asp:Panel ID="Panel2" runat="server"> <table style="width: 100%"> <tr> <td> <strong style="font-size: large">Subjects</strong></td> <td> &nbsp;</td> </tr> <tr> <td> <hr size=3px color="#DA6BB6"/> </td> <td> &nbsp;</td> </tr> <tr> <td> <asp:CheckBoxList ID="CBLsubjects" runat="server"> </asp:CheckBoxList> <asp:Label ID="lblMsg2" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label> <br /> </td> <td> &nbsp;</td> </tr> <tr> <td> <asp:Button ID="btnAddMarks" runat="server" Text="Add Marks" onclick="btnAddMarks_Click" /> </td> <td> &nbsp;</td> </tr> </table> </asp:Panel> </td> </tr> <tr> <td style="width: 113px"> &nbsp;</td> <td valign="top"> <asp:Panel ID="Panel1" runat="server" BorderColor="#DA6BB6" BorderWidth="2px"> <table style="width:100%;"> <tr> <td style="width: 20px"> <asp:DataList ID="DLmarks" runat="server" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" GridLines="Both" Width="350px" > <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /> <FooterTemplate> TOTAL MARKS&nbsp;&nbsp;&nbsp;&nbsp; :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:Label ID="lblTotalMarks" runat="server" Text=""></asp:Label> </FooterTemplate> <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" /> <HeaderTemplate> MARKS BOARD </HeaderTemplate> <ItemStyle BackColor="White" ForeColor="#330099" /> <ItemTemplate> <table style="width:100%;"> <tr> <td align="left" style="width: 112px"> <asp:Label ID="lblSubjectId" runat="server" Text='<%# Eval("SubjectId") %>' Visible="False"></asp:Label> &nbsp&nbsp&nbsp <asp:Label ID="lblSubjectName" runat="server" Text='<%# Eval("SubjectName") %>'></asp:Label> </td> <td align="left"> <asp:TextBox ID="txtMarks" runat="server"></asp:TextBox> </td> <td> </td> </tr> </table> </ItemTemplate> <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /> </asp:DataList> </td> </tr> <tr> <td style="width: 20px"> &nbsp;</td> </tr> <tr> <td style="width: 20px" align="center"> <asp:ImageButton ID="ImgBtnSubmit" runat="server" Height="33px" ImageUrl="~/IMG/submit1.jpg" onclick="ImgBtnSubmit_Click" /> </td> </tr> </table> </asp:Panel> </td> </tr> </table> </td> </tr> <tr> <td> &nbsp;</td> </tr> </table>\
\
</asp:Content>\
aspx.cs code:using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;\
namespace PUseatAllot.SSLC_Board{ public [partial class](https://www.mindstick.com/forum/155717/define-partial-class-in-mvc) ManageMarks : System.Web.UI.Page { BL b = new BL(); static int Mid; static int StudId; static Label label4; //static string[] StudId2; protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { //label4 = DLmarks.Controls[DLmarks.Controls.Count - 1].FindControl("lblTotalMarks") as Label;\
Panel2.Visible = false; Panel1.Visible = false;\
DDLschool.DataSource = b.School_GetAll(); DDLschool.DataTextField = "SchoolName"; DDLschool.DataValueField = "SchoolId"; DDLschool.DataBind(); DDLschool.Items.Insert(0, "- select -"); } }\
protected void DDLschool_SelectedIndexChanged(object sender, EventArgs e) { if (DDLschool.SelectedIndex != 0) { DataTable t_stud = b.Student_Get_SchlId(int.Parse(DDLschool.SelectedItem.Value)); if (t_stud.Rows.Count > 0) { GVstudents.EmptyDataText = "No Record."; GVstudents.DataSource = t_stud; GVstudents.DataBind(); } else { lblMsg.Text = "-- No Record --"; } } }\
protected void LBstudent_Click(object sender, EventArgs e) { StudId = int.Parse(((LinkButton)sender).CommandArgument);\
Panel2.Visible = true;\
//binding subjects to checkboxlist DataTable t_sub = b.Subject_GetAll(); if (t_sub.Rows.Count > 0) { CBLsubjects.DataSource = t_sub; CBLsubjects.DataTextField = "SubjectName"; CBLsubjects.DataValueField = "SubjectId"; CBLsubjects.DataBind(); } else { lblMsg2.Text = "No Subjects. Please Add Subject details and then proceed."; } }\
protected void btnAddMarks_Click(object sender, EventArgs e) { DataTable t_Marks = new DataTable(); t_Marks.Columns.Add("SubjectId",typeof(int)); t_Marks.Columns.Add("SubjectName",typeof(string));\
string subId = null; for (int i = 0; i < CBLsubjects.Items.Count; i++) { if (CBLsubjects.Items[i].Selected) subId = subId+CBLsubjects.Items[i].Value+"_"+CBLsubjects.Items[i].Text + ","; }\
if (subId != null) { //creating [dynamic data](https://www.mindstick.com/forum/2180/dynamic-data-in-repeater) table and storing selected subject details subId = subId.Substring(0, subId.Length - 1); string [] StudId2 = subId.Split(',');\
// checking where 6 subjects are selected or not if (StudId2.Length == 6) { for (int i = 0; i < StudId2.Length; i++) { string[] v = StudId2[i].Split('_');\
DataRow dt = t_Marks.NewRow(); dt["SubjectId"] = int.Parse(v[0]); dt["SubjectName"] = v[1]; t_Marks.Rows.Add(dt); } //--------------------------------------------------------------\
//binding selected subject details to datalist Panel1.Visible = true; DLmarks.DataSource = t_Marks; DLmarks.DataBind(); //-------------------------------------------------------------- } else { Response.Write("<script>alert('Please select 6 subjects.')</script>"); Panel1.Visible = false; } } else Response.Write("<script>alert('Please select subjects.')</script>"); }\
static int totalMarks;\
protected void ImgBtnSubmit_Click(object sender, ImageClickEventArgs e) { foreach (DataListItem item in DLmarks.Items) { Label lblSubId= (Label)item.FindControl("lblSubjectId"); Label lblSubName = (Label)item.FindControl("lblSubjectName"); TextBox txtMarks = (TextBox)item.FindControl("txtMarks"); Label LbTotalMarks = (Label)item.FindControl("lblTotalMarks");\
if (b.Marks_Add(StudId, int.Parse(lblSubId.Text), int.Parse(txtMarks.Text)) == 1) { totalMarks += int.Parse(txtMarks.Text); LbTotalMarks.Text = totalMarks.ToString(); Response.Write("<script>alert('Marks added successfully.')</script>"); } else Response.Write("<script>alert('ERROR in adding marks.')</script>");\
//if (item.ItemType == ListItemType.Footer) //{ // Label LbTotalMarks = (Label)item.FindControl("lblTotalMarks"); // LbTotalMarks.Text = totalMarks.ToString(); //} } // label4.Text = totalMarks.ToString(); }\
//protected void DLmarks_ItemDataBound(object sender, DataListItemEventArgs e) //{ // //when the item is a footer item (only happens once) [put your](https://answers.mindstick.com/qa/50309/how-to-put-your-iphone-into-recovery-mode) global into a control // if (e.Item.ItemType == ListItemType.Footer) // { // Label LbTotalMarks = (Label)e.Item.FindControl("lblTotalMarks"); // LbTotalMarks.Text = totalMarks.ToString(); // } //} }}\
Please help me......!


---

Original Source: https://www.mindstick.com/forum/12597/how-to-access-label-control-placed-in-asp-datalist-footer-template-using-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
