I have a gridview and two buttons Save and Next. On Save Button Click I am able to save each row of GridView and on Next Button I am able to call GridView_PageIndexChanging method but the problem is as under:-
On last Page index reached I am not able to dispaly a message like "You have"
//Design of Gridview is
<asp:GridView ID="gvStudents" runat="server" AutoGenerateColumns="False" AllowPaging="True"
OnPageIndexChanging="gvStudents_PageIndexChanging" OnPageIndexChanged="gvStudents_PageIndexChanged"
OnRowDataBound="gvStudents_RowDataBound" DataKeyNames="staff_id" EnablePersistedSelection="True"
EmptyDataText="<p>You Have no Data</p>">
<Columns>
<asp:TemplateField HeaderText="Staff_id">
<ItemTemplate>
<asp:Label ID="lblstaffid" runat="server" Text='<%#Eval("staff_id") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblstaffname" runat="server" Text='<%#Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Father's Name">
<ItemTemplate>
<asp:Label ID="lblfname" runat="server" Text='<%#Eval("fname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="In Time (Optional)">
<ItemTemplate>
<asp:DropDownList ID="ddlInTime" runat="server" AutoPostBack="true">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Out Time(Optional)">
<ItemTemplate>
<asp:DropDownList ID="ddlOutTime" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Attendence">
<ItemTemplate>
<asp:DropDownList ID="AttendanceOption" runat="server">
<asp:ListItem Selected="True" Text="Present"></asp:ListItem>
<asp:ListItem Text="Absent"></asp:ListItem>
<asp:ListItem Text="Half Day Present"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remarks">
<ItemTemplate>
<asp:TextBox ID="txtremarks" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
//End of Design of Gridview
//Design for two buttons
<asp:UpdatePanel ID="PanelGridData" runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="btnSave" />
<asp:PostBackTrigger ControlID="btnNext" />
</Triggers>
</asp:UpdatePanel>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="SaveStudentAttendance"
CausesValidation="False" />
<asp:Button ID="btnNext" runat="server" Text="Next" CausesValidation="False" OnClick="btnNext_Click"
Width="66px" />
//End of Design for buttons
//Code to Save each row of Gridview
foreach (GridViewRow row in gvStudents.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
string Astatus = ((row.FindControl("AttendanceOption") as DropDownList).SelectedItem.ToString());
float staffId = float.Parse((row.FindControl("lblstaffid") as Label).Text.Trim());
string Name = ((row.FindControl("lblstaffname") as Label).Text.Trim());
string FName = ((row.FindControl("lblfname") as Label).Text.Trim());
string attendenceDate = (this.txtselectDate.Text.Trim());
string Remarks = ((row.FindControl("txtremarks") as TextBox).Text.Trim());
string InTime = ((row.FindControl("ddlInTime") as DropDownList).SelectedItem.ToString());
string OutTime = ((row.FindControl("ddlOutTime") as DropDownList).SelectedItem.ToString());
string constring = System.Configuration.ConfigurationManager.ConnectionStrings["AISConnectionString"].ConnectionString;
using (SqlConnection con1 = new SqlConnection(constring))
{
string query = "INSERT INTO d_attendance(staff_id,name,fname,dept,c_date,inTime,outTime,status,remarks) VALUES('" + staffId + "','" + Name + "', '" + FName + "', '" + lblsession.Text + "','" + attendenceDate + "','" + InTime + "','" + OutTime + "','" + Astatus + "','" + Remarks + "')";
using (SqlCommand cmd = new SqlCommand(query, con1))
{
con1.Open();
cmd.ExecuteNonQuery();
//Response.Write("<script>alert('Attendance Data Saved for First Page Index. Proceed to next Page Index ')</script>");
lblnotify.Text = "Attendance Data Saved. Please Click Next";
con1.Close();
btnSave.Enabled = false;
btnNext.Enabled = true;
txtselectDate.Enabled = true;
}
}
}
}
//End of Code to save each row of grid
//Code behind Next Button
protected void btnNext_Click(object sender, EventArgs e)
{
int a = gvStudents.PageCount;
try
{
if (gvStudents.PageIndex < a)
{
gvStudents.PageIndex = gvStudents.PageIndex + 1;
lblnotify.Text = "";
PopulateStudentDetails();
btnSave.Enabled = true;
btnNext.Enabled = false;
}
}
catch (Exception ex)
{
if (gvStudents.PageIndex >= a)
{
lblnotify.Text = "You have reached end of grid";
btnNext.Enabled = false;
btnSave.Enabled = false;
}
}
}
Please help me I am not able to display message on last page of gridview.