Solved

CheckBox in a TemplateField of Gridview

Posted on 2011-03-02
6
2,191 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
In C# Using WebClient method how to post certificate from file with password 3 31
ASP.NET MVC 2 31
IIS redirect 1 65
ASP.NET MVC identity 6 26
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now