Solved

Single select Checkbox in a Repeater control

Posted on 2006-07-04
7
1,631 Views
Last Modified: 2012-05-05
I Have a Checkbox in a repeater control.. like this

<asp:Repeater ID="rptDetails" runat="server" EnableViewState="False" >
                                <ItemTemplate>
                                    <tr>
                                        <td width="1%">
                                            <itemtemplate>
                                                <input id="chkSelected" type="checkbox" value="<%#Container.DataItem("ContactID")%>" name="ItemID" runat="server" />
                        </itemtemplate>
                                        </td>
                                    </tr>
                                    <tr>
                                         <td..... And so on with other values

The user should be able to check only checkbox at a time in this repeater control..
When the user checks a different check box in the repeater control.. then the one which the user selected previously must be unchecked automatically..

Also.. I want to loop through the repeater control.. to see which checkbox the user has checked..

How can i achieve this??


Please help!!!
0
Comment
Question by:Collindsouza
[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
  • 2
7 Comments
 
LVL 16

Assisted Solution

by:Edwin_C
Edwin_C earned 250 total points
ID: 17039950
It is a known problem that the GroupName of radiobutton does not work in databound coontrols such as repeater.  You can use the GroupRadioButton control in http://www.codeproject.com/aspnet/How_group_RButtons.asp to solve the problem.

The article also demonstrate how to read the value of the button.

Hope that helps
Edwin
0
 
LVL 9

Expert Comment

by:zaghaghi
ID: 17040781
1-i created a checkboxlist and bind it to a table
2-handle checkboxlist DataBound event:

    protected void CheckBoxList1_DataBound(object sender, EventArgs e)
    {
        int listItemIndex = 0;
        foreach (ListItem li in CheckBoxList1.Items)
        {
            string script = "clearall('" + CheckBoxList1.ClientID +"',"+
                listItemIndex.ToString()+");";
            li.Attributes.Add("onclick", script);
            listItemIndex++;
        }
    }

3-add this script aspx
this script uncheck all items in a checkboxlist except one that its index is in 2nd parameter

<script language="javascript">
function clearall(checkBoxListId, chk)
{
    objCtrl = document.getElementById(checkBoxListId);
   
    if(objCtrl == null)
    {
        return;
    }

    var i = 0;
    var objItem = null;


   
    for(i = 0; document.getElementById(checkBoxListId + '_' + i) != null; i++)
    {
        objItem = document.getElementById(checkBoxListId + '_' + i);

      if(i!=chk)
              objItem.checked = false;
    }
}
</script>
0
 
LVL 5

Author Comment

by:Collindsouza
ID: 17043463
Hi zaghaghi

Thank you for your reply..

Could you tell me how can i... created a checkboxlist and bind it to a table

Thanks in advance

Collin
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Accepted Solution

by:
zaghaghi earned 250 total points
ID: 17043919
drag and drop a CheckBoxlist on your form, to create a checkboxlist, or use this way to create it programatically:

CheckBoxList cbl = new CheckBoxList();

then create a datasource, and set it to checkboxlist

I use this code in pageload event handler

            CheckBoxList1.DataSource = SqlDataSource1;
            CheckBoxList1.DataTextField = "RoleName";
            CheckBoxList1.DataValueField = "RoleCode";
            CheckBoxList1.DataBind();

if you want I upload my vs2005 solution that can help you more, if you want tell here
0
 
LVL 9

Expert Comment

by:zaghaghi
ID: 17043934
Oops I forget that set Datamember

            CheckBoxList1.DataMember = "tablename";
            CheckBoxList1.DataSource = SqlDataSource1;
            CheckBoxList1.DataTextField = "RoleName";
            CheckBoxList1.DataValueField = "RoleCode";
            CheckBoxList1.DataBind();
0
 
LVL 16

Expert Comment

by:Edwin_C
ID: 17048575
Allowing user to select only one choice from a number of choices is the typical function of radio button.  Why bother to create a checkbox list that resemble the function of radiobutton list?  
0
 
LVL 5

Author Comment

by:Collindsouza
ID: 17048834
Hi Zaghaghi,

I have taken the following steps to resolve this issue..

First :- i have selected a radio button so the user can select only one option at a time..

Second :- i set the value of the radio button as follows

 <input id="rbSelected" type="radio" value='<%# DataBinder.Eval(Container.DataItem,"ContactID") %>' runat="server" />

This value the Radio button has the respective ID..

Third :- on click of select button at the bottom of the page i loop through the repeater control and find out which radio button is checked  
 
   protected void btnSelect_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < rptDetails.Items.Count; i++)
        {
            if (((HtmlInputRadioButton)rptDetails.Items[i].FindControl("rbSelected")).Checked == true)
            {
                string ContactID = ((HtmlInputRadioButton)rptDetails.Items[i].FindControl("rbSelected")).Value;

            }
    }

Now.. so far so good...

Since all of this operation is happening on the pop up screen...

what i want to do now is when the looping is complete in  btnSelect_Click event... I want to refresh the parent page by passing the contactID as part of the query string.. meaning..
1). I now want to Close this pop up screen and i want to pass this contactID whicih i found through looping the repeater control.. to the parent window..
2). Currently the Parent url is...

http://localhost/IP/AddNewIP.aspx

i want the parent url to be http://localhost/IP/AddNewIP.aspx?ContactID='8386'

How can i achive this through the code behind btnSelect_Click
Please help
 
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

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