Result cannot be changed upon changing gridview page number - asp.net

When I was changing page number, I was getting error
"The GridView 'GridView1' fired event PageIndexChanging which wasn't handled."
But afterwards, I searched and try to put this code in PageIndexChanging even, still it isn't working :
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.SelectedIndex = e.NewPageIndex;
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
Originally, when user prompts to page, I am showing all data on gridview, then user can search data, and upon clicking Search button, below code is executing :
protected void Button1_Click(object sender, EventArgs e)
{
DateTime dt1 = DateTime.Now, dt2 = DateTime.Now;
Connection.getCon();
try
{
dt1 = Convert.ToDateTime(TextBox3.Text);
dt2 = Convert.ToDateTime(TextBox4.Text).AddDays(1);
lblError.Visible = false;
}
catch (Exception exc) {
lblError.Visible = true;
}
string cmd = "select * from tblLogs where (users like '%"+TextBox1.Text.Trim()+"%') and (request like '%"+TextBox2.Text.Trim()+"%') and (requesttime>='"+dt1+"') and (requesttime<'"+dt2+"') ";
SqlDataSource1.SelectCommand = cmd;
DataView dv= (DataView) SqlDataSource1.Select(DataSourceSelectArguments.Empty);
GridView1.DataSourceID = null;
GridView1.DataSource= dv;
GridView1.DataBind();
//GridView1.AllowPaging = false;
}
Now, I am not getting any error, but still page is not changing and staying on 1.
Thanks.

You are using the wrong property in your PageIndexChanging event, it should be PageIndex instead of SelectedIndex:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}

Related

Asp.net Gridview row edit - How to remove select row event

Hi Friends wish you all Happy New Year 2017!
I am displaying records in grid and i have code to select the row and display records in text boxes as per selected row.
I have Edit Button to edit records in grid row but when i click on text box (edit mode) to enter value it is showing error because the "select row" event is still active.
Any help how to remove the select row action when edit button is clicked.
// click on the row to select and display records in text boxes
protected void gvUsrEdit_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = gvUsrEdit.SelectedRow;
Label l1 = row.FindControl("Label1") as Label;
Label l2 = row.FindControl("Label2") as Label;
i_TranInputID.Text = l1.Text;
tReason.Text = l11.Text;
gvUsrEdit.Visible = false;
}
protected void gvUsrEdit_RowEditing(object sender, GridViewEditEventArgs e)
{
gvUsrEdit.EditIndex = e.NewEditIndex;
show1();
}
public void show1()
{
string strquery = "select * from btaprs2 where vEmpID=#d1 and vQuarter=#d2 and vyear1=#d3 and tKRA=#d4 and v10='Active' ";
con.Open();
SqlCommand cmd = new SqlCommand(strquery, con);
try
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvUsrEdit.DataSource = ds;
gvUsrEdit.DataBind();
con.Close();
}
catch (Exception ex)
{
Response.Write(ex);
Label46.Text = "Error in page please check!";
}
}
You need to change the RowIndex.
protected void gvUsrEdit_RowEditing(object sender, GridViewEditEventArgs e)
{
gvUsrEdit.EditIndex = -1;
show1();
}
I would suggest to Edit the rows this way:
You may want to use RowUpdating method like this:
protected void gvUsrEdit_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
i_TranInputID.Text = ((Label)gvUsrEdit.Rows[e.RowIndex].FindControl("Label1") ).Text;
tReason.Text = ((Label)gvUsrEdit.Rows[e.RowIndex].FindControl("Label2")).Text;
tReason.Text = l11.Text;
gvUsrEdit.EditIndex = -1;
show1();
}

Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount

i want to bind my data Gridview but it throw an error.
i allow paging when i click on new page e.g 1.2.3 then the above error throw
here is my code.
the one is page index change and the other one is my method of BindGrid.
the error come when i click one new page 2 or 3 etc
updated code
protected void DataGrid1_PageIndexChanged(Object sender, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataSource = Session["data"] as DataTable;
DataGrid1.DataBind();
}
private void BindGrid()
{
DataTable data = storedProcedureManager.sp_inactiveFiles(
providerID,
days, CaseTypeID, CollectorID, user.UserRegID);
lblMsg.Text = data.Rows.Count + " Record's Found.";
log.Info(lblMsg.Text);
Session["data"] = data;
DataGrid1.DataSource = data;
DataGrid1.DataBind();
log.Info("Report Displayed.");
}
Problem what is happening is when you fetch the data again in BindGrid() method you must be getting less number of pages.so you can do two things.
1)If you are filtering your data then reset the page index to 1.
protected void DataGrid1_PageIndexChanged(Object sender, DataGridPageChangedEventArgs e)
{
BindGrid();
}
private void BindGrid()
{
DataTable data = storedProcedureManager.sp_inactiveFiles(
providerID,
days,CaseTypeID,CollectorID,user.UserRegID);
lblMsg.Text = data.Rows.Count + " Record's Found.";
log.Info(lblMsg.Text);
DataGrid1.DataSource = data;
DataGrid1.CurrentPageIndex = 0;
DataGrid1.DataBind();
log.Info("Report Displayed.");
}
2)If you are not filtering your data then store the datagrid value in a session object and use this for pageing.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}
protected void DataGrid1_PageIndexChanged(Object sender, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataSource = Session["value"] as DataTable;
DataGrid1.DataBind();
}
private void BindGrid()
{
DataTable data = storedProcedureManager.sp_inactiveFiles(
providerID,
days,CaseTypeID,CollectorID,user.UserRegID);
lblMsg.Text = data.Rows.Count + " Record's Found.";
log.Info(lblMsg.Text);
Session["value"]=data;
DataGrid1.DataSource = data;
DataGrid1.DataBind();
log.Info("Report Displayed.");
}
DataGrid1.DataSource = data;
DataGrid1.DataBind();
DataGrid1.CurrentPageIndex = 0;
Reset the CurrentPageIndex to 0 after the page has been bound.

keep dataTable present through page navigating aspx.net

