asp.net c# 是否选中了复选框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10173290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:44:32  来源:igfitidea点击:

asp.net c# is checkbox checked?

c#asp.net

提问by Jono

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?!

调试时,当我在浏览器中选中复选框时,它永远不会进入 If 语句。想法?!

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

我认为可能还有其他一些影响此的代码如下...

In Page_load I have the following:

在 Page_load 我有以下内容:

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:

newsItem 是我的数据对象,我需要相应地设置复选框选中状态。当点击保存按钮时,我需要根据复选框的选中状态更新 IsDraft 属性:

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.

所以,isDraft = 1 应该等于 checkbox 被选中,否则 checkbox 应该被取消选中。目前,它没有显示这一点。

采纳答案by Jono

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.

对我来说,最终最好的解决方案是创建 2 个单独的页面:1 个用于编辑新闻文章,1 个用于新的新闻文章。因此,当页面重新加载时,我永远不会处于创建新新闻数据对象的位置。

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.

当按下保存按钮时,两个页面都返回到文章索引列表页面,这似乎可以保存草稿复选框的状态,然后在编辑页面上显示状态。

回答by Habib

Specify event for Button Click

指定按钮单击事件

<asp:Button ID="PublishButton" runat="server" Text="Save" onclick="PublishButton_Click" />

回答by Arion

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

我可以看到你OnClick的按钮上没有。所以像这样:

<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;
            }
}

回答by Rickie Marsh

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.

尝试onclick="PublishButton_Click"在表单上的按钮字段中添加。我不知道它是否有区别,但生成的事件处理程序受保护无效。

回答by Prabu Parthipan

Please replace code as following code..

请将代码替换为以下代码..

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

回答by SnakeOilKings

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).

checkbox.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;
  }
}