DataGrid PageIndexChangedHandler not getting called
-
Hi all, I am facing a great problem with the Webcontrol DataGrids PageIndexChangedHandler is not getting Called. I am creating a DropDownList and a DataGrid Dynamically. The DropDownLists AutoPostBack=true. On selecting an Item in the Dropdownlist PostBack happens and Dropdownlists SelectionChanged handler gets called where the Query is fired to Database and the DataSet is assigned to DataGrid and its DataBind() is called. But the problem lies here, when you click the page index of DataGrid. The code never runs into the PageIndexChange Handler of the DataGrid. It runs through the Page_Load() and comes out. What may be the problem? Please help me out.
-
Hi all, I am facing a great problem with the Webcontrol DataGrids PageIndexChangedHandler is not getting Called. I am creating a DropDownList and a DataGrid Dynamically. The DropDownLists AutoPostBack=true. On selecting an Item in the Dropdownlist PostBack happens and Dropdownlists SelectionChanged handler gets called where the Query is fired to Database and the DataSet is assigned to DataGrid and its DataBind() is called. But the problem lies here, when you click the page index of DataGrid. The code never runs into the PageIndexChange Handler of the DataGrid. It runs through the Page_Load() and comes out. What may be the problem? Please help me out.
-
1, View the cs code, confirm that you have created the PageIndexChanged function; 2, Did you call the function in 'private void InitializeComponent()' No pains, no gains.
I have the PageIndexChanged function in my cs code. And since I am creating the controls in the Page_Load() I cannot call the function in InitializeComponent. But now I found something else when the DataGrid is Populated on Page_Load itself the Paging works but when it is populated on Dropdownlist Selection Handler DataGrid Paging doesn't work. Here is my cs code....Please help me.... protected System.Web.UI.HtmlControls.HtmlForm Form1; //Object of the form same as form id in html private void Page_Load(object sender, System.EventArgs e) { SqlConnection conn = new SqlConnection("Data Source=10.0.0.245;Initial Catalog=Sibi;User Id=sa;Password=sa"); SqlDataAdapter adpt = new SqlDataAdapter("Select * from Login",conn); DataSet ds = new DataSet(); adpt.Fill(ds); DropDownList drplst = new DropDownList(); //Dropdownlist is created drplst.ID = "drplst"; drplst.AutoPostBack = true; drplst.SelectedIndexChanged +=new EventHandler (this.drplst_SelectedIndexChanged); drplst.Items.Add("Green"); drplst.Items.Add("Blue"); drplst.Items.Add("Red"); Form1.Controls.Add(drplst); DataGrid dgrid = new DataGrid(); //DataGrid is Created dgrid.ID = "dgrid"; dgrid.PageIndexChanged +=new DataGridPageChangedEventHandler(this.dgrid_PageIndexChanged); dgrid.AllowPaging = true; dgrid.PagerStyle.Mode = PagerMode.NumericPages; dgrid.PageSize = 2; Form1.Controls.Add(dgrid); dgrid.AutoGenerateColumns = false; BoundColumn name = new BoundColumn(); name.HeaderText = "NAME"; name.DataField = "Name"; dgrid.Columns.Add(name); BoundColumn Id = new BoundColumn(); Id.HeaderText = "Id"; Id.DataField = "Id"; dgrid.Columns.Add(Id); ViewState["DS"] = ds; } private void dgrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) { DataGrid dgrid = (DataGrid)Form1.FindControl("dgrid"); dgrid.CurrentPageIndex = e.NewPageIndex; dgrid.DataSource = (DataSet)ViewState["DS"]; dgrid.DataBind(); } private void drplst_SelectedIndexChanged(object sender, System.EventArgs e) { DataGrid dgrid = (DataGrid)Form1.FindControl("dgrid"); dgrid.DataSource = (DataSet)ViewState["DS"]; dgrid.DataBind(); }
-
I have the PageIndexChanged function in my cs code. And since I am creating the controls in the Page_Load() I cannot call the function in InitializeComponent. But now I found something else when the DataGrid is Populated on Page_Load itself the Paging works but when it is populated on Dropdownlist Selection Handler DataGrid Paging doesn't work. Here is my cs code....Please help me.... protected System.Web.UI.HtmlControls.HtmlForm Form1; //Object of the form same as form id in html private void Page_Load(object sender, System.EventArgs e) { SqlConnection conn = new SqlConnection("Data Source=10.0.0.245;Initial Catalog=Sibi;User Id=sa;Password=sa"); SqlDataAdapter adpt = new SqlDataAdapter("Select * from Login",conn); DataSet ds = new DataSet(); adpt.Fill(ds); DropDownList drplst = new DropDownList(); //Dropdownlist is created drplst.ID = "drplst"; drplst.AutoPostBack = true; drplst.SelectedIndexChanged +=new EventHandler (this.drplst_SelectedIndexChanged); drplst.Items.Add("Green"); drplst.Items.Add("Blue"); drplst.Items.Add("Red"); Form1.Controls.Add(drplst); DataGrid dgrid = new DataGrid(); //DataGrid is Created dgrid.ID = "dgrid"; dgrid.PageIndexChanged +=new DataGridPageChangedEventHandler(this.dgrid_PageIndexChanged); dgrid.AllowPaging = true; dgrid.PagerStyle.Mode = PagerMode.NumericPages; dgrid.PageSize = 2; Form1.Controls.Add(dgrid); dgrid.AutoGenerateColumns = false; BoundColumn name = new BoundColumn(); name.HeaderText = "NAME"; name.DataField = "Name"; dgrid.Columns.Add(name); BoundColumn Id = new BoundColumn(); Id.HeaderText = "Id"; Id.DataField = "Id"; dgrid.Columns.Add(Id); ViewState["DS"] = ds; } private void dgrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) { DataGrid dgrid = (DataGrid)Form1.FindControl("dgrid"); dgrid.CurrentPageIndex = e.NewPageIndex; dgrid.DataSource = (DataSet)ViewState["DS"]; dgrid.DataBind(); } private void drplst_SelectedIndexChanged(object sender, System.EventArgs e) { DataGrid dgrid = (DataGrid)Form1.FindControl("dgrid"); dgrid.DataSource = (DataSet)ViewState["DS"]; dgrid.DataBind(); }
Pls see the code: protected System.Web.UI.HtmlControls.HtmlForm Form1; private void Page_Load(object sender, System.EventArgs e) { SetTempData(); DataGrid dgrid = new DataGrid(); //DataGrid is Created dgrid.ID = "dgrid"; dgrid.PageIndexChanged += new DataGridPageChangedEventHandler(this.dgrid_PageIndexChanged); dgrid.AllowPaging = true; dgrid.PagerStyle.Mode = PagerMode.NumericPages; dgrid.PageSize = 2; Form1.Controls.Add(dgrid); dgrid.AutoGenerateColumns = false; BoundColumn name = new BoundColumn(); name.HeaderText = "Name"; name.DataField = "Name"; dgrid.Columns.Add(name); BoundColumn Id = new BoundColumn(); Id.HeaderText = "ID"; Id.DataField = "ID"; dgrid.Columns.Add(Id); dgrid.DataSource = ((DataTable)ViewState["DT"]).DefaultView; dgrid.DataBind(); } private void dgrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) { DataGrid dgrid = (DataGrid)Form1.FindControl("dgrid"); dgrid.CurrentPageIndex = e.NewPageIndex; dgrid.DataSource = ((DataTable)ViewState["DT"]).DefaultView; dgrid.DataBind(); } private void SetTempData() { DataTable dt = new DataTable(); dt.Columns.Add("ID", typeof(String)); dt.Columns.Add("Name", typeof(String)); DataRow dr; for (int i=0; i<100; i++) { dr = dt.NewRow(); dr["ID"] = i.ToString("000"); dr["Name"] = "Name" + i.ToString("000"); dt.Rows.Add(dr); } ViewState["DT"] = dt; } No pains, no gains.