Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update data after checkbox value is changed 1

Status
Not open for further replies.

vituja123

Programmer
Jun 16, 2005
110
0
0
US
Hi All,

I have a nice working datagrid with a checkbox for each row. I also set the value of the checkbox depending on the data.

But I also want to allow the user to check/uncheck a box and have it post back that value (update) the database.

I bind the checkbox with this code:

Sub ComputeSumAndBindAccept(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
'First, make sure we are dealing with an Item or AlternatingItem
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then
'Snip out the ViewCount
Dim chkB As CheckBox = New CheckBox
chkB = CType(e.Item.FindControl("AcceptCheck"), CheckBox)
If e.Item.DataItem("userwants") = "Y" Then
chkB.Checked = True
Dim viewCount As Integer = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "Bid"))
viewCountSum += viewCount
Else
chkB.Checked = False
End If

ElseIf e.Item.ItemType = ListItemType.Footer Then
e.Item.Cells(3).Text = "Total: "
e.Item.Cells(3).HorizontalAlign = HorizontalAlign.Right
e.Item.Cells(4).Text = "$" & String.Format("{0:#,###}", viewCountSum)
e.Item.Cells(4).Font.Bold = True
e.Item.Cells(4).ForeColor = Color.Red
End If
End Sub


OK, so now I want to convert the checkbox value from 0,1 to Y/N and write it back to the db.

The question is should I use client side Javascript or have the checkbox autopostback and update the db that way?

First, I cannot seem to trap the checkbox event on the codebehind page.

Any help would be appreicated. Thanks.
 
here's what I've done...
this is my checkbox in the datagrid...
Code:
<ItemTemplate>
 <asp:CheckBox ID="chk" Font-Size="13px" Font-Name="arial" Visible="true" Runat="server" EnableViewState="true"
Enabled="true" OnCheckedChanged="Check_Clicked" AutoPostBack="true"></asp:CheckBox>
</ItemTemplate>

this is my sub to catch it's click event...
Code:
 Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs)
    'This is used in the Top 20 Web Page...so this will make it get the updated data
    Session("dvTop20") = Nothing

    Dim ck1 As CheckBox = CType(sender, CheckBox)
    Dim dgItem As DataGridItem = CType(ck1.NamingContainer, DataGridItem)

    Dim updateMe As New ClientCare.DataUpdater
    Dim myCommand As SqlCommand = New SqlCommand("ClientCare_SPTrackClient")
    myCommand.CommandType = CommandType.StoredProcedure
    myCommand.CommandTimeout = 120
    myCommand.Parameters.Add("@clientID", CInt(dgItem.Cells(0).Text))

    myCommand.Parameters.Add("@track", "0")

    updateMe.returnUpdateStatus(myCommand)
    updateMe.returnUpdateStatusLIVE(myCommand)
    Me.bindGrid("Client Asc")
  End Sub

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Thanks checkai,

That's pretty much as would do it. But I can't seem to get the Check_Click event to fire.

I placed a breakpoint in my sub and the page does the autopostback when i click on a checkbox, but it never seems to fire the sub below:


Sub Row_Change(ByVal sender As Object, ByVal e As EventArgs)
Dim ck1 As CheckBox = CType(sender, CheckBox)
<BREAKPOINT> Dim dgItem As DataGridItem = CType(ck1.NamingContainer, DataGridItem)
End Sub

-------------------------------

<asp:TemplateColumn HeaderText="Accept?" >
<HeaderStyle Width="3px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:CheckBox ID="AcceptCheck" AutoPostBack="true" EnableViewState="true" OnCheckedChanged="Row_Change" Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
 
I still don't understand why the checkbox is not firing the Row_Change sub.

Any ideas would be helpful. Thanks.
 
Hi checkai,

FYI: I was able to get the this to work. I had some code in the page_load which pervented the checkbox call routine from working.

Anyway, in the grid I set a DataKeyField. So in the "Check_Clicked" sub, how do I retrive that value.

This doesn't work:
Dim myDataGridItem As DataGridItem
Dim SelectedTNumber As String = PaymentData.DataKeys(myDataGridItem.ItemIndex)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top