asp.net C#是选中复选框? [英] asp.net c# is checkbox checked?

查看:70
本文介绍了asp.net C#是选中复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何确定是否复选框被选中还是未选中?
很困惑,为什么这是行不通的 - 它是如此简单

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

在我的Web表单:

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

$ C $其背后c在click事件运行为我保存按钮:

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

我想有可能其他一些code影响本如下...

在Page_Load中,我有以下:

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是我的数据对象,我需要设置的复选框选中状态相应。
当保存按钮被击中我需要根据复选框的选中状态来更新IsDraft属性:

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

所以,isDraft = 1应该等于复选框勾选,否则复选框应该取消检查。目前,它没有显示这一点。

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

推荐答案

有关我到底最好的解决方案已经建立2个独立的网页:1编辑新闻文章和放大器; 1一个新的新闻文章。所以我永远不会再成为一个新的新闻数据对象的位置在创建时重新加载页面。

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.

这两个页​​面返回文章索引列表页面时保存按钮pssed $ P $和似乎能够保存复选框草案的状态,然后显示在编辑页面上的状态下工作。

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.

这篇关于asp.net C#是选中复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