T
Tomek R.
Hello !
I've got weird problem when adding new datagrid item. Here is the situation:
my grid dgDeps is binded to DepartmentList arraylist, stored in Session
between round-trips..
To add new item I press buttton with ibNewDep_Click event.
After that the grid is in edit state and I can modify new item, it has some
text, set as default - "<nowy wydzia³>".
But - this is the weirdness - when I add some text following the default
value
e.g "<nowy wydzial>xyz", the default text is not shown in grid cell after
edit finishes. What I can see is "xyz" !!!!
When I edit the item once again, texbox shows "<nowy wydzial>xyz".
Seems like grid and its datasource are out of sync. They become sync'ed when
I cut off the default text and text entered during edition is the only
contents of that item.
Any suggestions will be appreciated.
I include whole my code, to present the whole situation.
HTML part:
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="LibraryDepartments.ascx.cs"
Inherits="interLAN.DNN.Modules.Library.LibraryDepartments"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<P>
<asp:ImageButton id="ibNewDep" runat="server" ImageUrl="~/images/add.gif"
ToolTip="Nowy wydzia³"></asp:ImageButton>
<aspataGrid id="dgDeps" runat="server" DataKeyField="ItemID"
AutoGenerateColumns="False">
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Zakoñcz"
CancelText="Przerwij" EditText="Edytuj"></asp:EditCommandColumn>
<asp:BoundColumn DataField="Name" SortExpression="Name" HeaderText="Nazwa
wydzia³u"></asp:BoundColumn>
<asp:ButtonColumn Text="Kasuj" ButtonType="PushButton"
CommandName="Delete"></asp:ButtonColumn>
</Columns>
</aspataGrid></P>
Code behind part:
namespace interLAN.DNN.Modules.Library
{
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class LibraryDepartments : UserControl
{
private const string Name_ColumnName="Name";
private const string Delete_ColumnName="Delete";
protected System.Web.UI.WebControls.ImageButton ibNewDep;
protected System.Web.UI.WebControls.DataGrid dgDeps;
private ArrayList DepartmentList
{
get
{
return (ArrayList)Session[ClientID+"_DepartmentList"];
}
set
{
Session[ClientID+"_DepartmentList"]=value;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
LoadData();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ibNewDep.Click += new
System.Web.UI.ImageClickEventHandler(this.ibNewDep_Click);
this.dgDeps.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDeps_CancelComm
and);
this.dgDeps.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDeps_EditComman
d);
this.dgDeps.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDeps_UpdateComm
and);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private InterLANLibraryDepartmentInfo FindListDepById(int depId, ArrayList
depList)
{
foreach (InterLANLibraryDepartmentInfo d in depList)
{
if (d.ItemID==depId)
return d;
}
return null;
}
private InterLANLibraryDepartmentInfo FindListDepById(int depId)
{
return FindListDepById(depId, this.DepartmentList);
}
private int SetNewTempItemId()
{
Random r = new Random(1);
return -r.Next(2,100000000);
}
private void ibNewDep_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
InterLANLibraryDepartmentInfo d = new InterLANLibraryDepartmentInfo();
d.ItemID=SetNewTempItemId();
d.Name="<nowy wydzia³>";
this.DepartmentList.Insert(0,d);
dgDeps.EditItemIndex=this.DepartmentList.IndexOf(d);
BindGrid();
}
private void dgDeps_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgDeps.EditItemIndex=e.Item.ItemIndex;
BindGrid();
}
private void dgDeps_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgDeps.EditItemIndex=-1;
BindGrid();
}
private void dgDeps_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int NameColumnNr = 1;
foreach (DataGridColumn c in dgDeps.Columns)
{
if ((c is BoundColumn) &&
((c as BoundColumn).DataField==Name_ColumnName))
{
NameColumnNr=dgDeps.Columns.IndexOf(c);
break;
}
}
string NameText = ((TextBox)e.Item.Cells[NameColumnNr].Controls[0]).Text;
int depId = (int)dgDeps.DataKeys[e.Item.ItemIndex];
// zmiana warto¶ci w li¶cie
InterLANLibraryDepartmentInfo d = this.FindListDepById(depId);
if (d!=null)
d.Name=NameText;
dgDeps.EditItemIndex=-1;
BindGrid();
}
private void BindGrid()
{
dgDeps.DataSource=this.DepartmentList;
dgDeps.DataBind();
}
public override void LoadData()
{
dgDeps.EditItemIndex=-1;
// bo inaczej grid pozostaje w trakcie edycji
// je¶li wywo³ano by³ tu¿ przed wywo³anie tej metody
this.DepartmentList =
(new InterLANLibraryDepartmentController()).GetAll();
this.BindGrid();
}
}
}
I've got weird problem when adding new datagrid item. Here is the situation:
my grid dgDeps is binded to DepartmentList arraylist, stored in Session
between round-trips..
To add new item I press buttton with ibNewDep_Click event.
After that the grid is in edit state and I can modify new item, it has some
text, set as default - "<nowy wydzia³>".
But - this is the weirdness - when I add some text following the default
value
e.g "<nowy wydzial>xyz", the default text is not shown in grid cell after
edit finishes. What I can see is "xyz" !!!!
When I edit the item once again, texbox shows "<nowy wydzial>xyz".
Seems like grid and its datasource are out of sync. They become sync'ed when
I cut off the default text and text entered during edition is the only
contents of that item.
Any suggestions will be appreciated.
I include whole my code, to present the whole situation.
HTML part:
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="LibraryDepartments.ascx.cs"
Inherits="interLAN.DNN.Modules.Library.LibraryDepartments"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<P>
<asp:ImageButton id="ibNewDep" runat="server" ImageUrl="~/images/add.gif"
ToolTip="Nowy wydzia³"></asp:ImageButton>
<aspataGrid id="dgDeps" runat="server" DataKeyField="ItemID"
AutoGenerateColumns="False">
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Zakoñcz"
CancelText="Przerwij" EditText="Edytuj"></asp:EditCommandColumn>
<asp:BoundColumn DataField="Name" SortExpression="Name" HeaderText="Nazwa
wydzia³u"></asp:BoundColumn>
<asp:ButtonColumn Text="Kasuj" ButtonType="PushButton"
CommandName="Delete"></asp:ButtonColumn>
</Columns>
</aspataGrid></P>
Code behind part:
namespace interLAN.DNN.Modules.Library
{
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class LibraryDepartments : UserControl
{
private const string Name_ColumnName="Name";
private const string Delete_ColumnName="Delete";
protected System.Web.UI.WebControls.ImageButton ibNewDep;
protected System.Web.UI.WebControls.DataGrid dgDeps;
private ArrayList DepartmentList
{
get
{
return (ArrayList)Session[ClientID+"_DepartmentList"];
}
set
{
Session[ClientID+"_DepartmentList"]=value;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
LoadData();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ibNewDep.Click += new
System.Web.UI.ImageClickEventHandler(this.ibNewDep_Click);
this.dgDeps.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDeps_CancelComm
and);
this.dgDeps.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDeps_EditComman
d);
this.dgDeps.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDeps_UpdateComm
and);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private InterLANLibraryDepartmentInfo FindListDepById(int depId, ArrayList
depList)
{
foreach (InterLANLibraryDepartmentInfo d in depList)
{
if (d.ItemID==depId)
return d;
}
return null;
}
private InterLANLibraryDepartmentInfo FindListDepById(int depId)
{
return FindListDepById(depId, this.DepartmentList);
}
private int SetNewTempItemId()
{
Random r = new Random(1);
return -r.Next(2,100000000);
}
private void ibNewDep_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
InterLANLibraryDepartmentInfo d = new InterLANLibraryDepartmentInfo();
d.ItemID=SetNewTempItemId();
d.Name="<nowy wydzia³>";
this.DepartmentList.Insert(0,d);
dgDeps.EditItemIndex=this.DepartmentList.IndexOf(d);
BindGrid();
}
private void dgDeps_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgDeps.EditItemIndex=e.Item.ItemIndex;
BindGrid();
}
private void dgDeps_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgDeps.EditItemIndex=-1;
BindGrid();
}
private void dgDeps_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int NameColumnNr = 1;
foreach (DataGridColumn c in dgDeps.Columns)
{
if ((c is BoundColumn) &&
((c as BoundColumn).DataField==Name_ColumnName))
{
NameColumnNr=dgDeps.Columns.IndexOf(c);
break;
}
}
string NameText = ((TextBox)e.Item.Cells[NameColumnNr].Controls[0]).Text;
int depId = (int)dgDeps.DataKeys[e.Item.ItemIndex];
// zmiana warto¶ci w li¶cie
InterLANLibraryDepartmentInfo d = this.FindListDepById(depId);
if (d!=null)
d.Name=NameText;
dgDeps.EditItemIndex=-1;
BindGrid();
}
private void BindGrid()
{
dgDeps.DataSource=this.DepartmentList;
dgDeps.DataBind();
}
public override void LoadData()
{
dgDeps.EditItemIndex=-1;
// bo inaczej grid pozostaje w trakcie edycji
// je¶li wywo³ano by³ tu¿ przed wywo³anie tej metody
this.DepartmentList =
(new InterLANLibraryDepartmentController()).GetAll();
this.BindGrid();
}
}
}