i have a simple access DB that contains the table new_members. what i want to do is fill a table with the data from new_members and then choose whether to activate or delete them. what i have done already is used asp:repeater to build the dynamic table and of course it works perfectly, the table looks great. however, all of this is in a form because i want to access the checkboxes for activate/delete. here is an example of my repeater code to give you an idea of what im dealing with:

<ItemTemplate>

<table border="0" width="90%">
    <tr>
        <td width="26%">
	     <#Container.DataItem("first_name")%>
	</td>
	<td width="26%">
            <%#Container.DataItem("last_name")%>
	</td>
	<td width="26%">
	    <%#Container.DataItem("email")%>
	</td>
	<td width="5%" align="center">
	    <asp:CheckBox Runat ="server" ID="activate"></asp:CheckBox>
	</td>
	<td width="5%" align="center">
	    <asp:CheckBox Runat ="server" ID="delete"></asp:CheckBox>
	</td>
    </tr>
</table>

</ItemTemplate>

now i want to access those checkboxes in the codebehind, but i dont know how? is there a better way to do this? im thinking yes ;)

To answer your question: Yes, you can, and here is how:

Your ID is how you use the object in the codebehind. So something like

if activate = false then
... ' Do something
end if

BUT! I am finding this to be an issue with all new coders, and I curse Microsoft for starting this trend, but when coding you should use some Hungarian Notation (or some variation of it) inorder to make you code both logical and readable.

What I mean is this:

Your checkboxes should be named like this: chkActivate & chkDelete respectively. The chk prefix differentiates what you are referring to in your code, and indicates CHECKBOX.

Same as when creating variables:

Dim intValue as Integer ' int prefix indicates you are using an Integer value.
Dim strValue as String ' str prefix indicates you are using a string value

Just some thoughs....

Hope this helped. Happy coding :cool:

i have a simple access DB that contains the table new_members. what i want to do is fill a table with the data from new_members and then choose whether to activate or delete them. what i have done already is used asp:repeater to build the dynamic table and of course it works perfectly, the table looks great. however, all of this is in a form because i want to access the checkboxes for activate/delete. here is an example of my repeater code to give you an idea of what im dealing with:

<ItemTemplate>

<table border="0" width="90%">
    <tr>
        <td width="26%">
	     <#Container.DataItem("first_name")%>
	</td>
	<td width="26%">
            <%#Container.DataItem("last_name")%>
	</td>
	<td width="26%">
	    <%#Container.DataItem("email")%>
	</td>
	<td width="5%" align="center">
	    <asp:CheckBox Runat ="server" ID="activate"></asp:CheckBox>
	</td>
	<td width="5%" align="center">
	    <asp:CheckBox Runat ="server" ID="delete"></asp:CheckBox>
	</td>
    </tr>
</table>

</ItemTemplate>

now i want to access those checkboxes in the codebehind, but i dont know how? is there a better way to do this? im thinking yes ;)

thanks for the reply Paladine but I have changed the table slightly and I'm not sure if what you suggested earlier will still work. chances are you're right and i'm a little confused though :cheesy:

i've attached a screenshot of what my table looks like. what i want it to do is, once i hit the execute button, those on the list will be inserted into the active members table. if any of the delete boxes are checked a message will pop up and ask "are you sure", after clicking yes those will be deleted from the non-active members table.

is this overly complicated? i've done a decent amount of searching and i haven't found any good examples to follow. also, i like the flexablity of the repeater control for my formating but maybe i should use something else? datagrid/datalist?

-5 points for Paladine... you didn't answer his question one bit! All you did was tell him how to rename his variables.

To anwer your question: No. You cannot assign a value to an asp:checkbox control, and later retrieve it in the codebehind. Its a problem with Microsoft. I have been reading several threads about it, (and came across this one), and it looks like the best solution is to create your own custom checkbox control, and use that. That, or use old-school HTML checkbox's, and reference them in the code-behind that way. I am still looking for a more elegant solution ... but have yet to find one.

To answer your question: Yes, you can, and here is how:

Your ID is how you use the object in the codebehind. So something like

if activate = false then
... ' Do something
end if

BUT! I am finding this to be an issue with all new coders, and I curse Microsoft for starting this trend, but when coding you should use some Hungarian Notation (or some variation of it) inorder to make you code both logical and readable.

What I mean is this:

Your checkboxes should be named like this: chkActivate & chkDelete respectively. The chk prefix differentiates what you are referring to in your code, and indicates CHECKBOX.

Same as when creating variables:

Dim intValue as Integer ' int prefix indicates you are using an Integer value.
Dim strValue as String ' str prefix indicates you are using a string value

Just some thoughs....

Hope this helped. Happy coding :cool:

I found a real solution. No old-skool HTML, and no compiling of custom DLLs.

A little preface: I assume you (the reader) know how to code-behind, and how that works. Although, this solution will easily work for any non-codebehind file as well.

Here is the gist of it: You can add any attribute you want to the checkbox control. What we are going to do:

1. When the checkbox control gets populateds from the databind(), we are going to overwrite how that is done, and assign our own custom attribute to the control.

2. When the form does a post-back (submit button, whatever), we are going to read in the custom attribute we assigned to the checkbox control.

My example is done in VB.NET. Here it goes:

