hello everyone.
i have a gridview with 2000 rows. i have filled a page and it fills dynamically very well. one of its object is a linkbutton and when i select it, it opens the requiered page properly.
1. i have changed the page in matrix form with 6 objects in each page. suddently, the libkbutton click has lost it function???!!! may i ask you please see in the attached codes and let me know the reason? i appreciate any correction in form of codes, very kind regards
2. i have used page indexes for pages. but the global value, which is 0 from the beginning, remains 0, even i increament or decreament in next/previous buttons click methods. Why??!! please see the attached codes and i appreciate corrections with codes, very kind regards
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using AjaxControlToolkit;
namespace evenemang
{
public partial class locals : System.Web.UI.Page
{
string mainconn = ConfigurationManager.ConnectionStrings["CustomerDB"].ConnectionString;//@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\venus-messiah\source\repos\WebAd\App_Data\CustomerDB.mdf;Integrated Security=True";//ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["CustomerDB"].ConnectionString);//@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\venus-messiah\source\repos\WebAd\App_Data\CustomerDB.mdf;Integrated Security=True"); //@"Data Source=d:\web\localuser\myearn.org\world\App_Data\customerDB.mdf;Integrated Security=True";
DataTable dt = new DataTable();
string search, namn, nr;
int m = 0, Pindex=0, Psize=6;
string[] name = new string[9000];
string[] image = new string[9000];
string[] Email = new string[9000];
Sorting objSorting = new Sorting();
private string SortDirection
{
get { return ViewState["SortDirection"] != null ? ViewState["SortDirection"].ToString() : "ASC"; }
set { ViewState["SortDirection"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
Session["result"] = null;
if (Session["search"] != null) //from search
{
search = Session["search"].ToString();
if (Session["Name"] != null && search == "search")
{
namn = Session["Name"].ToString();
BindGridView(1);
Session["search"] = null;
}
else
lblMessage.Text = "No title!";
}
if (!IsPostBack)
{
Session["result"] = null;
BindGridView(0);
}
FillPage(Pindex, Psize);
}
void BindGridView(int n)
{
SqlCommand commandad = new SqlCommand("", sqlcon);
if (n == 0)
{
string query = string.Format("Select * from customerad");
commandad.CommandText = query;
}
else if (n == 1)
{
string query = string.Format("Select * from customerad where Name='{0}'", namn);
commandad.CommandText = query;
}
SqlDataAdapter da = new SqlDataAdapter(commandad);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
Session["Name"] = null;
}
string bringreview(string k)
{
string betyg = "";
string q = string.Format("Select * from tblrating where Email='{0}'", k);
using (var conad = new SqlConnection(mainconn))
using (var comandad = new SqlCommand(q, conad))
{
conad.Open();
SqlDataReader reader = comandad.ExecuteReader();
while (reader.Read())
{
betyg = (reader.GetInt32(4)).ToString();
}
conad.Close();
}
return betyg;
}
protected void FillPage(int pageindex, int pagesize)
{
HtmlForm form2 = new HtmlForm();
TableRow row = null;
int col = 0, rows=0;
int mm= GridView1.Rows.Count/6;
int totalColumns = 3;
int totalrows = 2;
for ( int i = pageindex*pagesize; i<(1+pageindex)*pagesize && i < GridView1.Rows.Count; i++)
{
if (col == 0)
{
row = new TableRow();
mytable.Rows.Add(row); // Add a new row to the table
}
TableCell cell = new TableCell();
Panel panel = new Panel(); // Add your controls to the panel here
nr = GridView1.Rows[i].Cells[0].Text; //id
m = Convert.ToInt32(nr); //id
LinkButton adName = new LinkButton();
adName.Text = nr + ": " + GridView1.Rows[i].Cells[1].Text; //+ ": Name: "
name[i] = GridView1.Rows[i].Cells[1].Text;
panel.Controls.Add(adName);
adName.Click += new EventHandler(this.adName_Click);
ImageMap ImageMap1 = new ImageMap();
ImageMap1.Height = 150;
ImageMap1.Width = 150;
ImageMap1.ImageUrl = GridView1.Rows[i].Cells[11].Text;
image[i] = GridView1.Rows[i].Cells[11].Text;
panel.Controls.Add(ImageMap1);
Label adprice = new Label();
adprice.Text = " pris: " + GridView1.Rows[i].Cells[9].Text + " kr";
panel.Controls.Add(adprice);
Label adSeat = new Label();
adSeat.Text = " Sittande: " + GridView1.Rows[i].Cells[12].Text;
panel.Controls.Add(adSeat);
Label adStand = new Label();
adStand.Text = " Stående: " + GridView1.Rows[i].Cells[13].Text;
panel.Controls.Add(adStand);
Label adbetyg = new Label();
adbetyg.Text = " Betyg: " + bringreview(GridView1.Rows[i].Cells[3].Text);
panel.Controls.Add(adbetyg);
Label adEmail = new Label();
adEmail.Text = " Email: " + GridView1.Rows[i].Cells[3].Text;
Email[i] = GridView1.Rows[i].Cells[3].Text;
//string betyg = bringreview(Email[i]);
adEmail.Visible = false;
panel.Controls.Add(adEmail);
cell.Controls.Add(panel);
row.Cells.Add(cell);
this.form2.Controls.Add(mytable);
col++;
if (col == totalColumns) //a row is filled
{
col = 0;
rows++;
if (rows == totalrows) //all rows all filled (2 rows)
{
rows = 0;
if (mm == 0 && pageindex==0) //items of all pages
{
lblPage.Text = (pageindex+1).ToString();
btnPrev.Visible = false;
btnNext.Visible = false;
}
else if(mm != 0 && pageindex == 0)
{
lblPage.Text = (pageindex + 1).ToString();
//lnkbNext.Text = "Next";
btnPrev.Visible = false;
btnNext.Visible = true;
}
else if(mm != 0 && pageindex > 0 && pageindex < mm + 1)
{
lblPage.Text = (pageindex + 1).ToString();
//lnkbNext.Text = "Next";
//lnkbPrev.Text = "Previous";
btnPrev.Visible = true;
btnNext.Visible = true;
}
else if (mm != 0 && pageindex == mm + 1)
{
lblPage.Text = (pageindex + 1).ToString();
//lnkbNext.Text = "Next";
//lnkbPrev.Text = "Previous";
btnPrev.Visible = true;
btnNext.Visible = false;
}
}
}
}
}
protected void btnPrev_Click(object sender, EventArgs e)
{
Pindex--; //it becomes -1 becasue Pindex remains 0
BindGridView(0);
FillPage(Pindex, Psize);
}
protected void btnNext_Click(object sender, EventArgs e)
{
Pindex++; //it becomes 1 becasue Pindex remains 0
BindGridView(0);
FillPage(Pindex, Psize);
}
private void adName_Click(object sender, EventArgs e)
{
LinkButton adName = sender as LinkButton;
var s1 = adName.Text.Split(':');
Session["nr"] = s1[0];
Session["Image"] = image[Convert.ToInt32(s1[0]) - 1];
Server.Transfer("adpage.aspx");
}
}
}
Tuhin PaulPosted Oct 13, 2024, 3:42 AM
Attach the event handler for the
LinkButton.Tuhin PaulPosted Oct 13, 2024, 3:40 AM
When you dynamically create controls (such as
LinkButton), their events may not fire on postbacks unless they are recreated during every page lifecycle. This can happen because dynamic controls are not automatically persisted across postbacks, and the event handlers need to be reattached.In your
FillPagemethod, you are creatingLinkButtoncontrols dynamically, and the click event for theLinkButtonneeds to be correctly wired up. Here's a revised version of your method to ensure that the event handler is attached and the controls are correctly recreated on every postback.Naimish MakwanaPosted Sep 10, 2024, 2:41 PM
It looks like you’re facing two main issues with your GridView and pagination. Let’s address them one by one.
1. LinkButton Click Event Not Firing
When you change the layout to a matrix form, the
LinkButtonclick event might not be firing due to the dynamic creation of controls. Ensure that theLinkButtonis recreated on every postback and that the event handler is correctly attached. Here’s a revised version of yourFillPagemethod:2. Page Index Not Updating
The issue with the page index not updating could be due to the way the global variable is being handled. Ensure that the page index is being correctly incremented or decremented in the button click events. Here’s an example of how you might handle the
NextandPreviousbutton clicks:Make sure that
Pindexis not being reset to 0 on every postback. You might need to store it inViewStateorSessionto maintain its value across postbacks:Summary
LinkButtonclick event is correctly attached when dynamically creating controls.Pindexvalue across postbacks usingViewStateorSession.Thanks