Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to create a Multi Select dropdown list custom control inheriting from the checkboxlist control. it has a title property which will be set as the default text of the dropdown control rendered in a div tag with an image to show dropdown icon as in the code below. I also have a javascript function embedded which will attach event listener to the rendered checkboxlist checkboxes. What i want to do is, when the checkbox checked state is changed the Title property should be set from the javascript . or a c# method should be called so that Title property can be set. Please guide me on how this can be done.

my custom control code
C#
[ToolboxData("<{0}:CheckBoxDropDown  runat="server"></{0}:CheckBoxDropDown>")]
    public class CheckBoxDropDown : CheckBoxList
    {
public string Title { get; set; }

 protected override void Render(HtmlTextWriter writer)
        {
 string divFirstRow = @" <div id='{3}'> {0} <img id=""{1}"" src=""{2}""/></div>";

            writer.WriteLine(string.Format("<div class=\"{0}\" >", this.CssClass));

 writer.Write(string.Format(divFirstRow, this.Title + "  ",
        base.ClientID + "_arrowDown", ImageURL, base.ClientID + "_Title"));
            writer.WriteLine();

base.Render(writer); //render the checkboxlist under the div

            writer.WriteLine("</div>");


this is my javascript code that is rendered from the custom control.

JavaScript
window.onload = function () {
            for ( i = 0; i < checkbox.length; i++) {
                checkbox[i].attachEvent(""onclick"", checkChanged );
            }
        }


            function checkChanged() {
                strTitle='';
             for ( i = 0; i < checkbox.length; i++) {
                if(checkbox[i].checked==true)
                {
                      strTitle+=checkbox[i].value+ ";";
                }
                }
                CHK.Title=strTitle; // This has error
            }


I get the selected values in strTitle. but not able to set it as the Title property.
Posted
Updated 8-Jan-15 22:58pm
v2

1 solution

In your Render method you could add the title element with a specific id to reference from JavaScript using getElementById. Also for each checkbox or select control you can add an onchange event attribute that will call the required JavaScript function when the selection changes. The onchange attribute could be added text string within your Render method. Also on MSDN look up:

AddAttributesToRender
 
Share this answer
 
Comments
Malayali Coder 12-Jan-15 1:35am    
I am not sure i understand you. can you share a sample code based on my code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900