通过Coading完成分页时从gridview获取价值的问题 [英] problem in getting value from gridview when paging is done by coading
问题描述
朋友,
我启用分页并使用以下事件
Friends,
I enable paging and use following event
protected void gvReprintEmail_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvReprintEmail.PageIndex = e.NewPageIndex;
DataTable dt1 = (DataTable)Session["dt"];
gvReprintEmail.DataSource = dt1;
gvReprintEmail.DataBind();
}
但是当我想从gridview中获取任何值时,我就得到了异常,因为在rowcommand事件中,我从当前事件中获取了row的值.因此有一个异常,例如索引超出范围.必须为非负数并且小于集合的大小."
我使用的页面大小为3,在第二页和之后的页面中,出现了这样的异常.
这是gridview的列之一
< itemtemplate>
< asp:标签runat ="server" ID ="lblMessage" Text =''<%#Server.HtmlDecode(Eval("Body").ToString())%>''>
以下是发生异常的rowcommand事件
在第一页中这没有问题,但是在随后的页面中,当我单击gridview中给出的链接时,将出现异常
受保护的void gvReprintEmail_RowCommand(object sender,GridViewCommandEventArgs e)
{
int索引= Convert.ToInt32(e.CommandArgument); <-在第二页的第一行上,我单击,因此索引将为3,因为页面大小为3
标签lbl =(标签)gvReprintEmail.Rows [index] .FindControl("lblMessage"); <-exception
Session ["SentDate"] = gvReprintEmail.Rows [index] .Cells [0] .Text;
Session ["Subject"] = gvReprintEmail.Rows [index] .Cells [1] .Text;
会话[消息"] = lbl.Text;
OpenNewWindow("PrintEmail.aspx");
}
请帮助我.在此先感谢.
But when i want to get any value from gridview then i got exception because in rowcommand event i get value of row from current event. So there is exception like "Index was out of range. Must be non-negative and less than the size of the collection."
I use page size 3 and in 2nd and afterward pages i got exception like this.
here is one of column of gridview
<itemtemplate>
<asp:Label runat="server" ID="lblMessage" Text=''<%# Server.HtmlDecode(Eval("Body").ToString()) %>'' >
following is rowcommand event where i got exception
in first page there is no problem in this, but afterwards page when i click link given in gridview then there will exception
protected void gvReprintEmail_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument); <--on 2nd page first row i click so index will be 3 because page size 3
Label lbl = (Label)gvReprintEmail.Rows[index].FindControl("lblMessage"); <--exception
Session["SentDate"] = gvReprintEmail.Rows[index].Cells[0].Text;
Session["Subject"] = gvReprintEmail.Rows[index].Cells[1].Text;
Session["Message"] = lbl.Text;
OpenNewWindow("PrintEmail.aspx");
}
Kindly help me. Thanks in advance.
推荐答案
查看此处: ^ ]
private void BindData()
{
string connectionString = "Server=localhost;" +
"Database=Northwind;Trusted_Connection=true";
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand myCommand = new SqlCommand("usp_GetProducts",
myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@startRowIndex",
currentPageNumber);
myCommand.Parameters.AddWithValue("@maximumRows", PAGE_SIZE);
myCommand.Parameters.Add("@totalRows", SqlDbType.Int, 4);
myCommand.Parameters["@totalRows"].Direction =
ParameterDirection.Output;
SqlDataAdapter ad = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
ad.Fill(ds);
gvProducts.DataSource = ds;
gvProducts.DataBind();
// get the total rows
double totalRows = (int)myCommand.Parameters["@totalRows"].Value;
lblTotalPages.Text = CalculateTotalPages(totalRows).ToString();
lblCurrentPage.Text = currentPageNumber.ToString();
if (currentPageNumber == 1)
{
Btn_Previous.Enabled = false;
if (Int32.Parse(lblTotalPages.Text) > 0)
{
Btn_Next.Enabled = true;
}
else
Btn_Next.Enabled = false;
}
else
{
Btn_Previous.Enabled = true;
if (currentPageNumber == Int32.Parse(lblTotalPages.Text))
Btn_Next.Enabled = false;
else Btn_Next.Enabled = true;
}
}
计算表中的总行数
Calculate total rows in table
private int CalculateTotalPages(double totalRows)
{
int totalPages = (int) Math.Ceiling(totalRows / PAGE_SIZE);
return totalPages;
}
制作两个按钮(下一个/上一个),然后单击它们调用此方法
Make two buttons(next/Previous) and call this method on their click
protected void ChangePage(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "Previous":
currentPageNumber = Int32.Parse(lblCurrentPage.Text) - 1;
break;
case "Next":
currentPageNumber = Int32.Parse(lblCurrentPage.Text) + 1;
break;
}
BindData();
}
尝试以下链接:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=339 [ ^ ]
http://msdn.microsoft.com/en-us/library/5aw1xfh3.aspx [ ^ ]
Try the following links:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=339[^]
http://msdn.microsoft.com/en-us/library/5aw1xfh3.aspx[^]
这篇关于通过Coading完成分页时从gridview获取价值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!