problem when adding new datagrid item

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>
<asp:DataGrid 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>
</asp:DataGrid></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();
}
}
}
 
G

Guest

Hi,

The problem is with the brackets < > , because the grid is just a table in
ASP.NET, so if any string is surrounded by < > brackets, then your browser
will treat it as a HTML tag, eventhough it is wrong, nothing will be
displayed. So replace the brackets <> with someother characters, say for eg.
[]

/Thangam


Tomek R. said:
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>
<asp:DataGrid 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>
</asp:DataGrid></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();
}
}
}
 
Joined
Sep 14, 2006
Messages
6
Reaction score
0
I've a problem where I have a user control which has a exposed variable(text). This control just works fine i.e. inside/outside grid and if page submitted it also retains the state, but when grid is re-binded on a button click event, user control do not display any value.I analysed and found that variable of user controls is set at end after oninit and onload. e.g.

a.aspx
<ItemTemplate>
<ctrl:projectDetail id="ProjectDetail3" runat="server"
ProjectCode='<%#Container.DataItem%>'
/>
</ItemTemplate>
ProjectDetail.ascx.cs
protected override void OnLoad(EventArgs e)
{
lblProjectCode.Text = this.projectcode;
}

If require I can also send complete files, help me i'm getting mad .. (e-mail address removed)
 
Joined
Sep 14, 2006
Messages
6
Reaction score
0
I solved it the following way
ProjectDetail.ascx.cs
private bool updated = false;
public string ProjectCode
{
set { this.projectCode = value;
if ( !updated )
{
update();
updated = true;
}
}
get { return this.projectCode; }
}
protected override void OnLoad(EventArgs e)
{
updated = true;
update();
}
private void update()
{
txtProjectCode.Text = projectCode;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top