10/9/09

View Answerers Button



This button must show the list of users who answered the survey. Which survey? The one with its checkbox checked, of course :
This code is for finding which survey is checked (selected)
    private int findCheckedSurvey()
    {
        int survId = -1;
        foreach (RepeaterItem ri in Repeater1.Items)
        {
            CheckBox chkBox = (CheckBox)ri.FindControl("CheckBox1");
            if (chkBox != null && chkBox.Checked)
            {
                HiddenField hf;
                hf = (HiddenField)ri.FindControl("surveyID");
                survId = Convert.ToInt32(hf.Value);
                return survId;
            }
        }
        return survId;

    }


It means that only one survey allowed to be checked at once. How to force user not to check
more than one?
I wrote this code that unchecks the other checkboxes once user checked one of the surveys:
    protected void unCheckOthers(object sender, EventArgs e)
    {
        foreach (RepeaterItem ri in Repeater1.Items)
        {
            CheckBox chkBox = (CheckBox)ri.FindControl("CheckBox1");
            CheckBox c = (CheckBox)sender;
            bool thisIs=false;
            if (c.NamingContainer == chkBox.NamingContainer)
            {
                thisIs = true;
            }
   
           
            if (chkBox != null && chkBox.Checked && !thisIs)
            {
                chkBox.Checked = false;
            }
        }
    }

Note, that all the checkBoxes have the same ID property, so if we want the newly checked checkbox to stay checked we must use NamingContainer property which gets the unic identifier of repeater tags.
Important:If you want your checkboxes CheckedChacnged event to be handled at server code - you must set IsPostBack property to TRUE 

No comments:

Post a Comment

Getting started with docker

It is very simple to get started usig docker. All you need to do-is download the docker desktop for your system Once you get docker syste...