Form View OnPageIndexChanging Event not working - c#

I have made AllowPaging to True in formview .It is showing the link buttons but they are not working(obviously they need a function to execute).I have OnPageIndexChangeing event like Below .Can you please tell how to jump to next page and make the pager working.I am bind the Form view using sp in Page_Load event.
Aspx code:
<asp:FormView ID="form_view_with_sp" runat="server" OnModeChanging="mode_changeing" OnPageIndexChanging="page_index_chaging" AllowPaging="true">
page_index_changing event:-
protected void page_index_chaging(object sender, FormViewPageEventArgs e)
{
}
Page_Load:-
SqlConnection con = new SqlConnection(getconnectionstring());
SqlCommand cmd = new SqlCommand();
//cmd.CommandText = "selectprocedure";
DataTable dt = new DataTable();
cmd.Connection = con;
con.Open();
SqlDataAdapter adb = new SqlDataAdapter("selectprocedure", con);
form_view_with_sp.DataSource = dt;
form_view_with_sp.DataBind();
Thanks in advance.

You will need to assign the DataSource of the FormView in the page_index_chaging event handler. Please try to use the following
protected void page_index_chaging(object sender, FormViewPageEventArgs e)
{
DataTable dt = (DataTable)ViewState["DataSource"];
form_view_with_sp.DataSource = dt;
form_view_with_sp.DataBind();
}
Remember to put the DataTable into ViewState in Page_Load event when creating the DataSource.

Related

call function with parameters in the Page Load .NET

I'm doing a filter form by 3 criteria, I'm having problems on the pagination since I do not load the data from the second page of the GridView, apparently I have to call my function that fills the GridView in the Load page event, but this function receives parameters that are the ones that the user enters in the search filter, How can I then call the function sending the parameters to that function in the page load event every time the page is changed in the GridView?
function
public DataTable AdvertSearch(string tittle, DateTime datel, DateTime date2)
{
SqlConnection con = new SqlConnection(Util.GetConnectionString("ConnectionString"));
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select*from table where titAdvert=#tittle or dateStart=#dateS" or dateEnd=#dateE;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#tittle", tittle== "" ? (object)DBNull.Value : tittle);
cmd.Parameters.AddWithValue("#dateS", date1== "" ? (object)DBNull.Value : date1);
cmd.Parameters.AddWithValue("#dateE", date2== "" ? (object)DBNull.Value : date2);
cmd.Connection = con;
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
search button
protected void BtnSearch_Click(object sender, EventArgs e)
{
GridView1.DataSource = AdvertSearch(txtTitle.Text,txtDateI.Text,txtDateF.Text);
GridView1.DataBind();
}
You need to specify a pageIndexChanging event to trigger the grid to be reloaded when the user clicks to the next page. Check out the code below:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();
}
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = AdvertSearch(txtTitle.Text,txtDateI.Text,txtDateF.Text);
GridView1.DataBind();
}
And please write your code in english, as it makes it more readable for all programmers.

GridView bound to Datatable- Paging does not work properly

I have a webform set up with a button click that parses a .txt file and puts it in a datatable. when the parsing ends the datatable is bound to the gridview. I tried to add paging since I get tons of rows but it does not seem to work properly. Whenever I click on the next page the grid disappears and for it to appear again on the correct page I need to click on the button again.
I tried adding the following code to the source :
<asp:GridView ID="GridView1" Runat="server"
AutoGenerateColumns="true"
AllowPaging="True" OnPageIndexChanging="OnPageIndexChanging" >
and this bit :
protected void OnPageIndexChanging(object sender, EventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
but it doesn't seem to change anything. Any idea on what I'm doing wrong?
Try to Add OnPageIndexChanging Event inside GridView
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
OnPageIndexChanging="OnPageIndexChanging" PageSize="5">
and your aspx.cs code. Try to Bind Gridview on Page_Load Event.
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{ this.BindGrid();}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * From [dbo].[your table] "))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
//here yo have to assign datasource
//Some thing like this
///GridView1.DataSource=DataTable;
GridView1.DataBind();
}

