Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

In my project i am using asp.net and C#. I am having a asp.net calender control and in run time i am adding checkbox to each of the cell of the calender using the following code in the dayrender block.

SQL
CheckBox chkSelect = new CheckBox();
e.Cell.Controls.Add(chkSelect);


And there's a button as btnSave. On click of this button i want to save the selected dates. But i am not able to fin out how do i get the selected dates i.e. checkbox is checked for which date?

It would be really appreciated if somebody helps me in this.
Posted

For the checkboxes to exist in viewstate, you need to add them before page load. Once you've done that, you can find them the same way you put them in there, search through the cells of the control and find them with FindControl.
 
Share this answer
 
But how can i add them before page load?
I mean i have to add the checkboxes to each of the cell in the calender control. So i put the code in DayRender event.

Can u give any example or add more information to this please!!
 
Share this answer
 
Any tutorial in dynamically adding controls will explain it for you. I believe that the LoadViewState method is a good place to inject code prior to page load.
 
Share this answer
 
You can achive this by following way:

Day render event:

void cal_DayRender(object sender, DayRenderEventArgs e)
        {
            CheckBox chk = new CheckBox();
            //Assign date as a ID
            chk.ID = "cal" + e.Day.Date.ToShortDateString();
            //For showing selected/checked checkbox in Postback
            if (!String.IsNullOrEmpty(Request.Form[chk.ID]))
                chk.Checked = true;
            
            e.Cell.Controls.Add(chk);
        }


Button click event

C#
protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string calKeys = "";
            string[] selectedDates=null;

            //Looping through all keys 
            for (int i = 0; i < Request.Form.Keys.Count; i++)
            {
                //Get only keys having "cal" word
                if(Request.Form.Keys[i].ToString().Contains("cal"))
                    calKeys += Request.Form.Keys[i].ToString() + ",";
            }

            if (calKeys != string.Empty)
            {
                calKeys = calKeys.Trim(','); 
               
                selectedDates = calKeys.Split(',');

                for (int i = 0; i < selectedDates.Length; i++)
                {
                    //Removing "cal" word from date.
                    selectedDates[i] = selectedDates[i].Substring(3);
                }
            }

               //Show selected dates in Lable control
               lblMessage.Text = string.Join("," , selectedDates);

        }
 
Share this answer
 

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