2014年7月13日 星期日

[RESOLVED] Gridview OR Formview


I am using Visual studio 2010 C# language and SQL SERVER 2008 for a web application...


I have a Gridview control, a Button control, and a Label control on a web page...The button control is placed on the web page and not inside the Gridview control...The page size of the Gridview control is 1 so it displays one row per each page...


My aim is when I click on the Button control then the Gridview pager will move from one page to another page and the text on the cell (2) of the present row in the Gridview control will be displayed on the label control...


I ran my code and was getting an error message....Look at my code below: 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;
using System.Data.SqlClient;



public partial class testing : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
Response.Write("WELCOME");


}
public void CustomersGridView_PageIndexChanging(Object sender, GridViewSelectEventArgs e)
{
GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex];
lblmsg.Text = CustomersGridView.SelectedRow.Cells[2].ToString();
}
public void CustomersGridView_PageIndexChanged(Object sender, EventArgs e)
{
// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;
MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";
}

public void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{

// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;

// Display the company name from the selected row.
// In this example, the third column (index 2) contains
// the company name.
MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";

}

public void CustomersGridView_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{

// Get the currently selected row. Because the SelectedIndexChanging event
// occurs before the select operation in the GridView control, the
// SelectedRow property cannot be used. Instead, use the Rows collection
// and the NewSelectedIndex property of the e argument passed to this
// event handler.
GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex];



// You can cancel the select operation by using the Cancel
// property. For this example, if the user selects a customer with
// the ID "ANATR", the select operation is canceled and an error message
// is displayed.
if (row.Cells[1].Text == "ANATR")
{

e.Cancel = true;
MessageLabel.Text = "You cannot select " + row.Cells[2].Text + ".";

}

}
protected void Button1_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
Response.Redirect("home.aspx", true);
}
protected void btnnext_Click(object sender, EventArgs e)
{
if (CustomersGridView.PageIndex == null)
{

}
else
{



CustomersGridView.PageIndex = CustomersGridView.PageIndex + 1;

lblmsg.Text = CustomersGridView.SelectedRow.Cells[2].ToString();

{

}

if (CustomersGridView.PageIndex == CustomersGridView.PageCount)
{
lblmsge.Text = "THIS IS THE LAST PAGE!!!.......";

}
else
{
lblmsge.Text = "THIS IS PAGE " + CustomersGridView.PageIndex + "of " + CustomersGridView.PageCount + ".";
}
}

}


}

I got this error message:


Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index


Description: An
unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 



Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index



Source Error: 











Line 99:             CustomersGridView.PageIndex = CustomersGridView.PageIndex + 1;
Line 100: //lblmsg.Text = "You have selected " + row.Cells[2].Text + ".";
Line 101: lblmsg.Text = CustomersGridView.SelectedRow.Cells[2].ToString(); Line 102:
Line 103: {




Source File: c:\onlinelibrary\testing.aspx.cs    Line: 101 



Stack Trace: 











[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
System.Collections.ArrayList.get_Item(Int32 index) +9373696
System.Web.UI.WebControls.GridView.get_SelectedRow() +53
testing.btnnext_Click(Object sender, EventArgs e) in c:\onlinelibrary\testing.aspx.cs:101
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563



Please I need your help to solve this problem...





Make sure CustomersGridView. PageIndex=0

hi,




ken4aspnet


I have a Gridview control, a Button control, and a Label control on a web page...The button control is placed on the web page and not inside the Gridview control...The page size of the Gridview control is 1 so it displays one row per each page...


the button is not inside the gridview and the CustomersGridView_PageIndexChanging event will can't get the
CustomersGridView.SelectedRow


the page size is 1 you can get the cell (2) by the following code :


public void CustomersGridView_PageIndexChanging(Object sender, GridViewSelectEventArgs e)
{
GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex];
lblmsg.Text = CustomersGridView.Rows[0].Cells[2].Text();
}

Hope this helps




Thanks...I changed the bntnext_click event code like this:


protected void btnnext_Click(object sender, EventArgs e)
{
if (CustomersGridView.PageIndex == null)
{

}
else
{



CustomersGridView.PageIndex = CustomersGridView.PageIndex + 1;

//lblmsg.Text = CustomersGridView.SelectedRow.Cells[2].ToString();
lblnxt.Text = CustomersGridView.Rows[0].Cells[2].Text + ".";
txtid.Text = CustomersGridView.Rows[0].Cells[1].Text;
txtfname.Text = CustomersGridView.Rows[0].Cells[2].Text;

if (CustomersGridView.PageIndex == CustomersGridView.PageCount)
{
lblmsge.Text = "THIS IS THE LAST PAGE!!!.......";

}
else
{
lblmsge.Text = "THIS IS PAGE " + CustomersGridView.PageIndex + "of " + CustomersGridView.PageCount + ".";
}
}


}

I ran the program and it worked fine...




沒有留言:

張貼留言