Solved

Checking status of checkbox in datalist

Posted on 2008-10-26
5
402 Views
Last Modified: 2010-04-21
Hi

I'm trying to check status for checked/unchecked in a checkbox wrapped by a datalist control. The aspx code is imple and looks like this:
<asp:DataList
        ID="RolesDataList"
        DataSource="<%# MembershipHelper.GetRoles() %>"
        DataKeyField="RoleName"
        BorderStyle="None"
        RepeatColumns="1"
        RepeatDirection="Horizontal"
        RepeatLayout="Table"
        runat="server"
      >
        <ItemTemplate>
            <asp:CheckBox ID="RoleCheckBox" Text='<%# Eval("RoleName") %>' runat="server" />
        </ItemTemplate>
      </asp:DataList>
---------------------------------------------------
The code behin looks like this:

For Each control As Control In RolesDataList.Controls
                Dim roleCheckBox As CheckBox = control.FindControl("RoleCheckBox")

                If Not roleCheckBox Is Nothing AndAlso roleCheckBox.Checked Then
                    ' Do something
                End If
            Next
-----------------------------------------------------------
The strange thing is that independent if the checbox is checked or not the value is returned as checked=false (I have debugged the page to see). It finds the checkbox items ok and everything but I can not understand why it want show it as checked when it is.

If anyone hase any suggestions I would be grateful.

Peter
0
Comment
Question by:peternordberg
[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
  • 2
5 Comments
 
LVL 6

Expert Comment

by:M3mph15
ID: 22809661
Hi,
Try using the DirectCast mehod. So it becomes
Dim roleCheckBox As CheckBox = DirectCast(control.FindControl("RoleCheckBox"), CheckBox)

-M3mph15
0
 

Author Comment

by:peternordberg
ID: 22811607
Thanks for help!

No, unfortunately that doesn't work either.

Peter
0
 

Author Comment

by:peternordberg
ID: 22813551
Hi agein,

It has something to do with viewstate. The checkboxes gets emptied on postback for some reason. The controls enebleviewstate is not set to false though. Any suggestions?

Peter
0
 
LVL 6

Accepted Solution

by:
M3mph15 earned 500 total points
ID: 22818634
Hi,
Ok i'm highly familar with DataLists but i'm thinking they have like a databind method that is being called on every postback. Also I'm going to assume that there is a column in your datalist that has a unique value in it.

Well on the postback what you could do is loop through every row and check if the checkbox is checked, if so then grab the unique value of that row and add it to an arraylist. Then in the Databinding method loop through every row and grab the unique value, see if it matches a unique value in the arraylist if so then make the checkbox checked.

This will record all the records with the checkbox checked, and re check them after a post back making it look like to the uiser that nothing has changed. The same method of recording which records have been checked can be used in the sorting method aswell.
I hope this makes sense
-M3mph15
0
 

Author Closing Comment

by:peternordberg
ID: 31510142
Thanks, I managed to fix it with your information. Peter
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

630 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