473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Logical error with c# datagrid

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('txtHo useNum').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('txtHo useNum').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
Nov 16 '05 #1
4 1437
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/

On Thu, 28 Oct 2004 07:17:17 -0700, "Stephen"
<St*****@discussions.microsoft.com> wrote:
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('txtH ouseNum').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('txtH ouseNum').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


Nov 16 '05 #2
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" <St*****@discussions.microsoft.com> wrote in message
news:EB**********************************@microsof t.com...
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('txtHo useNum').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('txtHo useNum').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

Nov 16 '05 #3
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" wrote:
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/

On Thu, 28 Oct 2004 07:17:17 -0700, "Stephen"
<St*****@discussions.microsoft.com> wrote:
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('txtH ouseNum').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('txtH ouseNum').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


Nov 16 '05 #4
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.

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

On Thu, 28 Oct 2004 10:48:30 -0400, "Ignacio Machin \( .NET/ C# MVP
\)" <ignacio.machin AT dot.state.fl.us> wrote:
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,


Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hari Om | last post by:
Here are the details of my error log files: I execute the command and get following message at console: ---------------------------------------------------------------------- ../sqlldr...
14
by: blueboy | last post by:
Hi, I am planning to automate a nighty restore of a DB on another server can someone point me in the right direction with the SQL script to modify the logical file names to the correct path and...
11
by: Dominic Vella | last post by:
I am using MS-Access2000. I can't seem to set the default values for Logical type fields. I start with Dim dbsTmp As Object ' I think it's DAO.Database Set dbsTmp =...
6
by: Hari Om | last post by:
Here are the details of my error log files: I execute the command and get following message at console: ---------------------------------------------------------------------- ../sqlldr...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.