Solved

CheckBox in a TemplateField of Gridview

Posted on 2011-03-02
6
2,245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

734 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