Link to home
Start Free TrialLog in
Avatar of westdh
westdhFlag for United States of America

asked on

asp.net: how to update a checkbox in an editItemTemplate true or false

how to update a checkbox in an editItemTemplate - true or false

<asp:TemplateField ItemStyle-Width="220" ItemStyle-BackColor = "#F4F4F4" ItemStyle-HorizontalAlign="Center" HeaderStyle-BackColor="#A1C1D9" HeaderStyle-Font-Size="9" HeaderText="IsActive">
                        <ItemTemplate>
                              <asp:CheckBox ID="lblIsActive" runat="server" Text='<%# DataBinder.Eval( Container.DataItem, "IsActive")%>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                              <asp:CheckBox ID="lblIsActive" Checked='<%# Eval("IsActive") %>' runat="server" />
                        </EditItemTemplate>         
            </asp:TemplateField> User generated image
Avatar of westdh
westdh
Flag of United States of America image

ASKER

Also how to set it to true if 'isActive' = 'true'
Avatar of SAMIR BHOGAYTA
Hello,

You have to find the checkbox id through for loop and then if comes true from database, it checked = true. Write the code for when the value comes true and false.

Avatar of westdh

ASKER

I am not in a loop: I am in a GridView row and the the Value is True. I want to edit it in EditItemTemplate. I want to set it false.
Avatar of westdh

ASKER

Here is my update proc.

I can add this: ???

  Dim chkIsActive As CheckBox = uxItemDetailGrid.Rows(e.RowIndex).FindControl("chkIsActive")

what else:

what do I add in the sql update
Protected Sub uxItemDetailGrid_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)

        Dim lbl As Label = uxItemDetailGrid.Rows(e.RowIndex).FindControl("lblLocationID")
        Dim txttitle As TextBox = uxItemDetailGrid.Rows(e.RowIndex).FindControl("txtTitle")
        Dim FreeTextBox1 As FreeTextBoxControls.FreeTextBox = uxItemDetailGrid.Rows(e.RowIndex).FindControl("FreeTextBox1")
        Dim txtaddress As TextBox = uxItemDetailGrid.Rows(e.RowIndex).FindControl("txtAddress")
        Dim txtcity As TextBox = uxItemDetailGrid.Rows(e.RowIndex).FindControl("txtCity")
        Dim txtstate As TextBox = uxItemDetailGrid.Rows(e.RowIndex).FindControl("txtState")
        Dim txtzip As TextBox = uxItemDetailGrid.Rows(e.RowIndex).FindControl("txtZip")
        Dim txtcountry As TextBox = uxItemDetailGrid.Rows(e.RowIndex).FindControl("txtCountry")

        Dim txtext2 As TextBox = uxItemDetailGrid.Rows(e.RowIndex).FindControl("txtExt2")
        Dim txtext3 As TextBox = uxItemDetailGrid.Rows(e.RowIndex).FindControl("txtExt3")
        Dim txtext4 As TextBox = uxItemDetailGrid.Rows(e.RowIndex).FindControl("txtExt4")
        Dim txtext5 As TextBox = uxItemDetailGrid.Rows(e.RowIndex).FindControl("txtExt5")
        
        Dim strSelectCommand As String = "update trip4usdnn.Netism_MapExtreme_Locations set title = '" & txttitle.Text & "', description = '" & FreeTextBox1.Text & "', address = '" & txtaddress.Text & "', city = '" & txtcity.Text & "', state = '" & txtstate.Text & "', zip = '" & txtzip.Text & "', country = '" & txtcountry.Text & "', ext2 = '" & txtext2.Text & "', ext3 = '" & txtext3.Text & "', ext4 = '" & txtext4.Text & "', ext5 = '" & txtext5.Text & "'  where LocationID = " & lbl.Text & ""

        Dim dtVolumeOrder As New DataTable()
        Using sqlConn As New SqlConnection(ConfigurationManager.ConnectionStrings("SiteSqlServer").ConnectionString)
            Using adapPatientBills As New SqlDataAdapter(strSelectCommand, sqlConn)
                adapPatientBills.Fill(dtVolumeOrder)
            End Using
        End Using
        'iRowsCount = dtVolumeOrder.Rows.Count - 1
        'dsGridview.DataSource = dtVolumeOrder

        
        uxItemDetailGrid.EditIndex = -1
        dsGridview.DataBind()

        bind()

    End Sub

Open in new window

you want something like this:
Dim chkIsActive As CheckBox = uxItemDetailGrid.Rows(e.RowIndex).FindControl("lblIsActive")

If chk.Checked Then
'IS CHECKED CODE HERE
End if

Open in new window

sorry mistype:
Dim chkIsActive As CheckBox = uxItemDetailGrid.Rows(e.RowIndex).FindControl("lblIsActive")

If chkIsActive.Checked Then
'IS CHECKED CODE HERE
End if

Open in new window

Avatar of westdh

ASKER

what do I do here: 'IS CHECKED CODE HERE


Dim chkIsActive As CheckBox = uxItemDetailGrid.Rows(e.RowIndex).FindControl("lblIsActive")
       
         If chkIsActive.Checked Then
            ??? chkIsActive = true
         Else
            ??? chkIsActive = false
         End if

       
        Dim strSelectCommand As String = "update trip4usdnn.Netism_MapExtreme_Locations set title = '" & txttitle.Text & "', description = '" & FreeTextBox1.Text & "', address = '" & txtaddress.Text & "', city = '" & txtcity.Text & "', state = '" & txtstate.Text & "', zip = '" & txtzip.Text & "', country = '" & txtcountry.Text & "', ext2 = '" & txtext2.Text & "', ext3 = '" & txtext3.Text & "', ext4 = '" & txtext4.Text & "', ext5 = '" & txtext5.Text & "' IsActive = '" & ??????? & "' where LocationID = " & lbl.Text & ""

If chkIsActive.Checked Then
            chkIsActive.Checked = true
         Else
            chkIsActive.Checked = false
         End if

Open in new window

Avatar of westdh

ASKER

I get an error on:  If chkIsActive.Checked Then
object not set to an instance of an object


 Dim chkIsActive As CheckBox = uxItemDetailGrid.Rows(e.RowIndex).FindControl("lblIsActive")
       
         If chkIsActive.Checked Then
         '      chkIsActive.Checked = true  ** commented this out in order to isolate the error
         Else
         '      chkIsActive.Checked = false ** ** commented this out in order to isolate the error

         End if
Try this:

If chkIsActive.CheckState = 1 Then
   chkIsActive.Checked = true
 Else
   chkIsActive.Checked = false
End if

Open in new window

if that does not work try this:

Dim chkIsActive As CheckBox = (CheckBox)uxItemDetailGrid.Rows(e.RowIndex).FindControl("lblIsActive")
Avatar of westdh

ASKER

Got it finnally, Thanks very much for your help
ASKER CERTIFIED SOLUTION
Avatar of disrupt
disrupt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of westdh

ASKER

thanks again