How to make a button refill a GridView each time it's clicked

I have a dropdown menu in Visual Studio and when a selection is made, a user clicks a button. Then the button fills a GridView.
It works for one time, but if I select something a second time and click the button, nothing happens. How do I make it refresh or do the action again?
Button Click in C#:
protected void ButtonChangeEvent_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["Events2"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("spRegistrantsGridView"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#Action", "SELECT");
cmd.Parameters.Add("#EventId", SqlDbType.Int).Value = DropDownListEvent.SelectedValue;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
Check the value of DropDownListEvent.SelectedValue when you click button for 2nd time. I suspect it takes the 1st value from ddl always. This happens if you have bound ddl in Page_Load event handler without putting !IsPostBack condition check.
protected void Page_Load(object sender, EventArgs e) {
if(!IsPostBack) {
// bind dropdownlist here
}
}

How to show data in grid view in another page on click of button in previous button

I am using these code to open details in another page on click of search button in previous page.
Is there any other alternative for these.
ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "Window.Open('Search_Advance.aspx','_blank');", true);
You can use asp:LinkButton for this. Set PostBackUrl to you page name. in this case
<asp:LinkButton ID="LnkBtn1" runat="server" PostBackUrl="Search_Advance.aspx" Text="Search"></asp:LinkButton>
on click of this button it ll go to tht page. and you can bind the gridview there.
SqlConnection con=new SqlConnection("connection Strin");
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand("SEARCH", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#City_Id",DropDownList4.SelectedValue.ToString());
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
Session["sessDataSet"] = ds;
Response.Redirect("Search_Basic.aspx");
then in Search_Basic.aspx
protected void Page_Load(object sender, Eventargs e)
{
DataSet ds=(DataSet)Session["sessDataSet"];
GridViewId.DataSource=ds;
GridViewId.DataBind();
}

binding grid view on button click in listview

I have a list view with a button for each row:
<td> <asp:Button ID="Button2" runat="server" Text="select" CommandName="view" CommandArgument='<%# Eval("inquiry_id")%>' onclick="buttonClick"/></td>
On click of this button i retrieve the id of the clicked row, set the session with the same id and bind the grid view.This the code behind the button click in list view:
ListViewItem item = (sender as Button).NamingContainer as ListViewItem;
Button butDetails = (Button)item.FindControl("Button2");
Int64 inquiryID = Convert.ToInt64(butDetails.CommandArgument);
Session["session_view_id"] = inquiryID;
this.BindGrid();
return;
The session is retrieved in bindGrid function. But the problem is gridview is not displayed at first click but on second click it gets displayed but with the data of the previous id clicked.The session is set but while binding the grid it uses old session value. Where am i going wrong to bind the grid?
The code for the bindGrid() function is :
int inquiryID = Convert.ToInt32(Session["session_view_id"]);
MySqlConnection conn = null;
try
{ MySqlCommand cmd = new MySqlCommand("SELECT * FROM crm_support_inquiry inner join crm_inquiry_perticipant on crm_support_inquiry.inquiry_id=?id inner join crm_mailer_types on crm_support_inquiry.mailer_id=crm_mailer_types.mailer_id limit 4", connect);
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Parameters.AddWithValue("?id", inquiryID);
cmd.Connection = connect;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
lblComp.Text = dt.Rows[0]["company"].ToString();
lblCname.Text = dt.Rows[0]["contact_name"].ToString();
lblEmail.Text = dt.Rows[0]["email"].ToString();
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
Are you checking for IsPostBack in your Page_Load method? You need to wrap your BindGrid call in your Page_Load in an if(!IsPostBack) statement to prevent the Page_Load from initially refreshing the data on postback. This can prevent the changes you are making later on in your event handler.
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}
This answer can provide additional info: How to update page data after event handling?

Categories