Hey,
Struggling for a few days now.
I have a piece of code (see below). It is an array bound to an asp-datagrid.
There are 3 objects in the array, so 3 items in the datagrid.
I select the second item (Object2) and delete it out of the array an then
rebind the array to the datagrid.
Then automatically the next item in datagrid is selected (Onject2), I delete
out of the array and rebind the array.
Then nothing is selected in the array, but Object1 is still there. One I
push delete (without selecting the item with a mouse-click) I receive an
error on the test if there is an item selected.
I don't understand what is wrong here. Ok, there is no selecteditem but
therfor I do a test. And that test fails?
Can someone explains me what is wrong in that case.
Thanks a lot.
(this is working code)
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace TestDg
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DGusers;
protected System.Web.UI.WebControls.Button Button2;
ArrayList myarr = new ArrayList();
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
myarr.Add(new Obj("Object1","nhz"));
myarr.Add(new Obj("Object2","pvp"));
myarr.Add(new Obj("Object3","jnj"));
Session.Add("Arr",null);
Session["Arr"] = myarr;
this.DGusers.DataSource = myarr;
this.DGusers.DataBind();
}
myarr = (ArrayList) Session["Arr"];
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
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.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button2_Click(object sender, System.EventArgs e)
{
if (this.DGusers.SelectedItem != null)
{
myarr.RemoveAt(this.DGusers.SelectedIndex);
this.DGusers.DataSource = myarr;
this.DGusers.DataBind();
Session["Arr"] = myarr;
}
}
public class Obj
{
public Obj(string nm, string ini)
{
this.mnaam = nm;
this.minit = ini;
}
private string mnaam;
public string Naam
{
get
{
return mnaam;
}
set
{
mnaam = value;
}
}
private string minit;
public string Initialen
{
get
{
return minit;
}
set
{
minit = value;
}
}
}
}
}