Logical error with c# datagrid

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stephen

    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 (cmdExcAdrConti nue) 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 cmdExcAdrContin ue_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 cmdExcAdrContin ue_Click(object sender, System.EventArg s e)
    {
    ArrayList addresses;
    addresses = (ArrayList) ViewState["Addresses"];

    Address newAddress = new Address();
    newAddress.Addr ess1 = this.txtAddress 1.Text.Trim();
    newAddress.Addr ess2 = this.txtAddress 2.Text.Trim();
    newAddress.Addr ess3 = this.txtAddress 3.Text.Trim();
    newAddress.Addr ess4 = this.txtAddress 4.Text.Trim();
    newAddress.Addr ess5 = this.txtAddress 5.Text.Trim();
    newAddress.Addr ess6 = this.txtAddress 6.Text.Trim();

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

    this.dgSearchAd dresses.DataSou rce = addresses;
    this.dgSearchAd dresses.DataBin d();

    //clear down the textboxes
    this.txtAddress 1.Text = "";
    this.txtAddress 2.Text = "";
    this.txtAddress 3.Text = "";
    this.txtAddress 4.Text = "";
    this.txtAddress 5.Text = "";
    this.txtAddress 6.Text = "";

    this.pnlExcepti onAddress.Visib le = false;

    LiteralControl li = new LiteralControl( "<script
    language=JavaSc ript>document.g etElementById(' txtHouseNum').f ocus();</script>");
    Page.Controls.A dd(li);

    checkArrayList( );
    }

    private void Page_Load(objec t sender, System.EventArg s 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.dgSearchAd dresses.DataSou rce =
    addresses; this.dgSearchAd dresses.DataBin d();
    LiteralControl li = new LiteralControl( "<script
    language=JavaSc ript>document.g etElementById(' txtHouseNum').f ocus();</script>");
    Page.Controls.A dd(li);
    }
    }
    //Hide this two panels to begin with
    this.pnlAddress Results.Visible =false;
    this.pnlExcepti onAddress.Visib le =false;
    }

    private void checkArrayList( )
    {
    if(dgSearchAddr esses.Items.Cou nt == 0)
    {
    this.pnlSearchA ddresses.Visibl e = false;
    }
    else
    {
    this.pnlSearchA ddresses.Visibl e = true;
    }
    }

    Thanks for any help anyone can give me
  • Scott Allen

    #2
    Re: Logical error with c# datagrid

    Stephen:

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

    HTH,

    --
    Scott


    On Thu, 28 Oct 2004 07:17:17 -0700, "Stephen"
    <Stephen@discus sions.microsoft .com> wrote:
    [color=blue]
    >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 (cmdExcAdrConti nue) 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 cmdExcAdrContin ue_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 cmdExcAdrContin ue_Click(object sender, System.EventArg s e)
    >{
    > ArrayList addresses;
    > addresses = (ArrayList) ViewState["Addresses"];
    >
    > Address newAddress = new Address();
    > newAddress.Addr ess1 = this.txtAddress 1.Text.Trim();
    > newAddress.Addr ess2 = this.txtAddress 2.Text.Trim();
    > newAddress.Addr ess3 = this.txtAddress 3.Text.Trim();
    > newAddress.Addr ess4 = this.txtAddress 4.Text.Trim();
    > newAddress.Addr ess5 = this.txtAddress 5.Text.Trim();
    > newAddress.Addr ess6 = this.txtAddress 6.Text.Trim();
    >
    > addresses.Add(n ewAddress);
    > ViewState["Addresses"] = addresses;
    >
    > this.dgSearchAd dresses.DataSou rce = addresses;
    > this.dgSearchAd dresses.DataBin d();
    >
    > //clear down the textboxes
    > this.txtAddress 1.Text = "";
    > this.txtAddress 2.Text = "";
    > this.txtAddress 3.Text = "";
    > this.txtAddress 4.Text = "";
    > this.txtAddress 5.Text = "";
    > this.txtAddress 6.Text = "";
    >
    > this.pnlExcepti onAddress.Visib le = false;
    >
    > LiteralControl li = new LiteralControl( "<script
    >language=JavaS cript>document. getElementById( 'txtHouseNum'). focus();</script>");
    > Page.Controls.A dd(li);
    >
    > checkArrayList( );
    >}
    >
    >private void Page_Load(objec t sender, System.EventArg s 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.dgSearchAd dresses.DataSou rce =
    >addresses; this.dgSearchAd dresses.DataBin d();
    >LiteralContr ol li = new LiteralControl( "<script
    >language=JavaS cript>document. getElementById( 'txtHouseNum'). focus();</script>");
    >Page.Controls. Add(li);
    > }
    > }
    >//Hide this two panels to begin with
    > this.pnlAddress Results.Visible =false;
    > this.pnlExcepti onAddress.Visib le =false;
    >}
    >
    >private void checkArrayList( )
    >{
    > if(dgSearchAddr esses.Items.Cou nt == 0)
    > {
    > this.pnlSearchA ddresses.Visibl e = false;
    > }
    > else
    > {
    > this.pnlSearchA ddresses.Visibl e = true;
    > }
    >}
    >
    >Thanks for any help anyone can give me[/color]

    Comment

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

      #3
      Re: Logical error with c# datagrid

      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" <Stephen@discus sions.microsoft .com> wrote in message
      news:EB32E987-2174-440E-B29C-0231C836687C@mi crosoft.com...[color=blue]
      > 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[/color]
      is[color=blue]
      > 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 (cmdExcAdrConti nue) the datagrid is populated
      > and shown in the panel however an extra blank row is created in the[/color]
      datagrid[color=blue]
      > containing extra values. I stepped through the code and it seems to go
      > through the cmdExcAdrContin ue_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 cmdExcAdrContin ue_Click(object sender, System.EventArg s e)
      > {
      > ArrayList addresses;
      > addresses = (ArrayList) ViewState["Addresses"];
      >
      > Address newAddress = new Address();
      > newAddress.Addr ess1 = this.txtAddress 1.Text.Trim();
      > newAddress.Addr ess2 = this.txtAddress 2.Text.Trim();
      > newAddress.Addr ess3 = this.txtAddress 3.Text.Trim();
      > newAddress.Addr ess4 = this.txtAddress 4.Text.Trim();
      > newAddress.Addr ess5 = this.txtAddress 5.Text.Trim();
      > newAddress.Addr ess6 = this.txtAddress 6.Text.Trim();
      >
      > addresses.Add(n ewAddress);
      > ViewState["Addresses"] = addresses;
      >
      > this.dgSearchAd dresses.DataSou rce = addresses;
      > this.dgSearchAd dresses.DataBin d();
      >
      > //clear down the textboxes
      > this.txtAddress 1.Text = "";
      > this.txtAddress 2.Text = "";
      > this.txtAddress 3.Text = "";
      > this.txtAddress 4.Text = "";
      > this.txtAddress 5.Text = "";
      > this.txtAddress 6.Text = "";
      >
      > this.pnlExcepti onAddress.Visib le = false;
      >
      > LiteralControl li = new LiteralControl( "<script
      >[/color]
      language=JavaSc ript>document.g etElementById(' txtHouseNum').f ocus();</script>
      ");[color=blue]
      > Page.Controls.A dd(li);
      >
      > checkArrayList( );
      > }
      >
      > private void Page_Load(objec t sender, System.EventArg s 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.dgSearchAd dresses.DataSou rce =
      > addresses; this.dgSearchAd dresses.DataBin d();
      > LiteralControl li = new LiteralControl( "<script
      >[/color]
      language=JavaSc ript>document.g etElementById(' txtHouseNum').f ocus();</script>
      ");[color=blue]
      > Page.Controls.A dd(li);
      > }
      > }
      > //Hide this two panels to begin with
      > this.pnlAddress Results.Visible =false;
      > this.pnlExcepti onAddress.Visib le =false;
      > }
      >
      > private void checkArrayList( )
      > {
      > if(dgSearchAddr esses.Items.Cou nt == 0)
      > {
      > this.pnlSearchA ddresses.Visibl e = false;
      > }
      > else
      > {
      > this.pnlSearchA ddresses.Visibl e = true;
      > }
      > }
      >
      > Thanks for any help anyone can give me[/color]


      Comment

      • Stephen

        #4
        Re: Logical error with c# datagrid

        It was something to do with the aspx page. when I deleted the following:
        onclick="cmdExc eptionAddress_C lick" 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:
        [color=blue]
        > 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"
        > <Stephen@discus sions.microsoft .com> wrote:
        >[color=green]
        > >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 (cmdExcAdrConti nue) 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 cmdExcAdrContin ue_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 cmdExcAdrContin ue_Click(object sender, System.EventArg s e)
        > >{
        > > ArrayList addresses;
        > > addresses = (ArrayList) ViewState["Addresses"];
        > >
        > > Address newAddress = new Address();
        > > newAddress.Addr ess1 = this.txtAddress 1.Text.Trim();
        > > newAddress.Addr ess2 = this.txtAddress 2.Text.Trim();
        > > newAddress.Addr ess3 = this.txtAddress 3.Text.Trim();
        > > newAddress.Addr ess4 = this.txtAddress 4.Text.Trim();
        > > newAddress.Addr ess5 = this.txtAddress 5.Text.Trim();
        > > newAddress.Addr ess6 = this.txtAddress 6.Text.Trim();
        > >
        > > addresses.Add(n ewAddress);
        > > ViewState["Addresses"] = addresses;
        > >
        > > this.dgSearchAd dresses.DataSou rce = addresses;
        > > this.dgSearchAd dresses.DataBin d();
        > >
        > > //clear down the textboxes
        > > this.txtAddress 1.Text = "";
        > > this.txtAddress 2.Text = "";
        > > this.txtAddress 3.Text = "";
        > > this.txtAddress 4.Text = "";
        > > this.txtAddress 5.Text = "";
        > > this.txtAddress 6.Text = "";
        > >
        > > this.pnlExcepti onAddress.Visib le = false;
        > >
        > > LiteralControl li = new LiteralControl( "<script
        > >language=JavaS cript>document. getElementById( 'txtHouseNum'). focus();</script>");
        > > Page.Controls.A dd(li);
        > >
        > > checkArrayList( );
        > >}
        > >
        > >private void Page_Load(objec t sender, System.EventArg s 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.dgSearchAd dresses.DataSou rce =
        > >addresses; this.dgSearchAd dresses.DataBin d();
        > >LiteralContr ol li = new LiteralControl( "<script
        > >language=JavaS cript>document. getElementById( 'txtHouseNum'). focus();</script>");
        > >Page.Controls. Add(li);
        > > }
        > > }
        > >//Hide this two panels to begin with
        > > this.pnlAddress Results.Visible =false;
        > > this.pnlExcepti onAddress.Visib le =false;
        > >}
        > >
        > >private void checkArrayList( )
        > >{
        > > if(dgSearchAddr esses.Items.Cou nt == 0)
        > > {
        > > this.pnlSearchA ddresses.Visibl e = false;
        > > }
        > > else
        > > {
        > > this.pnlSearchA ddresses.Visibl e = true;
        > > }
        > >}
        > >
        > >Thanks for any help anyone can give me[/color]
        >
        >[/color]

        Comment

        • Scott Allen

          #5
          Re: Logical error with c# datagrid

          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


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

          Comment

          Working...