Solved

CheckBox in a TemplateField of Gridview

Posted on 2011-03-02
6
2,150 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

13 Experts available now in Live!

Get 1:1 Help Now