Datagrid problem. Creating extra unwanted row some who??

G

Guest

I have 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;
}
}
 
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.

Stephen said:
I have 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;
}
}
 

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