Gridview keeps adding rows when user causes postback

Discussion in 'ASP .Net Datagrid Control' started by postings@alexshirley.com, Oct 6, 2006.

  1. Guest

    Hi

    I have a gridview that has a footer with text fields and an "add"
    linkbutton. This footer allows the user to add new rows to the
    gridview.

    When the user clicks the "add" linkbutton everything works as you would
    expect, a record is added to the gridview. The problem is that if the
    user causes a post back after adding a record (i.e. hitting refresh or
    F5 on the browser) it will add the same record again and again. e.g. If
    the user causes 5 postbacks then 5 duplicate records will be entered.

    I can stop this behaviour by disabling viewstate on the gridview, but
    then the edit functionality of the gridview stops working properly.

    Does anybody have any ideas how I can stop this behaviour?
    Here's the code:

    Protected Sub lnkAdd_Click(ByVal sender As Object, ByVal e As
    System.EventArgs)

    Page.Validate()

    If Page.IsValid Then

    'Add record in the database
    mygridview.DataBind()

    End If

    End Sub


    Many thanks!

    Alex
    , Oct 6, 2006
    #1
    1. Advertising

  2. Niraj Ranka Guest

    Hello,

    Referesh or presing F5 is not postback.

    Just let us know... u r adding row to grid using... server side or client
    side scripting.

    Regards
    --
    Niraj Ranka
    http://dotnetpub.blogspot.com/
    <> wrote in message
    news:...
    > Hi
    >
    > I have a gridview that has a footer with text fields and an "add"
    > linkbutton. This footer allows the user to add new rows to the
    > gridview.
    >
    > When the user clicks the "add" linkbutton everything works as you would
    > expect, a record is added to the gridview. The problem is that if the
    > user causes a post back after adding a record (i.e. hitting refresh or
    > F5 on the browser) it will add the same record again and again. e.g. If
    > the user causes 5 postbacks then 5 duplicate records will be entered.
    >
    > I can stop this behaviour by disabling viewstate on the gridview, but
    > then the edit functionality of the gridview stops working properly.
    >
    > Does anybody have any ideas how I can stop this behaviour?
    > Here's the code:
    >
    > Protected Sub lnkAdd_Click(ByVal sender As Object, ByVal e As
    > System.EventArgs)
    >
    > Page.Validate()
    >
    > If Page.IsValid Then
    >
    > 'Add record in the database
    > mygridview.DataBind()
    >
    > End If
    >
    > End Sub
    >
    >
    > Many thanks!
    >
    > Alex
    >
    Niraj Ranka, Oct 7, 2006
    #2
    1. Advertising

  3. Guest

    Thanks for replying Niraj..

    In a nutshell: server side.

    Thanks

    Alex

    ..
    Niraj Ranka wrote:
    > Hello,
    >
    > Referesh or presing F5 is not postback.
    >
    > Just let us know... u r adding row to grid using... server side or client
    > side scripting.
    >
    > Regards
    > --
    > Niraj Ranka
    > http://dotnetpub.blogspot.com/
    > <> wrote in message
    > news:...
    > > Hi
    > >
    > > I have a gridview that has a footer with text fields and an "add"
    > > linkbutton. This footer allows the user to add new rows to the
    > > gridview.
    > >
    > > When the user clicks the "add" linkbutton everything works as you would
    > > expect, a record is added to the gridview. The problem is that if the
    > > user causes a post back after adding a record (i.e. hitting refresh or
    > > F5 on the browser) it will add the same record again and again. e.g. If
    > > the user causes 5 postbacks then 5 duplicate records will be entered.
    > >
    > > I can stop this behaviour by disabling viewstate on the gridview, but
    > > then the edit functionality of the gridview stops working properly.
    > >
    > > Does anybody have any ideas how I can stop this behaviour?
    > > Here's the code:
    > >
    > > Protected Sub lnkAdd_Click(ByVal sender As Object, ByVal e As
    > > System.EventArgs)
    > >
    > > Page.Validate()
    > >
    > > If Page.IsValid Then
    > >
    > > 'Add record in the database
    > > mygridview.DataBind()
    > >
    > > End If
    > >
    > > End Sub
    > >
    > >
    > > Many thanks!
    > >
    > > Alex
    > >
    , Oct 9, 2006
    #3
  4. Guest

    Oh to add to this:

    <FooterTemplate>
    <asp:LinkButton ID="lnkAdd" runat="server" CausesValidation="False"
    CommandName="Add" OnClick="lnkAdd_Click" Text="Add"></asp:LinkButton>
    </FooterTemplate>

    Runs the server side code.....

    Thanks

    Alex


    Niraj Ranka wrote:
    > Hello,
    >
    > Referesh or presing F5 is not postback.
    >
    > Just let us know... u r adding row to grid using... server side or client
    > side scripting.
    >
    > Regards
    > --
    > Niraj Ranka
    > http://dotnetpub.blogspot.com/
    > <> wrote in message
    > news:...
    > > Hi
    > >
    > > I have a gridview that has a footer with text fields and an "add"
    > > linkbutton. This footer allows the user to add new rows to the
    > > gridview.
    > >
    > > When the user clicks the "add" linkbutton everything works as you would
    > > expect, a record is added to the gridview. The problem is that if the
    > > user causes a post back after adding a record (i.e. hitting refresh or
    > > F5 on the browser) it will add the same record again and again. e.g. If
    > > the user causes 5 postbacks then 5 duplicate records will be entered.
    > >
    > > I can stop this behaviour by disabling viewstate on the gridview, but
    > > then the edit functionality of the gridview stops working properly.
    > >
    > > Does anybody have any ideas how I can stop this behaviour?
    > > Here's the code:
    > >
    > > Protected Sub lnkAdd_Click(ByVal sender As Object, ByVal e As
    > > System.EventArgs)
    > >
    > > Page.Validate()
    > >
    > > If Page.IsValid Then
    > >
    > > 'Add record in the database
    > > mygridview.DataBind()
    > >
    > > End If
    > >
    > > End Sub
    > >
    > >
    > > Many thanks!
    > >
    > > Alex
    > >
    , Oct 9, 2006
    #4
  5. Guest

    Hi Niraj

    Any luck on this?

    Thanks

    Alex

    wrote:
    > Oh to add to this:
    >
    > <FooterTemplate>
    > <asp:LinkButton ID="lnkAdd" runat="server" CausesValidation="False"
    > CommandName="Add" OnClick="lnkAdd_Click" Text="Add"></asp:LinkButton>
    > </FooterTemplate>
    >
    > Runs the server side code.....
    >
    > Thanks
    >
    > Alex
    >
    >
    > Niraj Ranka wrote:
    > > Hello,
    > >
    > > Referesh or presing F5 is not postback.
    > >
    > > Just let us know... u r adding row to grid using... server side or client
    > > side scripting.
    > >
    > > Regards
    > > --
    > > Niraj Ranka
    > > http://dotnetpub.blogspot.com/
    > > <> wrote in message
    > > news:...
    > > > Hi
    > > >
    > > > I have a gridview that has a footer with text fields and an "add"
    > > > linkbutton. This footer allows the user to add new rows to the
    > > > gridview.
    > > >
    > > > When the user clicks the "add" linkbutton everything works as you would
    > > > expect, a record is added to the gridview. The problem is that if the
    > > > user causes a post back after adding a record (i.e. hitting refresh or
    > > > F5 on the browser) it will add the same record again and again. e.g. If
    > > > the user causes 5 postbacks then 5 duplicate records will be entered.
    > > >
    > > > I can stop this behaviour by disabling viewstate on the gridview, but
    > > > then the edit functionality of the gridview stops working properly.
    > > >
    > > > Does anybody have any ideas how I can stop this behaviour?
    > > > Here's the code:
    > > >
    > > > Protected Sub lnkAdd_Click(ByVal sender As Object, ByVal e As
    > > > System.EventArgs)
    > > >
    > > > Page.Validate()
    > > >
    > > > If Page.IsValid Then
    > > >
    > > > 'Add record in the database
    > > > mygridview.DataBind()
    > > >
    > > > End If
    > > >
    > > > End Sub
    > > >
    > > >
    > > > Many thanks!
    > > >
    > > > Alex
    > > >
    , Oct 17, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xavier Osa
    Replies:
    0
    Views:
    616
    Xavier Osa
    Jan 9, 2004
  2. Tim_Mac
    Replies:
    10
    Views:
    13,752
    Ealavan
    May 10, 2010
  3. VijayRama
    Replies:
    2
    Views:
    2,291
    Gregory A. Beamer
    Oct 12, 2009
  4. Gridview keeps adding rows when user causes postback

    , Oct 6, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    744
  5. dev648237923

    Button in GridView causes Invalid postback or callback argument

    dev648237923, Dec 28, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    1,058
    dev648237923
    Dec 28, 2006
Loading...

Share This Page