Search code examples
asp.netvb.netvisual-studio-2008gridviewaspnetdb

how to change the label1 text to block n unblock if checkbox in gridview1 is checked?


I want if checkbox1 in gridview is checked then the label1 text in gridview is Block if checkbox1 in gridview is unchecked then label1 text n gridview is unblock ...

i want to do this because is want .... to block unblock user in ASPNETDB.MDF membership table. ..

enter image description here

whatz wrong in this code :

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
        Dim linkbutton1 As LinkButton = Me.GridView1.SelectedRow.FindControl("LinkButton1")
        Dim chk As CheckBox = Me.GridView1.SelectedRow.FindControl("CheckBox1")
        If chk.Checked = True Then
            linkbutton1.Text = "Block"
            Dim user As MembershipUser = Membership.GetUser(GridView1.SelectedRow.Cells(1).Text.ToString)
            'To block a specific user:
            user.IsApproved = False
            Membership.UpdateUser(user)
        Else
            linkbutton1.Text = "UnBlock"
            Dim user As MembershipUser = Membership.GetUser(GridView1.SelectedRow.Cells(1).Text.ToString)
            'To block a specific user:
            user.IsApproved = True
            Membership.UpdateUser(user)
        End If
    End Sub

Solution

  • Check in the backend if its actually updating the user. If not, then there are bigger issues here. How are you also binding the grid, every page load, or just on the initial page load and during any updates?

    Does the text get changed the first time it posts back, but reverts on future postbacks? It may also be the text switch from block to unblock is not be something that automatically saves to viewstate. Therefore, you may need to reassign whether its blocked or unblocked to the linkbutton text on every postback, in RowCreated...

    Could you provide some more information about what isn't working?

    HTH.