Click here to Skip to main content
15,907,910 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when i run the following code i get the error like

The event 'System.Web.UI.WebControls.Button.Click' can only appear on the left hand side of += or -= C:\Documents and Settings\cmsdev3\My Documents\Visual Studio 2005\Projects\CreatingCheckBoxList\CreatingCheckBoxList\CheckBoXList.aspx.cs 40 25 CreatingCheckBoxList

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

protected void Button1_Click(object sender, EventArgs e)
{
    if (Button1.Click)
    {
        if (isCheckboxSelected())
        {
            string.Format(" completion is successful");
        }
        else
        {
            string.Format(" completion is not successful please select each check box");
        }
    }
}
Posted
Updated 16-Aug-11 4:21am
v2

That's because you're trying to use an event in some bizarre way (I have no idea what you're expecting to happen).

Remove the line if (Button1.Click), and see if that doens't work better for you.
 
Share this answer
 
it is because of your first if statement under button click,

if (Button1.Click)
{
}

remove that 'if' condition. It should work.
 
Share this answer
 
why are you setting it like if(Button1.Click)

If you want to perform any action, just do it like this..

C#
protected void Button1_Click(object sender, EventArgs e)
{
    //if (Button1.Click)
    //{
        if (isCheckboxSelected())
        {
            string.Format(" completion is successful");
        }
        else
        {
            string.Format(" completion is not successful please select each check box");
        }
    //}
}


Button1_Click itself means that you are telling it to do something on the button click event. So no need to add something like Button1.Click

NOTE:
u can use this Button1.Click in those cases when you want the same work to do on the action of some other event, like

protected void Button2_Focus(object sender, EventArgs e)
{
Button1.Click();
}

Now the focus on button2 make it call to the button1 click event routine..
 
Share this answer
 
Comments
pratheep7 16-Aug-11 10:50am    
when user click the submit button i want that validation happen and show the messages.
Button.Click means you want to create EventHandler for your button click event. Button.Click is not a conditional statement nor an expression so it will not work with the 'if'. I believe you've gone pass this problem, I just want to add reason(s) why your code did not work
 
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