i have data Table which is dynamically created and is bind to grid View and the page have a button which is redirecting user on another page when is clicked. That's page one.Now when user is redirected on page 2, and if he want to get back on page 1, I want to be present data Table when page load.Some example code how to do that?
page1
protected void Page_Load(object sender, EventArgs e)
{
dtCurrentTable = (DataTable)ViewState["Markici"];
GridView2.DataSource = dtCurrentTable();
GridView2.DataBind();
}
public void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("Page2.aspx");
}
//method for insert dataTable in database
public void Button2_Click(object sender, EventArgs e)
{
dtCurrentTable = (DataTable)ViewState["Markici"];
Session["Markici"] = dtCurrentTable;
dtCurrentTable = (DataTable)Session["Markici"];
GridView2.DataSource = dtCurrentTable;
GridView2.DataBind();
SqlCommand cmd = new SqlCommand("InsertMarkica",conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "InsertMarkica";
foreach (DataRow dr in dtCurrentTable.Rows)
{
cmd.Parameters.Clear();
//SqlCommand cmd = new SqlCommand(cmdstr, conn);
cmd.Parameters.AddWithValue("#FirmaID", dr["FirmaID"]);
cmd.Parameters.AddWithValue("#Godina", dr["Godina"]);
cmd.Parameters.AddWithValue("#Kasa", dr["KasaID"]);
cmd.Parameters.AddWithValue("#Masa", dr["Masa"]);
cmd.Parameters.AddWithValue("#MarkicaID", dr["MarkicaID"]);
cmd.Parameters.AddWithValue("#Datum", dr["Datum"]);
cmd.Parameters.AddWithValue("#VrabotenID", dr["VrabotenID"]);
cmd.Parameters.AddWithValue("#Smena", dr["Smena"]);
cmd.Parameters.AddWithValue("#VkIznos", dr["VkIznos"]);
cmd.Parameters.AddWithValue("#VkDanok", dr["VkDanok"]);
cmd.ExecuteNonQuery();
cmd.Connection = conn;
}
page2
public void Button2_Click(object sender, EventArgs e)
{
Reponse.Redirect("Page1.aspx");
//Now when is redirected on page1, it should be present data Tale,
//needed that data Table for inserting records in database
}
You can create a session variable as follow
protected void Page_Load(object sender, EventArgs e)
{
dtCurrentTable = (DataTable)ViewState["Markici"];
Session["Markici"]=dtCurrentTable;
GridView2.DataSource = dtCurrentTable();
GridView2.DataBind();
}
Not the value of Session["Markici"] will be available in the current session on any page. You need to cast and use it as follow
dtCurrentTable = (DataTable)Session["Markici"];

GridView PageIndexChanging event

I have a gridview for which I set the boundfields as well as the datasource in code behind and on the PageIndexChanging event I set:
protected void grvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grvList.PageIndex = e.NewPageIndex;
grvList.DataBind();
}
BoundFields are also coming from the database, I have added them like this:
foreach (DataRow drColumn in dtColumns.Rows)
{
BoundField bfEmbeddedColumn = new BoundField();
bfEmbeddedColumn.HeaderText = drColumn["ColName"].ToString();
bfEmbeddedColumn.DataField = drEmbeddedTaskColumn["ColName"].ToString();
bfEmbeddedColumn.ItemStyle.Width = 120;
grvList.Columns.Add(bfEmbeddedColumn);
}
It does show the records on the next page, but my problem is that each time the page index is changed it add the boundfields again. How can I prevent this from happening, it there a way I can solve this issue?
Thank you very much.
you have to move the following index to the variable e to the pager.
try this:
grdSqlQuery.PageIndex = e.NewPageIndex
grdSqlQuery.DataSource = ViewState("dts_Sqlquery")
grdSqlQuery.DataBind()
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
try
{
int count = 0;
SetData();
GridView1.AllowPaging = false;
GridView1.DataBind();
ArrayList arr = (ArrayList)ViewState["SelectedRecords"];
count = arr.Count;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (arr.Contains(GridView1.DataKeys[i].Value))
{
Session.Add("PatientID", GridView1.DataKeys[i].Value.ToString());
arr.Remove(GridView1.DataKeys[i].Value);
Response.Redirect("~/EditPaitientDetails.aspx");
}
}
ViewState["SelectedRecords"] = arr;
hfCount.Value = "0";
GridView1.AllowPaging = true;
BindGrid();
//ShowMessage(count);
}
catch (Exception ex)
{
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
Found the solution to this. My problem was caused by the fact that on Page_Load I was adding the boundfields over and over again.

How to select Listbox value in asp.net

I have a ListBox in my webpage that is bound from database in this way:
ListBox1.DataTextField = "Text";
ListBox1.DataValueField = "MenuID";
ListBox1.DataSource = SqlHelper.ExecuteReader(DAL.DALBase.ConnectionString, "GetMenu");
ListBox1.DataBind();
I want to get selected item value and used this code but have a error and does not worked.
ListBox1.SelectedValue;
Forgive me if I have problems on how to write because my English is not good.
Can you be more specific on the error you are getting?
Using ListBox1.SelectedValue should work.
For example:
int mySelectedValue = int.Parse(ListBox1.SelectedValue);
or
string mySelectedValue = ListBox1.SelectedValue;
Edit
Added code to ensure the original poster was keeping values in ListBox databound.
protected void Page_Load( object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindListBox();
}
}
private BindListBox()
{
ListBox1.DataTextField = "Text";
ListBox1.DataValueField = "MenuID";
ListBox1.DataSource = SqlHelper.ExecuteReader(DAL.DALBase.ConnectionString, "GetMenu");
ListBox1.DataBind();
}
protected void SomeButton_Click( object sender, EventArgs e)
{
string mySelectedValue = ListBox1.SelectedValue;
}

Resources