Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox Issue

Status
Not open for further replies.

Esoteric

ISP
Feb 10, 2001
93
US
hey guys I have a problem, don't know what I am doing wrong here. I have a gridview and in that gridview I have a checkbox as below.
Code:
<ItemTemplate>
<asp:checkbox runat="server" id="ITEMCHECKBOX"></asp:checkbox>
</ItemTemplate>

if I check this box it does not change the status. If I change it to this
Code:
<ItemTemplate>
<asp:checkbox runat="server" id="ITEMCHECKBOX" checked></asp:checkbox>
</ItemTemplate>
The status is alwasy checked. I just can't seem to get the state of the checkbox to be correct when its clicked. Visually its perfect. When I run through the items in the gridview and get the status of the control its alwasy whatever the default is in the template. ALWAYS. What am I missing.
 
What is the code you are using to get the status of each checkbox? I belive that is where the problem is.

Jim
 
I just tried rebinding the data before I checked the status of this dreaded checkbox and still nothing.

Code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        GridView1.DataBind()
        Dim row As GridViewRow
        Dim chkSelected As System.Web.UI.WebControls.CheckBox
        Dim strstorename As String
        For Each row In GridView1.Rows
            chkSelected = row.FindControl("ITEMCHECKBOX")
            If (Not chkSelected Is Nothing) Then
                If chkSelected.Checked Then
                    strstorename = row.Cells(1).Text
                    Label2.Text += strstorename
                Else
                    Label2.Text += "Nothing Checked"
                End If
            Else
                Label2.Text += "Control Not Found"
            End If
        Next
    End Sub
 
I'm surprized this would work. You should cast the controlt to a checkbox type. The base Control object doesn't have a check property
Code:
For Each row In GridView1.Rows
   chkSelected = [COLOR=blue]CType([/color]row.FindControl("ITEMCHECKBOX")[COLOR=blue], CheckBox)[/color]
   If (Not chkSelected Is Nothing) Then
      If chkSelected.Checked Then
         strstorename = row.Cells(1).Text
         Label2.Text += strstorename
      Else
         Label2.Text += "Nothing Checked"
      End If
   Else
      Label2.Text += "Control Not Found"
   End If
Next
I'm not sure of the exact Vb syntax for casting objects, so CType argurments may be in the wrong order.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Casting is not necessary, but a good best practice

I actually ran your code with a grid I created and th code works fine for me.
 
Yeah I figured it out. I was Gridview1.Databind() a few to many times in the wrong place. New to this .net so I am learning how to be a "better" programmer :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top