Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2311
  • Last Modified:

CheckBox in a TemplateField of Gridview

I need to know the ID of the row selected. I have put a CommandArgument in Binding the ID but I get the below error.

CommandArgument' is not a valid property on 'CheckBox
<asp:TemplateField HeaderText="checks" ShowHeader="False">
	<ItemTemplate>
			<table border="0" cellspacing="0" cellpadding="0">
  				<tr>
    				<td align="left">
        <asp:CheckBox ID="chkStatement" runat="server" Checked='<%# Bind("bStatement") %>' CssClass="textBold" CommandArgument='<%# Bind("ID") %>' CommandName="UpdateStatement" Text="Statement" AutoPostBack="True" />
      				</td>
  				</tr>
  				<tr>
    		         </table>
	</ItemTemplate>
</asp:TemplateField>

        If e.CommandName = "UpdateStatement" Then
            Dim chkStatement As CheckBox = TryCast(gvCases.FindControl("chkStatement"), CheckBox)
            Dim index As Integer = Convert.ToInt32(gvCases.EditIndex)
            Dim ExhibitID As Integer = Val(e.CommandArgument)
            Dim sUpdate As String = "UPDATE tExhibits SET bStatement = @bStatement WHERE ID = @ID"
            Dim cmdUpdate As New Data.SqlClient.SqlCommand(sUpdate, myConn.SQLConn)
            cmdUpdate.Parameters.AddWithValue("@bStatement", chkStatement.Checked)
            cmdUpdate.Parameters.AddWithValue("@ID", ExhibitID)
            myConn.SQLConn.Open()
            cmdUpdate.ExecuteNonQuery()
            myConn.SQLConn.Close()
        End If

Open in new window

0
mgordon-spi
Asked:
mgordon-spi
  • 3
  • 3
1 Solution
 
Tuyau2poilCommented:
That's true : commandargument is'nt a property of checkbox.

simply put ID in a hidden textbox "Hidden_TB" in your existing template field and get it by doing :
Dim MyTB As TextBox = CType(e.Row.FindControl("Hidden_TB"), TextBox)          
and don't use gridview ref : use e.row instead :
the correct syntax would be :

 If e.CommandName = "UpdateStatement" Then
            Dim chkStatement As CheckBox = TryCast(e.Row..FindControl("chkStatement"), CheckBox)
            Dim index As Integer =e.Row.rowindex
           Dim ExhibitID As Integer = CType(e.Row.FindControl("Hidden_TB"), TextBox)
            Dim sUpdate As String = "UPDATE tExhibits SET bStatement = @bStatement WHERE ID = @ID"
            Dim cmdUpdate As New Data.SqlClient.SqlCommand(sUpdate, myConn.SQLConn)
            cmdUpdate.Parameters.AddWithValue("@bStatement", chkStatement.Checked)
            cmdUpdate.Parameters.AddWithValue("@ID", ExhibitID)
            myConn.SQLConn.Open()
            cmdUpdate.ExecuteNonQuery()
            myConn.SQLConn.Close()
        End If
0
 
Tuyau2poilCommented:
I do a mistake  on " Dim ExhibitID As Integer = CType(e.Row.FindControl("Hidden_TB"), TextBox) "

right syntax is :  Dim ExhibitID As Integer = val(CType(e.Row.FindControl("Hidden_TB"), TextBox).text)

sorry !
0
 
mgordon-spiAuthor Commented:
I get the following error when putting in e.row

'Row' is not a member of 'System.Web.UI.WebControls.GridViewCommandEventArgs'
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
mgordon-spiAuthor Commented:
I have worked my way around it by using an OnCheckedChanged event and using DataKeys

       
<asp:CheckBox ID="chkStatement" runat="server" Checked='<%# Bind("bStatement") %>' OnCheckedChanged="chkStatement_CheckedChanged" CssClass="textBold" CommandName="UpdateStatement" Text="Statement" AutoPostBack="True" />


    Protected Sub chkItem_CheckedChanged(ByVal objSender As Object, ByVal e As System.EventArgs)
        Dim chkItem As CheckBox = TryCast(gvCases.FindControl("chkItem"), CheckBox)
        Dim checkbox As CheckBox = CType(objSender, CheckBox)
        Dim row As GridViewRow = CType(checkbox.NamingContainer, GridViewRow)
        Dim ExhibitID As Integer = gvCases.DataKeys(row.DataItemIndex).Value
        Dim sUpdate As String = "UPDATE tExhibits SET bItem = @bItem WHERE ID = @ID"
        Dim cmdUpdate As New Data.SqlClient.SqlCommand(sUpdate, myConn.SQLConn)
        cmdUpdate.Parameters.AddWithValue("@bItem", checkbox.Checked)
        cmdUpdate.Parameters.AddWithValue("@ID", ExhibitID)
        myConn.SQLConn.Open()
        cmdUpdate.ExecuteNonQuery()
        myConn.SQLConn.Close()
    End Sub

Open in new window

0
 
Tuyau2poilCommented:
My code was fully buggy, sorry, it was night !
but your Code don't work too : hou must delete "commandname" from checkbox as it's not a property of that control and you must handle chkStatement_CheckedChanged (chkItem_CheckedChanged don't exist in your code).
I don't understand the line :  "Dim chkItem As CheckBox = TryCast(gvCases.FindControl("chkItem"), CheckBox)" : may be there's two checkbox ? chkitem and chkStatement ? anyway you can't get control by parsing gridview, as there is a checkbox in each row... you must first get row from sender
and search control in that row.      

This is a tested and working code, more simple and readable. hope it will help you.

<asp:CheckBox ID="chkStatement" runat="server" Checked='<%# Bind("bStatement") %>' OnCheckedChanged="chkStatement_CheckedChanged" CssClass="textBold" Text="Statement" AutoPostBack="True" />

Protected Sub chkStatement_CheckedChanged(ByVal Sender As Object, ByVal e As System.EventArgs)
   
                Dim row As GridViewRow = CType((CType(Sender, CheckBox)).NamingContainer, GridViewRow)
                Dim chkStatement As CheckBox = CType(row.FindControl("chkStatement"), CheckBox)
                Dim ExhibitID As Integer = row.RowIndex
                Dim sUpdate As String = "UPDATE tExhibits SET bItem = @bItem WHERE ID = @ID"
                Dim cmdUpdate As New Data.SqlClient.SqlCommand(sUpdate, myConn.SQLConn)
                cmdUpdate.Parameters.AddWithValue("@bItem", chkStatement.Checked)
                cmdUpdate.Parameters.AddWithValue("@ID", ExhibitID)
               
            End Sub

0
 
mgordon-spiAuthor Commented:
I answered it with my own research
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now