Logical error with c# datagrid

G

Guest

I've got a really annoying problem with a datagrid. I have an application
which populates a datagrid on the onclick event of a button. The datagrid is
bound to an ArrayList which holds the values. Everything worked perfectly
for me in my test application however when I copied and pasted the code
accross to my main project something strange started to happen. On the
on-click event of the button (cmdExcAdrContinue) the datagrid is populated
and shown in the panel however an extra blank row is created in the datagrid
containing extra values. I stepped through the code and it seems to go
through the cmdExcAdrContinue_Click event twice. It doesn't do this in my
test application so I can't seem to figure it out. its probably something
simple. Has anyone got any ideas????

protected void cmdExcAdrContinue_Click(object sender, System.EventArgs e)
{
ArrayList addresses;
addresses = (ArrayList) ViewState["Addresses"];

Address newAddress = new Address();
newAddress.Address1 = this.txtAddress1.Text.Trim();
newAddress.Address2 = this.txtAddress2.Text.Trim();
newAddress.Address3 = this.txtAddress3.Text.Trim();
newAddress.Address4 = this.txtAddress4.Text.Trim();
newAddress.Address5 = this.txtAddress5.Text.Trim();
newAddress.Address6 = this.txtAddress6.Text.Trim();

addresses.Add(newAddress);
ViewState["Addresses"] = addresses;

this.dgSearchAddresses.DataSource = addresses;
this.dgSearchAddresses.DataBind();

//clear down the textboxes
this.txtAddress1.Text = "";
this.txtAddress2.Text = "";
this.txtAddress3.Text = "";
this.txtAddress4.Text = "";
this.txtAddress5.Text = "";
this.txtAddress6.Text = "";

this.pnlExceptionAddress.Visible = false;

LiteralControl li = new LiteralControl("<script
language=JavaScript>document.getElementById('txtHouseNum').focus();</script>");
Page.Controls.Add(li);

checkArrayList();
}

private void Page_Load(object sender, System.EventArgs e)
{
checkArrayList();
ArrayList addresses;

// when the page is first loaded only
if( !IsPostBack )
{
addresses = new ArrayList();
ViewState["Addresses"] = addresses;
}
// on subsequent PostBacks:
else
{
addresses = (ArrayList) ViewState["Addresses"];
if( addresses != null )
{
this.dgSearchAddresses.DataSource =
addresses; this.dgSearchAddresses.DataBind();
LiteralControl li = new LiteralControl("<script
language=JavaScript>document.getElementById('txtHouseNum').focus();</script>");
Page.Controls.Add(li);
}
}
//Hide this two panels to begin with
this.pnlAddressResults.Visible =false;
this.pnlExceptionAddress.Visible =false;
}

private void checkArrayList()
{
if(dgSearchAddresses.Items.Count == 0)
{
this.pnlSearchAddresses.Visible = false;
}
else
{
this.pnlSearchAddresses.Visible = true;
}
}

Thanks for any help anyone can give me
 
S

Scott Allen

Stephen:

Double check the Init event and the <asp:Button> declaration to make
sure the event hasn't been wired up twice.

HTH,

--
Scott
http://www.OdeToCode.com/blogs/scott/

I've got a really annoying problem with a datagrid. I have an application
which populates a datagrid on the onclick event of a button. The datagrid is
bound to an ArrayList which holds the values. Everything worked perfectly
for me in my test application however when I copied and pasted the code
accross to my main project something strange started to happen. On the
on-click event of the button (cmdExcAdrContinue) the datagrid is populated
and shown in the panel however an extra blank row is created in the datagrid
containing extra values. I stepped through the code and it seems to go
through the cmdExcAdrContinue_Click event twice. It doesn't do this in my
test application so I can't seem to figure it out. its probably something
simple. Has anyone got any ideas????

protected void cmdExcAdrContinue_Click(object sender, System.EventArgs e)
{
ArrayList addresses;
addresses = (ArrayList) ViewState["Addresses"];

Address newAddress = new Address();
newAddress.Address1 = this.txtAddress1.Text.Trim();
newAddress.Address2 = this.txtAddress2.Text.Trim();
newAddress.Address3 = this.txtAddress3.Text.Trim();
newAddress.Address4 = this.txtAddress4.Text.Trim();
newAddress.Address5 = this.txtAddress5.Text.Trim();
newAddress.Address6 = this.txtAddress6.Text.Trim();

addresses.Add(newAddress);
ViewState["Addresses"] = addresses;

this.dgSearchAddresses.DataSource = addresses;
this.dgSearchAddresses.DataBind();

//clear down the textboxes
this.txtAddress1.Text = "";
this.txtAddress2.Text = "";
this.txtAddress3.Text = "";
this.txtAddress4.Text = "";
this.txtAddress5.Text = "";
this.txtAddress6.Text = "";

this.pnlExceptionAddress.Visible = false;

LiteralControl li = new LiteralControl("<script
language=JavaScript>document.getElementById('txtHouseNum').focus();</script>");
Page.Controls.Add(li);

checkArrayList();
}

private void Page_Load(object sender, System.EventArgs e)
{
checkArrayList();
ArrayList addresses;

// when the page is first loaded only
if( !IsPostBack )
{
addresses = new ArrayList();
ViewState["Addresses"] = addresses;
}
// on subsequent PostBacks:
else
{
addresses = (ArrayList) ViewState["Addresses"];
if( addresses != null )
{
this.dgSearchAddresses.DataSource =
addresses; this.dgSearchAddresses.DataBind();
LiteralControl li = new LiteralControl("<script
language=JavaScript>document.getElementById('txtHouseNum').focus();</script>");
Page.Controls.Add(li);
}
}
//Hide this two panels to begin with
this.pnlAddressResults.Visible =false;
this.pnlExceptionAddress.Visible =false;
}

private void checkArrayList()
{
if(dgSearchAddresses.Items.Count == 0)
{
this.pnlSearchAddresses.Visible = false;
}
else
{
this.pnlSearchAddresses.Visible = true;
}
}

Thanks for any help anyone can give me
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Why are you calling DataBind() in the postback ?
You only have to bind the datagrid once, after that it keep the values
between each postback. You only rebind it if you make some changes to the
collection.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Stephen said:
I've got a really annoying problem with a datagrid. I have an application
which populates a datagrid on the onclick event of a button. The datagrid is
bound to an ArrayList which holds the values. Everything worked perfectly
for me in my test application however when I copied and pasted the code
accross to my main project something strange started to happen. On the
on-click event of the button (cmdExcAdrContinue) the datagrid is populated
and shown in the panel however an extra blank row is created in the datagrid
containing extra values. I stepped through the code and it seems to go
through the cmdExcAdrContinue_Click event twice. It doesn't do this in my
test application so I can't seem to figure it out. its probably something
simple. Has anyone got any ideas????

protected void cmdExcAdrContinue_Click(object sender, System.EventArgs e)
{
ArrayList addresses;
addresses = (ArrayList) ViewState["Addresses"];

Address newAddress = new Address();
newAddress.Address1 = this.txtAddress1.Text.Trim();
newAddress.Address2 = this.txtAddress2.Text.Trim();
newAddress.Address3 = this.txtAddress3.Text.Trim();
newAddress.Address4 = this.txtAddress4.Text.Trim();
newAddress.Address5 = this.txtAddress5.Text.Trim();
newAddress.Address6 = this.txtAddress6.Text.Trim();

addresses.Add(newAddress);
ViewState["Addresses"] = addresses;

this.dgSearchAddresses.DataSource = addresses;
this.dgSearchAddresses.DataBind();

//clear down the textboxes
this.txtAddress1.Text = "";
this.txtAddress2.Text = "";
this.txtAddress3.Text = "";
this.txtAddress4.Text = "";
this.txtAddress5.Text = "";
this.txtAddress6.Text = "";

this.pnlExceptionAddress.Visible = false;

LiteralControl li = new LiteralControl("<script
language=JavaScript>document.getElementById('txtHouseNum').focus(); said:
Page.Controls.Add(li);

checkArrayList();
}

private void Page_Load(object sender, System.EventArgs e)
{
checkArrayList();
ArrayList addresses;

// when the page is first loaded only
if( !IsPostBack )
{
addresses = new ArrayList();
ViewState["Addresses"] = addresses;
}
// on subsequent PostBacks:
else
{
addresses = (ArrayList) ViewState["Addresses"];
if( addresses != null )
{
this.dgSearchAddresses.DataSource =
addresses; this.dgSearchAddresses.DataBind();
LiteralControl li = new LiteralControl("<script
 
G

Guest

It was something to do with the aspx page. when I deleted the following:
onclick="cmdExceptionAddress_Click" in the html view. I think its a problem
with VS 2002 im not sure. Just wasted 2 hours today working it out. Thanks
for your help.

Scott Allen said:
Stephen:

Double check the Init event and the <asp:Button> declaration to make
sure the event hasn't been wired up twice.

HTH,

--
Scott
http://www.OdeToCode.com/blogs/scott/

I've got a really annoying problem with a datagrid. I have an application
which populates a datagrid on the onclick event of a button. The datagrid is
bound to an ArrayList which holds the values. Everything worked perfectly
for me in my test application however when I copied and pasted the code
accross to my main project something strange started to happen. On the
on-click event of the button (cmdExcAdrContinue) the datagrid is populated
and shown in the panel however an extra blank row is created in the datagrid
containing extra values. I stepped through the code and it seems to go
through the cmdExcAdrContinue_Click event twice. It doesn't do this in my
test application so I can't seem to figure it out. its probably something
simple. Has anyone got any ideas????

protected void cmdExcAdrContinue_Click(object sender, System.EventArgs e)
{
ArrayList addresses;
addresses = (ArrayList) ViewState["Addresses"];

Address newAddress = new Address();
newAddress.Address1 = this.txtAddress1.Text.Trim();
newAddress.Address2 = this.txtAddress2.Text.Trim();
newAddress.Address3 = this.txtAddress3.Text.Trim();
newAddress.Address4 = this.txtAddress4.Text.Trim();
newAddress.Address5 = this.txtAddress5.Text.Trim();
newAddress.Address6 = this.txtAddress6.Text.Trim();

addresses.Add(newAddress);
ViewState["Addresses"] = addresses;

this.dgSearchAddresses.DataSource = addresses;
this.dgSearchAddresses.DataBind();

//clear down the textboxes
this.txtAddress1.Text = "";
this.txtAddress2.Text = "";
this.txtAddress3.Text = "";
this.txtAddress4.Text = "";
this.txtAddress5.Text = "";
this.txtAddress6.Text = "";

this.pnlExceptionAddress.Visible = false;

LiteralControl li = new LiteralControl("<script
language=JavaScript>document.getElementById('txtHouseNum').focus();</script>");
Page.Controls.Add(li);

checkArrayList();
}

private void Page_Load(object sender, System.EventArgs e)
{
checkArrayList();
ArrayList addresses;

// when the page is first loaded only
if( !IsPostBack )
{
addresses = new ArrayList();
ViewState["Addresses"] = addresses;
}
// on subsequent PostBacks:
else
{
addresses = (ArrayList) ViewState["Addresses"];
if( addresses != null )
{
this.dgSearchAddresses.DataSource =
addresses; this.dgSearchAddresses.DataBind();
LiteralControl li = new LiteralControl("<script
language=JavaScript>document.getElementById('txtHouseNum').focus();</script>");
Page.Controls.Add(li);
}
}
//Hide this two panels to begin with
this.pnlAddressResults.Visible =false;
this.pnlExceptionAddress.Visible =false;
}

private void checkArrayList()
{
if(dgSearchAddresses.Items.Count == 0)
{
this.pnlSearchAddresses.Visible = false;
}
else
{
this.pnlSearchAddresses.Visible = true;
}
}

Thanks for any help anyone can give me
 
S

Scott Allen

I believe it's because his textboxes contain filters for a new search
to display in the grid with each post, but it's hard to tell exactly
from the snippet.
 

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

Top