First, the function to overwrite the databind function:

Sub rptData_ItemDataBound(source As Object, e As RepeaterItemEventArgs)

    If e.Item.ItemType=ListItemType.Item Or e.Item.ItemType=ListItemType.AlternatingItem Then
      Dim MyCheckBox As CheckBox = CType(e.Item.FindControl("chkRecord"), CheckBox)

      MyCheckBox.Attributes("CustomerID") = e.Item.DataItem("id").ToString()

    End If

  End Sub

There are a couple things going on there:

We first dynamically search for our checkbox control. In my example, my checkbox control is named "chkRecord". Then, we assign a custom attribute "CustomerID" to that control, and get the ID from the database (In my case, the column I am pulling from the database is "id")

The sub name "rptData_ItemDataBound" is named aptly, because my repeater control is named "rptData" and I am overwriting the "ItemDataBound" event. Note: It really doesn't matter what you name the sub, as you can name it whatever you want.

Now, let's look at how I declare my repeater object (this is a simplified version):

<asp:Repeater ID="rptData" OnItemDataBound="rptData_ItemDataBound" runat="server">
  <ItemTemplate>
     <asp:CheckBox ID="chkRecord" runat="server" />
  </ItemTemplate>
</asp:Repeater>

Notice how I tell it OnItemDataBound to call my custom sub. This overwrites the default method. Now, with that in place, all our check boxes have a custom attribute "CustomerID" that we can reference.

So let's reference it. I have a button called "Submit", that when clicked, is set to call this sub:

Sub btnSubmit_clicked(sender As Object, e As System.EventArgs)
    Dim rc AS RepeaterItemCollection = rptData.Items

    For Each Item As RepeaterItem In rc
      Dim MyCheckBox As CheckBox = CType(Item.FindControl("chkRecord"), CheckBox)

      If MyCheckBox.Checked Then HttpContext.Current.Trace.Write("CheckBox Value", MyCheckBox.Attributes("CustomerID"))

    Next

  End Sub

For each object in the repeater collection, I check to see if it is a check box named "chkRecord", and if it is, then I grab it, and read the custom attribute "CustomerID". Now, I am writing it to the Stack Trace, but you can do whatever you want with it. For example:

strSQL = "DELETE FROM table WHERE ID = " & MyCheckBox.Attributes("CustomerID")

Actually I disagree:

His question was this: "choose whether to activate or delete them"

So what he asked, I answered. Testing the innate value of the checkbox will determine the action taken in the codebehind.

But if that is not what you were asking for dru987, I apologize, and hope the code swingheim provided is of some help.

Happy coding.

-5 points for Paladine... you didn't answer his question one bit! All you did was tell him how to rename his variables.

To anwer your question: No. You cannot assign a value to an asp:checkbox control, and later retrieve it in the codebehind. Its a problem with Microsoft. I have been reading several threads about it, (and came across this one), and it looks like the best solution is to create your own custom checkbox control, and use that. That, or use old-school HTML checkbox's, and reference them in the code-behind that way. I am still looking for a more elegant solution ... but have yet to find one.

swingheim, your idea about the repeater is brilliant and I’m trying to implement it. However, it is a little advanced for my level. I can follow what is going on well enough but the changes I make don't seem to work. Of course the repeater loads correctly (i used your exact code =) with exception to renaming a few vars) and I only make slight changes when checking for the checked boxes.

here is what I have done to change the onclick sub:

Sub cmdSubmit_clicked(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim MyConn As OleDbConnection = New OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
        Dim SQL As String
        Dim rptCollection As RepeaterItemCollection = new_members.Items

        For Each Item As RepeaterItem In rptCollection

            Dim MyCheckBox As CheckBox = CType(Item.FindControl("chkDelete"), CheckBox)
            'If MyCheckBox.Checked Then HttpContext.Current.Trace.Write("CheckBox Value", MyCheckBox.Attributes("DeleteUser"))

            If MyCheckBox.Checked Then

                SQL = "DELETE FROM new_members WHERE username = " & MyCheckBox.Attributes("DeleteUser")

                ' Check if connection to DB is already open
                If MyConn.State = ConnectionState.Closed Then
                    MyConn.Open()
                End If

                Dim MyCmd As New OleDbCommand(SQL, MyConn)

                ' Try/Catch...in case DB error
                Try
                    MyCmd.ExecuteNonQuery()
                    MyConn.Close()
                    MyConn.Equals("")

                Catch ex As Exception
                    new_members_error.Text = "Error Connecting to Database!"
                    new_members_error.Style("Color") = "red"
                End Try

            Else
                'insert into members table

            End If

            new_members_error.Text = MyCheckBox.Attributes("DeleteUser")

        Next

        Response.Redirect("default.aspx")

    End Sub

What happens when i click the submit button, the page will just refresh. Even after i added the redirect at the bottom to try and send it to another page. Also, I tried to check if it is reading the checkbox.attributes correctly by adding a label called new_members_error and trying to write the value to see what is going on but nothing is printed in the label from it.

Any ideas as to what the heck I'm trying to do? ;)

And also I just want to add that this forum has been an exceptional help to me and my learning process. Keep up the good work!

hi swingheim,
your solution is great, it works fine :)
thanks,
venkatesh

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.