Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Following code does validation I believe.Now when a submit button is clicked i want the validation to happen and if its true then save the result and show a message to user like" completed successfull" but even if one of the boxes is not checked then return false and show a message like " you have to check each box". Now what should i write in the method protected void Button1_Click(object sender, EventArgs e) ?

C#
bool itischecked = false;
        public bool isCheckboxSelected()
        {
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (!CheckBoxList1.Items[i].Selected)
                {
                    return itischecked;
                    
                }
                 
                    
            }
            itischecked = true;
            return itischecked;
        }

protected void Button1_Click(object sender, EventArgs e)
        {

        }
Posted
Updated 16-Aug-11 3:28am
v2

I would do it like this:
C#
public bool isCheckboxSelected()
{
    foreach (var item in CheckBoxList1.Items)
    {
        if (!item.Selected)
        {
            return false;
        }
    }
    return true;
}

protected void Button1_Click(object sender, EventArgs e)
{
    var result = isCheckboxSelected();
    if (result)
    {
    // do your data update
    }
    else
    {
    // Show your Errormessage
    }
}
 
Share this answer
 
1.
C#
string messageText = isCheckboxSelected() ?
"completed successfull" :
"you have to check each box";

MessageBox.Show(messageText);


2.
C#
if(isCheckboxSelected())
{
MessageBox.Show("completed successfull");
}
else
{
MessageBox.Show("you have to check each box");
}


I think first variant is prefered.
 
Share this answer
 
v2
While your validation check may work there are a few redundancies there. the bool isitchecked is not really necessary. You also do not check whether the Item is a CheckBox. I would try something like this

C#
public bool AreCheckBoxesSelected()
        {
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i] is CheckBox)
                {
                    if (!CheckBoxList1.Items[i].Selected)
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            if (AreCheckBoxesSelected())
            {
                SaveResult();
                MessageBox.Show("Success ..... Yaaaaaaay");
            }
            else
            {
                MessageBox.Show("No Joy. Ensure all Check Boxes are CHECKED please.");
            }
        }


This assumes that you have a Method SaveResult which would save the result?

Hope this helps
 
Share this answer
 
Try this,
C#
//Add a label to your page with color red. say lblErrorMessage.

protected void Button1_Click(object sender, EventArgs e)
{
if(!isCheckboxSelected)
{
lblErrorMessage.Text = "you have to check each box"
return false;
}
else
{
lblErrorMessage.Text = "completed successfully"
}
}
 
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