views:

22

answers:

2

Hello Developers,

  I have developed a ASP.Net Server custom control in C# for 3.5. named "myCheckBoxList" inherited from CheckBoxList web control.

Working is very simple. It just works as a two option buttons. I have taken a CheckBoxList which will show two fixed checkboxes always. When I check one checkbox then another will uncheck as vise versa.

  This control works perfectly only when I have placed only one instance of that control on web page. If I place more than one instance of control then it doesnt works.

Please, I need solution from expert minds from developers as stackoverflow users.

A: 

Why not use radio buttons? You are trying to recreate the functionality of controls that already exist. You can skin them to look like check boxes if you like, but most users know what radio buttons do and how they are meant to behave.
Try the following. It will recreate the behaviour that you described in your comments. If you need to do this for multiple groups, this will require a currentSelection variable defined for each group and a different class for each group, but there is probably a clever way around this. I'll leave that to you to experiment with. Perhaps wrap it in a control as you have done already and generate the var and the document.ready function for each contriol. Only add the jquery-1.4.1.js reference once though.

<script type="text/javascript" src="/js/jquery-1.4.1.min.js"></script>

<script type="text/javascript" language="javascript">
var currentSelection;
$(document).ready(function() {
    $(".radio").click(function() {
        if (currentSelection == this.id) {
            this.checked = false;
        }
        else {
            currentSelection = this.id;
        }
    });
});
</script>        

<asp:RadioButton id="rb" runat="server" />
<asp:RadioButton id="rb1" runat="server" />

CodeBehind:

 rb.InputAttributes.Add("class", "radio");
 rb1.InputAttributes.Add("class", "radio");
Daniel Dyson
Thank you very much for your reply.Daniel, you are right. But once we have selected anyone option we cannot uncheck again in radio button. Right? I mean if I have two radio buttons, initially both are not uncheck, once I check(select) any one of them I cannot make unselect again. I want that functionality, which I can get with checkbox.
Kamlesh
This could still be done by placing a javascript on the click event of the radiobuttons. If the clicked radio button is already checked, set checked=false; Use JQuery selectors to identify the radiobuttons and hook up the click event to the function. It should be about 5 lines of JQuery. One line if you don't care about readability.
Daniel Dyson
Thank you Daniel to give me your nice suggestions. This is with radio button, but I strictly want to use checkboxlist control. Definatly, your suggestion will be helpful for me in future. Thank you once again.
Kamlesh
A: 

It is become one of the case study for me. I was working to develop a web server custome control in c# for 3.5. This custome control inherited by checkboxlist. I had put a javascript function "ClickMe()" only for one time (This is obvious statement). When I put the instatnce of this control more than one on the web page then at the runtime it create javascript function for each control by same name "ClickMe()" function. this was the bottleneck.

Finally, I decided to write this javascript function seperatly for each control, but on the runtime. for that I concate this.ID with "ClickMe()". (Like this this.ID+"ClickMe()"). And I got the solution.

Kamlesh