raks7777
05-19-2009, 12:02 PM
Hi all!
I am trying to build a web part page in VS2005 using C# and I am quite a beginner (on web part coding, not in C#). The problem that I am facing has to do with the paging of the datagrid that I am using to display my data (I will display my code in the end of this text).
I have used datagrid paging before (in web applications programming), so I am quite aware of the whole procedure. To summarise, I set "AllowPaging=true", "PageSize = 5", "PagerStyle.Mode = PagerMode.NumericPages". Furthermore, within the PageIndexChanged event handler I set dg.CurrentPageIndex = e.NewPageIndex, I provide the datagrid with the dataset and then I databind.
In total I have 9 pages, due to the number of records. Everything works fine when I click on the pages links. Except for two cases.
In the first case, when I am on a page number other than 1 and I click on the link to go to the 1st page, I still see the records of the page I was before, although the link of the 1st page is displayed as clicked.
In the second case, I am on the last page (page number 9 in my case) and when I click on the link of another page I still see the contents of the last page plus the ones of the first page! In other words, the links of the first and the last page work abnormally. Most interesting to note, I noticed while debugging that in both cases the PageIndexChanged event handler is not fired up. Below I display the code that I am using:
namespace XEE.Amoives3.Positions
{
[Guid("........")]
public class wbPositions : System.Web.UI.WebControls.WebParts.WebPart
{
DataGrid DGPositions = new DataGrid();
Button btnAddPosition = new Button();
private string _editUrl = "http://.......";
[WebBrowsable(true), Personalizable(true)]
public string myEditUrl
{
get { return _editUrl; }
set { _editUrl = value; }
}
public wbPositions()
{
btnAddPosition.Width = 100;
btnAddPosition.Text = "Addition";
}
protected override void OnInit(EventArgs e)
{
btnAddPosition.Click += new EventHandler(btnAddPosition_Click);
DGPositions.EditCommand += new DataGridCommandEventHandler(DGPositions_EditCommand);
DGPositions.DeleteCommand += new DataGridCommandEventHandler(DGPositions_DeleteCommand);
DGPositions.PageIndexChanged += new DataGridPageChangedEventHandler(DGPositions_PageIndexChanged);
}
void DGPositions_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
DGPositions.CurrentPageIndex = e.NewPageIndex;
DGPositions.DataSource = DAL.GetPositions();
DGPositions.DataBind();
}
void DGPositions_DeleteCommand(object source, DataGridCommandEventArgs e)
{
..........
..........
.........
}
void DGPositions_EditCommand(object source, DataGridCommandEventArgs e)
{
..............
.............
.............
}
void btnAddPosition_Click(object sender, EventArgs e)
{
............................
}
protected override void CreateChildControls()
{
base.CreateChildControls();
EditCommandColumn editCol = new EditCommandColumn();
editCol.HeaderText = "Edit";
editCol.EditText = "<img src='images/edit.jpg' border='0'>";
DGPositions.Columns.Add(editCol);
TemplateColumn deleteCol = new TemplateColumn();
deleteCol.HeaderText = "Del.";
deleteCol.ItemTemplate = new LinkButtonTemplate();
DGPositions.Columns.Add(deleteCol);
customizeDatagrid();
DGPositions.DataSource = DAL.GetPositions();
DGPositions.DataBind();
Literal ltBreakLine = new Literal();
StringBuilder sb = new StringBuilder();
sb.Append("<br><br><br>");
ltBreakLine.Text = sb.ToString();
//Row1-------------------------------
TableCell Row1Cell1 = new TableCell();
Row1Cell1.Width = 100;
Row1Cell1.Controls.Add(DGPositions);
Row1Cell1.Controls.Add(ltBreakLine);
Row1Cell1.Controls.Add(btnAddPosition);
TableRow Row1 = new TableRow();
Row1.Cells.AddAt(0, Row1Cell1);
Table myTable = new Table();
myTable.Rows.Add(Row1);
this.Controls.Add(myTable);
// TODO: add custom rendering code here.
// Label label = new Label();
// label.Text = "Hello World";
// this.Controls.Add(label);
}
private class LinkButtonTemplate : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
LinkButton btnDelete = new LinkButton();
btnDelete.CommandName = "Delete";
btnDelete.OnClientClick = "return confirm('Are you sure you want to delete this item?');";
btnDelete.Text = "<img src='images/delete.jpg' border='0'>";
container.Controls.Add(btnDelete);
}
}
private void customizeDatagrid()
{
DGPositions.AllowPaging = true;
DGPositions.PageSize = 5;
DGPositions.CellPadding = 4;
DGPositions.ForeColor = System.Drawing.ColorTranslator.FromHtml("#333333");
DGPositions.GridLines = GridLines.None;
DGPositions.FooterStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#507CD1");
DGPositions.FooterStyle.Font.Bold = true;
DGPositions.FooterStyle.ForeColor = System.Drawing.Color.White;
DGPositions.EditItemStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#2461BF");
DGPositions.SelectedItemStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#D1DDF1");
DGPositions.SelectedItemStyle.Font.Bold = true;
DGPositions.SelectedItemStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#333333");
DGPositions.PagerStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#2461BF");
DGPositions.PagerStyle.ForeColor = System.Drawing.Color.White;
DGPositions.PagerStyle.HorizontalAlign = HorizontalAlign.Right;
DGPositions.PagerStyle.Mode = PagerMode.NumericPages;
DGPositions.HorizontalAlign = HorizontalAlign.Center;
DGPositions.AlternatingItemStyle.BackColor = System.Drawing.Color.White;
DGPositions.ItemStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#EFF3FB");
DGPositions.HeaderStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#EFEFEF");
DGPositions.HeaderStyle.Font.Bold = true;
DGPositions.HeaderStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#1C4269");
}
}
}
I am sorry for displaying the whole code, but as I am a beginner in web parts coding I thought that it might be a problem of web part building that I am not aware of.
I would be grateful if someone could help me on this. Thank you in advance.
I am trying to build a web part page in VS2005 using C# and I am quite a beginner (on web part coding, not in C#). The problem that I am facing has to do with the paging of the datagrid that I am using to display my data (I will display my code in the end of this text).
I have used datagrid paging before (in web applications programming), so I am quite aware of the whole procedure. To summarise, I set "AllowPaging=true", "PageSize = 5", "PagerStyle.Mode = PagerMode.NumericPages". Furthermore, within the PageIndexChanged event handler I set dg.CurrentPageIndex = e.NewPageIndex, I provide the datagrid with the dataset and then I databind.
In total I have 9 pages, due to the number of records. Everything works fine when I click on the pages links. Except for two cases.
In the first case, when I am on a page number other than 1 and I click on the link to go to the 1st page, I still see the records of the page I was before, although the link of the 1st page is displayed as clicked.
In the second case, I am on the last page (page number 9 in my case) and when I click on the link of another page I still see the contents of the last page plus the ones of the first page! In other words, the links of the first and the last page work abnormally. Most interesting to note, I noticed while debugging that in both cases the PageIndexChanged event handler is not fired up. Below I display the code that I am using:
namespace XEE.Amoives3.Positions
{
[Guid("........")]
public class wbPositions : System.Web.UI.WebControls.WebParts.WebPart
{
DataGrid DGPositions = new DataGrid();
Button btnAddPosition = new Button();
private string _editUrl = "http://.......";
[WebBrowsable(true), Personalizable(true)]
public string myEditUrl
{
get { return _editUrl; }
set { _editUrl = value; }
}
public wbPositions()
{
btnAddPosition.Width = 100;
btnAddPosition.Text = "Addition";
}
protected override void OnInit(EventArgs e)
{
btnAddPosition.Click += new EventHandler(btnAddPosition_Click);
DGPositions.EditCommand += new DataGridCommandEventHandler(DGPositions_EditCommand);
DGPositions.DeleteCommand += new DataGridCommandEventHandler(DGPositions_DeleteCommand);
DGPositions.PageIndexChanged += new DataGridPageChangedEventHandler(DGPositions_PageIndexChanged);
}
void DGPositions_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
DGPositions.CurrentPageIndex = e.NewPageIndex;
DGPositions.DataSource = DAL.GetPositions();
DGPositions.DataBind();
}
void DGPositions_DeleteCommand(object source, DataGridCommandEventArgs e)
{
..........
..........
.........
}
void DGPositions_EditCommand(object source, DataGridCommandEventArgs e)
{
..............
.............
.............
}
void btnAddPosition_Click(object sender, EventArgs e)
{
............................
}
protected override void CreateChildControls()
{
base.CreateChildControls();
EditCommandColumn editCol = new EditCommandColumn();
editCol.HeaderText = "Edit";
editCol.EditText = "<img src='images/edit.jpg' border='0'>";
DGPositions.Columns.Add(editCol);
TemplateColumn deleteCol = new TemplateColumn();
deleteCol.HeaderText = "Del.";
deleteCol.ItemTemplate = new LinkButtonTemplate();
DGPositions.Columns.Add(deleteCol);
customizeDatagrid();
DGPositions.DataSource = DAL.GetPositions();
DGPositions.DataBind();
Literal ltBreakLine = new Literal();
StringBuilder sb = new StringBuilder();
sb.Append("<br><br><br>");
ltBreakLine.Text = sb.ToString();
//Row1-------------------------------
TableCell Row1Cell1 = new TableCell();
Row1Cell1.Width = 100;
Row1Cell1.Controls.Add(DGPositions);
Row1Cell1.Controls.Add(ltBreakLine);
Row1Cell1.Controls.Add(btnAddPosition);
TableRow Row1 = new TableRow();
Row1.Cells.AddAt(0, Row1Cell1);
Table myTable = new Table();
myTable.Rows.Add(Row1);
this.Controls.Add(myTable);
// TODO: add custom rendering code here.
// Label label = new Label();
// label.Text = "Hello World";
// this.Controls.Add(label);
}
private class LinkButtonTemplate : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
LinkButton btnDelete = new LinkButton();
btnDelete.CommandName = "Delete";
btnDelete.OnClientClick = "return confirm('Are you sure you want to delete this item?');";
btnDelete.Text = "<img src='images/delete.jpg' border='0'>";
container.Controls.Add(btnDelete);
}
}
private void customizeDatagrid()
{
DGPositions.AllowPaging = true;
DGPositions.PageSize = 5;
DGPositions.CellPadding = 4;
DGPositions.ForeColor = System.Drawing.ColorTranslator.FromHtml("#333333");
DGPositions.GridLines = GridLines.None;
DGPositions.FooterStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#507CD1");
DGPositions.FooterStyle.Font.Bold = true;
DGPositions.FooterStyle.ForeColor = System.Drawing.Color.White;
DGPositions.EditItemStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#2461BF");
DGPositions.SelectedItemStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#D1DDF1");
DGPositions.SelectedItemStyle.Font.Bold = true;
DGPositions.SelectedItemStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#333333");
DGPositions.PagerStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#2461BF");
DGPositions.PagerStyle.ForeColor = System.Drawing.Color.White;
DGPositions.PagerStyle.HorizontalAlign = HorizontalAlign.Right;
DGPositions.PagerStyle.Mode = PagerMode.NumericPages;
DGPositions.HorizontalAlign = HorizontalAlign.Center;
DGPositions.AlternatingItemStyle.BackColor = System.Drawing.Color.White;
DGPositions.ItemStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#EFF3FB");
DGPositions.HeaderStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#EFEFEF");
DGPositions.HeaderStyle.Font.Bold = true;
DGPositions.HeaderStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#1C4269");
}
}
}
I am sorry for displaying the whole code, but as I am a beginner in web parts coding I thought that it might be a problem of web part building that I am not aware of.
I would be grateful if someone could help me on this. Thank you in advance.