Gridview Event Procedures

Discussion in 'ASP .Net' started by =?Utf-8?B?U2hlbGRvbiBQZW5uZXI=?=, Nov 12, 2005.

  1. My ASP.Net 2.0 application uses a GridView control containing a template
    consisting of a checkbox and a Text property that I use to store a value to
    pass to a SQL statement in the OnCheckChanged event, whose code is as follows:

    If sender.checked = True Then
    insVal = "'" & Me.Calendar1.SelectedDate.ToShortDateString & "'"
    Else
    insVal = "Null"
    End If

    'Get TaskID
    TaskID = sender.text


    sql = "update tblTask set DateDone = " & insVal & _
    " where TaskID = " & TaskID

    Try
    dbConn.Open()

    'Update database
    dCmd = New SqlCommand(sql, dbConn)
    RowsAffected = dCmd.ExecuteNonQuery

    Catch ex As Exception
    '...

    Finally
    If Not IsNothing(dbConn) Then
    dbConn.Close()
    End If
    End Try

    Me.gridDoList.DataBind()

    This works fine if only a single row's checkbox has been checked. But if
    the user changes the checked/unchecked status of more than one, only the
    first one is processed. I know that I could turn on AutoPostBack causing
    each click on a checkbox to immediately post back to the server, but that is
    slower and causes an unpleasant "jump" in the screen position that I wish to
    avoid.

    Is there a way to have the server process all the changed checkboxes on a
    single postback?
     
    =?Utf-8?B?U2hlbGRvbiBQZW5uZXI=?=, Nov 12, 2005
    #1
    1. Advertising

  2. Hi Sheldon,

    If the step "Me.gridDoList.dataBind()" below in your code is re-binding the
    GridView then you would lose all of the other checkboxes events after you
    have processed the first one of them. Instead you need to move the
    databinding step to another event (e.g. on submit button click event) as I do
    in this sample
    http://www.societopia.net/Samples/DataGrid_ChildControlsEvents.aspx (My
    sample is using a datagrid in ASP.NET 1.1 but the concept is the same)
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Sheldon Penner" wrote:

    > My ASP.Net 2.0 application uses a GridView control containing a template
    > consisting of a checkbox and a Text property that I use to store a value to
    > pass to a SQL statement in the OnCheckChanged event, whose code is as follows:
    >
    > If sender.checked = True Then
    > insVal = "'" & Me.Calendar1.SelectedDate.ToShortDateString & "'"
    > Else
    > insVal = "Null"
    > End If
    >
    > 'Get TaskID
    > TaskID = sender.text
    >
    >
    > sql = "update tblTask set DateDone = " & insVal & _
    > " where TaskID = " & TaskID
    >
    > Try
    > dbConn.Open()
    >
    > 'Update database
    > dCmd = New SqlCommand(sql, dbConn)
    > RowsAffected = dCmd.ExecuteNonQuery
    >
    > Catch ex As Exception
    > '...
    >
    > Finally
    > If Not IsNothing(dbConn) Then
    > dbConn.Close()
    > End If
    > End Try
    >
    > Me.gridDoList.DataBind()
    >
    > This works fine if only a single row's checkbox has been checked. But if
    > the user changes the checked/unchecked status of more than one, only the
    > first one is processed. I know that I could turn on AutoPostBack causing
    > each click on a checkbox to immediately post back to the server, but that is
    > slower and causes an unpleasant "jump" in the screen position that I wish to
    > avoid.
    >
    > Is there a way to have the server process all the changed checkboxes on a
    > single postback?
    >
    >
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Nov 13, 2005
    #2
    1. Advertising

  3. Thank you for your help, Phillip. I will try your suggestion.


    "Phillip Williams" wrote:

    > Hi Sheldon,
    >
    > If the step "Me.gridDoList.dataBind()" below in your code is re-binding the
    > GridView then you would lose all of the other checkboxes events after you
    > have processed the first one of them. Instead you need to move the
    > databinding step to another event (e.g. on submit button click event) as I do
    > in this sample
    > http://www.societopia.net/Samples/DataGrid_ChildControlsEvents.aspx (My
    > sample is using a datagrid in ASP.NET 1.1 but the concept is the same)
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "Sheldon Penner" wrote:
    >
    > > My ASP.Net 2.0 application uses a GridView control containing a template
    > > consisting of a checkbox and a Text property that I use to store a value to
    > > pass to a SQL statement in the OnCheckChanged event, whose code is as follows:
    > >
    > > If sender.checked = True Then
    > > insVal = "'" & Me.Calendar1.SelectedDate.ToShortDateString & "'"
    > > Else
    > > insVal = "Null"
    > > End If
    > >
    > > 'Get TaskID
    > > TaskID = sender.text
    > >
    > >
    > > sql = "update tblTask set DateDone = " & insVal & _
    > > " where TaskID = " & TaskID
    > >
    > > Try
    > > dbConn.Open()
    > >
    > > 'Update database
    > > dCmd = New SqlCommand(sql, dbConn)
    > > RowsAffected = dCmd.ExecuteNonQuery
    > >
    > > Catch ex As Exception
    > > '...
    > >
    > > Finally
    > > If Not IsNothing(dbConn) Then
    > > dbConn.Close()
    > > End If
    > > End Try
    > >
    > > Me.gridDoList.DataBind()
    > >
    > > This works fine if only a single row's checkbox has been checked. But if
    > > the user changes the checked/unchecked status of more than one, only the
    > > first one is processed. I know that I could turn on AutoPostBack causing
    > > each click on a checkbox to immediately post back to the server, but that is
    > > slower and causes an unpleasant "jump" in the screen position that I wish to
    > > avoid.
    > >
    > > Is there a way to have the server process all the changed checkboxes on a
    > > single postback?
    > >
    > >
     
    =?Utf-8?B?U2hlbGRvbiBQZW5uZXI=?=, Nov 13, 2005
    #3
    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. R Millman
    Replies:
    3
    Views:
    434
    Yan-Hong Huang[MSFT]
    Aug 6, 2003
  2. Joel  Thornton

    GridView paging/sorting - will it work with stored procedures?

    Joel Thornton, Feb 17, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    189
    Joel Thornton
    Feb 17, 2005
  3. Roberto Kohler

    Gridview - how to use stored procedures to update

    Roberto Kohler, Dec 12, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    6
    Views:
    711
    pavan kumar
    Dec 7, 2006
  4. mc
    Replies:
    0
    Views:
    736
  5. Joel  Thornton

    GridView paging/sorting - will it work with stored procedures?

    Joel Thornton, Feb 17, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    369
    Joel Thornton
    Feb 17, 2005
Loading...

Share This Page