ASP.NET gridview复选框问题

发布于 2024-11-30 21:19:03 字数 1778 浏览 3 评论 0原文

我正在尝试在gridview中实现一个复选框,

这个复选框的作用是验证一条记录,

当按下这个验证按钮时,所有带有选中复选框的项目都会输入到数据库中

这是我的代码:

    protected void Button1_Click(object sender, EventArgs e)
    {

        foreach (GridViewRow row in GridView1.Rows)
        {

             CheckBox cbox = ((CheckBox)row.FindControl("Verify"));

            if (cbox.Equals(true))
            {
                String DraftsText = ((TextBox)row.FindControl("numDrafts")).Text;
                String TCtext = ((TextBox)row.FindControl("numTC")).Text;

                if (row.RowType == DataControlRowType.DataRow)
                {
                    //Header trs = new Header();
                    // GridView1.Rows[0].FindControl("numTC");
                    if (TCtext != "" && DraftsText != "")
                    {

                        //  try
                        //  {
                        string date = row.Cells[4].Text;

                        DateTime dateTime = Convert.ToDateTime(date);
                        string dateFormatted = dateTime.ToString("d-MMM-yy");

                        string unit = row.Cells[5].Text;
                        string currency = row.Cells[6].Text;
                        string totalFC = row.Cells[7].Text;
                        string totalDC = row.Cells[8].Text;
                        int d = Convert.ToInt32(DraftsText);
                        int tc = Convert.ToInt32(TCtext);


                        hdr = new Header(d, tc, dateFormatted, unit, currency, totalFC, totalDC);
                        hdr.InsertFCTC(hdr);
                    }

                }

            }
        }
    }

我可能会这样做错误的方式,但在 if (cbox.Equals(true)) 它给了我一个例外:对象引用未设置为对象的实例。

知道我能做什么来解决这个问题吗?

非常感谢

I am trying to implement a checkbox within gridview,

The job of this checkbox is to verify a record,

When this verify button is pressed, all items with a checked checkbox are inputted into the database

This is my code:

    protected void Button1_Click(object sender, EventArgs e)
    {

        foreach (GridViewRow row in GridView1.Rows)
        {

             CheckBox cbox = ((CheckBox)row.FindControl("Verify"));

            if (cbox.Equals(true))
            {
                String DraftsText = ((TextBox)row.FindControl("numDrafts")).Text;
                String TCtext = ((TextBox)row.FindControl("numTC")).Text;

                if (row.RowType == DataControlRowType.DataRow)
                {
                    //Header trs = new Header();
                    // GridView1.Rows[0].FindControl("numTC");
                    if (TCtext != "" && DraftsText != "")
                    {

                        //  try
                        //  {
                        string date = row.Cells[4].Text;

                        DateTime dateTime = Convert.ToDateTime(date);
                        string dateFormatted = dateTime.ToString("d-MMM-yy");

                        string unit = row.Cells[5].Text;
                        string currency = row.Cells[6].Text;
                        string totalFC = row.Cells[7].Text;
                        string totalDC = row.Cells[8].Text;
                        int d = Convert.ToInt32(DraftsText);
                        int tc = Convert.ToInt32(TCtext);


                        hdr = new Header(d, tc, dateFormatted, unit, currency, totalFC, totalDC);
                        hdr.InsertFCTC(hdr);
                    }

                }

            }
        }
    }

I might be going at this the wrong way but in the if (cbox.Equals(true))
its giving me an exception: Object reference not set to an instance of an object.

Any idea what i can do to solve this?

Many Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

長街聽風 2024-12-07 21:19:03

这个 if (cbox.Equals(true)) 应该是 if (cbox.Checked)

因为你的 cbox 是一个复选框对象,所以它不能用于比较,因此您必须使用 cbox Checked 属性,它将返回 true/false

This if (cbox.Equals(true)) should be if (cbox.Checked)

Since your cbox is a checkbox object it can't be used to compare, so you have to use the cbox Checked Property, which will return true/false

寂寞美少年 2024-12-07 21:19:03

您收到 NullPointerException,因为未找到建议的复选框!或者直接转换为 CheckBox 类型的实例无法按预期工作。

You receive a NullPointerException because the suggested checkbox wasn't found! Or the direct cast into an instance of type CheckBox doesn't worked as expected.

月棠 2024-12-07 21:19:03

将您的代码更改为类似以下内容并重试:

CheckBox cbox = ((CheckBox)row.FindControl("Verify"));

            if (cbox != null && cbox.Checked)
            {
....
}

Change your code to something like this and retry:

CheckBox cbox = ((CheckBox)row.FindControl("Verify"));

            if (cbox != null && cbox.Checked)
            {
....
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文