问题描述
我有一个带分页的 GridView.当我尝试使用 gridview.rows.count 计算 gridview 行时,它仅为我提供当前页面的行数.
I have a GridView with paging. When I try to count gridview row with gridview.rows.count, It gives me row count for current page only.
无论页面索引如何,如何获取 GridView 的总行数?
How can I get total rows of GridView irrespective of page index?
//这里是.aspx页面的代码
// Here is the code of .aspx page
<%@ Page Title="Search candidates based on vacancy" Language="C#" MasterPageFile="~/HR Department/hrmasterpage.master"
AutoEventWireup="true" CodeFile="searcAppForVac.aspx.cs" Inherits="HR_Department_searcAppForVac" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"></asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <table width="100%">
<tr>
<td>
</td>
</tr>
<tr>
<td align="center" class="tdtitle">
Search Candidates
</td>
</tr>
<tr>
<td>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table width="100%">
<tr>
<td class="RowHeight" width="20%">
Select Company
</td>
<td width="30%">
<asp:DropDownList ID="companyList" runat="server" AppendDataBoundItems="true" AutoPostBack="True"
OnSelectedIndexChanged="companyList_SelectedIndexChanged" Width="150px">
<asp:ListItem Text="-Select Company-" Value="-1"></asp:ListItem>
</asp:DropDownList>
</td>
<td width="20%">
Select Department
</td>
<td width="30%">
<asp:DropDownList ID="deptList" runat="server" AppendDataBoundItems="true" AutoPostBack="True"
onclick="Validate();" OnSelectedIndexChanged="deptList_SelectedIndexChanged"
Width="150px">
<asp:ListItem Value="-1">-Select Department-</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="RowHeight" width="20%">
Select Vacancy
</td>
<td colspan="3" width="*">
<asp:DropDownList ID="vacanyList" runat="server" AppendDataBoundItems="true"
Width="200px" AutoPostBack="True"
onselectedindexchanged="vacanyList_SelectedIndexChanged">
<asp:ListItem Value="-1">-Select Vacancy-</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="4" align="center">
<asp:Label ID="notifyLbl" runat="server" Font-Size="Large" ForeColor="Red"
Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td colspan="4">
<asp:Label ID="titleLbl" runat="server" Font-Size="Large" ForeColor="Red"
Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td colspan="4">
<asp:GridView ID="appForVacGrid" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" AllowPaging="True"
onpageindexchanging="appForVacGrid_PageIndexChanging" GridLines="None"
PageSize="3">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
App.ID
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="appIdLbl" runat="server" Text='<%# Eval("AppId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
First Name
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="firstNameLbl" runat="server" Text='<%# Eval("AppFirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Last Name
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lastNameLbl" runat="server" Text='<%# Eval("AppLastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Qualification
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="qualiNameLbl" runat="server" Text='<%# Eval("QualiName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Experience
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("TotalExpYear") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
EmailId
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="emailLbl" runat="server" Text='<%# Eval("AppEmailId1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Send Mail
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="sendMailBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"
HorizontalAlign="Right" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center"
VerticalAlign="Top" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"
Font-Size="Medium" HorizontalAlign="Left" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</td>
</tr>
<tr>
<td colspan="4" align="center">
</td>
</tr>
<tr>
<td align="center" colspan="4">
<asp:Button ID="sendMailBtn" runat="server" Height="40px" Text="SEND MAIL"
Width="100px" onclick="sendMailBtn_Click" />
</td>
</tr>
<tr>
<%--<td>
</td>--%>
</tr>
</table>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="companyList" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="deptList" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="appForVacGrid" EventName="pageindexchanged" />
</Triggers>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
<script type="text/javascript">
function alertOnBadSelection() {
var select = document.getElementById('companyList');
if (select.options[select.selectedIndex].value == "-Select Company-") {
alert('Please Select Company!');
return false;
}
}
</script>
//这是我的aspx.cs页面的代码
// This is code of my aspx.cs page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
public partial class HR_Department_searcAppForVac : System.Web.UI.Page
{
DataOperation oDo = new DataOperation();
AppVacancyDetailClass objAppVacDetail = new AppVacancyDetailClass();
protected void Page_Load(object sender, EventArgs e)
{
notifyLbl.Visible = false;
titleLbl.Visible = false;
sendMailBtn.Visible = false;
try
{
if (!IsPostBack)
{
// Disable department dropdown list and vacancy dropdown list till company is not selected.
deptList.Enabled = false;
vacanyList.Enabled = false;
//Fill Company dropdown list.
DataTable objCmpnyTable = oDo.DropDownList("select * from tblCompanyMaster");
if (objCmpnyTable.Rows.Count > 0)
{
companyList.DataSource = objCmpnyTable;
companyList.DataValueField = "CompId";
companyList.DataTextField = "CompName";
companyList.DataBind();
}
else
{
notifyLbl.Visible = true;
notifyLbl.Text = "There is not any company in the list.";
}
}
else
{
if (companyList.SelectedIndex <= 0)
{
//Disable department dropdown list and vacancy dropdown list till company is not selected.
deptList.Enabled = false;
vacanyList.Enabled = false;
}
}
}
catch (Exception)
{
throw;
}
}
protected void companyList_SelectedIndexChanged(object sender, EventArgs e)
{
//Disable vacancy dropdown list till depratment list is not selected
vacanyList.Enabled = false;
appForVacGrid.DataSource = null;
appForVacGrid.DataBind();
try
{
if (companyList.SelectedIndex > 0)
{
deptList.Enabled = true;
deptList.Items.Clear();
string str = "select * from vwCompWiseList where CompId=" + companyList.SelectedValue;
DataTable objDeptTable = oDo.DropDownList("select DeptId,DeptName from vwCompWiseDept where CompId= "+companyList.SelectedValue);
if (objDeptTable.Rows.Count > 0)
{
deptList.DataSource = objDeptTable;
deptList.DataTextField = "DeptName";
deptList.DataValueField = "deptId";
deptList.DataBind();
deptList.Items.Insert(0, new ListItem("--Select Department--", "-1"));
}
else
{
deptList.Items.Insert(0, new ListItem("--No Departments--", "-1"));
notifyLbl.Visible = true;
notifyLbl.Text = "No Departments Available in " + companyList.SelectedItem.Text;
}
}
else
{
notifyLbl.Visible = true;
notifyLbl.Text = "Select Company....";
appForVacGrid.DataSource = null;
appForVacGrid.DataBind();
}
}
catch (Exception)
{
throw;
}
}
protected void deptList_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (deptList.SelectedIndex > 0)
{
vacanyList.Enabled = true;
vacanyList.Items.Clear();
DataTable objVacancytbl = oDo.DropDownList("select VacId,VacTitle from tblVacancyMaster where DeptId =" + deptList.SelectedValue + " and CompId=" + companyList.SelectedValue);
if (objVacancytbl.Rows.Count > 0)
{
vacanyList.DataSource = objVacancytbl;
vacanyList.DataValueField = "VacId";
vacanyList.DataTextField = "VacTitle";
vacanyList.DataBind();
vacanyList.Items.Insert(0, new ListItem("--Select Vacancy--", "-1"));
appForVacGrid.DataSource = null;
appForVacGrid.DataBind();
}
else
{
notifyLbl.Visible = true;
notifyLbl.Text = "ALL VACANCIES ARE CLOSED IN "+" "+deptList.SelectedItem.Text.ToUpper();
vacanyList.Enabled = false;
appForVacGrid.DataSource = null;
appForVacGrid.DataBind();
}
}
else
{
notifyLbl.Visible = true;
notifyLbl.Text = "Select Department...";
appForVacGrid.DataSource = null;
appForVacGrid.DataBind();
vacanyList.Enabled = false;
}
}
catch (Exception)
{
throw;
}
}
protected void vacanyList_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
DataTable AppListTbl = objAppVacDetail.GetValue("CompId=" + companyList.SelectedValue + " and DeptId=" + deptList.SelectedValue + " and VacId=" + vacanyList.SelectedValue);
if (AppListTbl.Rows.Count > 0)
{
appForVacGrid.DataSource = AppListTbl;
appForVacGrid.DataBind();
appForVacGrid.Columns[5].Visible = false;
Session.Add("snAppListTbl", AppListTbl);
titleLbl.Visible = true;
titleLbl.Text = AppListTbl.Rows.Count.ToString() + " " + "CANDIDATE(S) ARE ELIGIBLE FOR THE POST OF" + " " + vacanyList.SelectedItem.Text.ToUpper() + ".";
sendMailBtn.Visible = true;
}
else
{
notifyLbl.Visible = true;
notifyLbl.Text = "ALL VACANCIES ARE CLOSED IN " + " " + deptList.SelectedItem.Text.ToUpper();
appForVacGrid.DataSource = null;
appForVacGrid.DataBind();
}
}
catch (Exception)
{
throw;
}
}
protected void appForVacGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//DataTable AppListTbl = (DataTable)Session["snAppListTbl"];
titleLbl.Visible = true;
titleLbl.Text = ((DataTable)Session["snAppListTbl"]).Rows.Count.ToString() + " " + "CANDIDATE(S) ARE ELIGIBLE FOR THE POST OF" + " " + vacanyList.SelectedItem.Text.ToUpper() + ".";
appForVacGrid.PageIndex = e.NewPageIndex;
appForVacGrid.DataSource = (DataTable)Session["snAppListTbl"];
appForVacGrid.DataBind();
sendMailBtn.Visible = true;
}
protected void sendMailBtn_Click(object sender, EventArgs e)
{
DataTable AppListTable = ((DataTable)Session["snAppListTbl"]);
int intTotalRows = AppListTable.Rows.Count;
string strFromId="",strToId="",strCcId="",strBccId="";
string strVacTitle="",strCompName="",strMailBody="";
string strSubject = "Rgarding Selection of Your Resume";
Label EmailLbl;
//for (int intRow = 0; intRow < intTotalRows; intRow++)
foreach (GridViewRow Row in appForVacGrid.Rows)
{
CheckBox objSendMail = (CheckBox)Row.FindControl("sendMailBox");
//CheckBox objSendMail = (CheckBox)appForVacGrid.Rows[Row].FindControl("sendMailBox");
if (objSendMail.Checked)
{
if (strToId == "")
{
//strToId = AppListTable.Rows[Row]["AppEmailId1"].ToString();
EmailLbl = (Label)Row.FindControl("emailLbl");
strToId = EmailLbl.Text;
}
else
{
//strToId += "," + AppListTable.Rows[Row]["AppEmailId1"].ToString();
EmailLbl = (Label)Row.FindControl("emailLbl");
strToId +=","+ EmailLbl.Text;
}
}
}
strVacTitle = AppListTable.Rows[intTotalRows]["VacTitle"].ToString();
strCompName = AppListTable.Rows[intTotalRows]["CompName"].ToString();
strMailBody = CommonProcedures.GetMailBody(strVacTitle, strCompName);
//CommonProcedures.SendMail(strFromId, strToId, strCcId, strBccId, strSubject, null, strMailBody, false);
}
}
现在我想向所有选中复选框的候选人发送邮件..我尝试了很多东西,但没有得到任何解决方案
Now I want to send mail to all those candidates whose checkbox is checked..I trid so many things but didnt get any solution
推荐答案
是的,没错,它只会返回当前页面行.如果您真的想获取总行数,则需要从数据源获取.
yes, that's true, it will only return the current page rows only. If you really want to get the total row count, you need to get from your datasource instead.
就像...如果你有 DataTable 那么它会像...
Like... if you have DataTable then it will be like...
DataTable.Rows.Count
这篇关于使用分页计算gridview的总行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!