Solved

CheckBox in a TemplateField of Gridview

Posted on 2011-03-02
6
2,221 Views
Last Modified: 2012-05-11
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
Comment
Question by:mgordon-spi
  • 3
  • 3
6 Comments
 
LVL 4

Expert Comment

by:Tuyau2poil
ID: 35023262
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
 
LVL 4

Expert Comment

by:Tuyau2poil
ID: 35023274
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
 

Author Comment

by:mgordon-spi
ID: 35023295
I get the following error when putting in e.row

'Row' is not a member of 'System.Web.UI.WebControls.GridViewCommandEventArgs'
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Accepted Solution

by:
mgordon-spi earned 0 total points
ID: 35023592
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
 
LVL 4

Expert Comment

by:Tuyau2poil
ID: 35024846
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
 

Author Closing Comment

by:mgordon-spi
ID: 35067792
I answered it with my own research
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question