2

How do I determine if the checkbox is checked or not checked? Very perplexed why this is not working - it is so simple!

On my web form:

<asp:CheckBox ID="DraftCheckBox" runat="server" Text="Save as Draft?" />
<asp:Button ID="PublishButton" runat="server" Text="Save" CssClass="publish" />

Code behind which runs in the click event for my save button:

 void PublishButton_Click(object sender, EventArgs e)
{
    if (DraftCheckBox.Checked)
            {
                newsItem.IsDraft = 1;
            }
}

When debugging it never steps into the If statement when I have the checkbox checked in the browser. Ideas?!

I think there maybe some other code affecting this as follows...

In Page_load I have the following:

PublishButton.Click += new EventHandler(PublishButton_Click);

if (newsItem.IsDraft == 1)
    {
        DraftCheckBox.Checked = true;
    }
    else
    {
        DraftCheckBox.Checked = false;
    }

newsItem is my data object and I need to set the checkbox checked status accordingly. When the save button is hit I need to update the IsDraft property based on the checked status of the checkbox:

void PublishButton_Click(object sender, EventArgs e)
{
    if (IsValid)
    {
        newsItem.Title = TitleTextBox.Text.Trim();
        newsItem.Content = ContentTextBox.Text.Trim();
        if (DraftCheckBox.Checked)
        {
            newsItem.IsDraft = 1;
        }
        else
        {
            newsItem.IsDraft = 0;
        }

        dataContext.SubmitChanges();
    }
}

So, isDraft = 1 should equal checkbox checked, otherwise checkbox should be un-checked. Currently, it is not showing this.

2
  • 1
    the event is linked to which control ?? Commented Apr 16, 2012 at 11:34
  • Yes I have that further up in my code behind: PublishButton.Click += new EventHandler(PublishButton_Click); Commented Apr 16, 2012 at 11:50

6 Answers 6

1

Specify event for Button Click

<asp:Button ID="PublishButton" runat="server" Text="Save" onclick="PublishButton_Click" />
Sign up to request clarification or add additional context in comments.

2 Comments

@Jono, I dont see it in aspx page, or atleast in your posted code on the OP, you haven't specified onclick="PublishButton_Click" in button tag
@Jono, I test it on my computer, and it is working exactly how it is supposed to work. Now you should check newsItem.IsDraft, whether it holds the value of 1 or not, Also Put break point on Click event of the button and right click on the project and select Debug -> Start new Instance. If you are doing right click on the page and view in the browser then it will not stop at the breakpoint
1

What i can see you have not got a OnClick on your button. So like this:

<asp:CheckBox ID="DraftCheckBox" runat="server" Text="Save as Draft?" />
<asp:Button ID="PublishButton" runat="server" OnClick="PublishButton_Click" 
Text="Save" CssClass="publish" />

And then the function should work like it is:

protected void PublishButton_Click(object sender, EventArgs e)
{
    if (DraftCheckBox.Checked)
            {
                newsItem.IsDraft = 1;
            }
}

Comments

0

Please replace code as following code..

void PublishButton_Click(object sender, EventArgs e)
    {
        if (DraftCheckBox.Checked==True)
                {
                    newsItem.IsDraft = 1;
                }
    }

2 Comments

Sorry I dont notice any change.
DraftCheckBox.Checked will return bool, so need to eval for True.
0

Try adding onclick="PublishButton_Click" in the button field on the form. And I don't know if it makes a difference, but generated event handlers are protected void.

1 Comment

I don't see onclick in your button code for the web form. It worked for me. Without onclick, it doesn't know what the name of the event handler is as you can create your own.
0

For me the best solution in the end has been to create 2 separate pages: 1 for editing a news articles & 1 for a new news article. So Ill never then be in the position of a new news data object being created when the page reloads.

Both page return to the article index list page when the save button is pressed and that seems to work with being able to save the state of the draft checkbox and then show the state on the edit page.

Comments

0

The checkbox.checked isn't used in the context you want it to (this is a boolean that if true, will make the checkbox look checked).

What you could do is to use instead a checkboxlist. Then you could do the following:

foreach(Listitem li in CheckBoxList1.Items)
{
  if (li.Selected)
  {
    NewsItem.Isdraft = 1;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.