Avatar of westdh
westdh
Flag 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> Checkbox
Visual Basic.NET

Avatar of undefined
Last Comment
westdh

8/22/2022 - Mon
westdh

ASKER
Also how to set it to true if 'isActive' = 'true'
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.

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
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

disrupt

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

disrupt

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
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 & ""
disrupt


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

Open in new window

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
Your help has saved me hundreds of hours of internet surfing.
fblack61
disrupt

Try this:

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

Open in new window

disrupt

if that does not work try this:

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

ASKER
Got it finnally, Thanks very much for your help
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
disrupt

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
westdh

ASKER
thanks again