检索 DropDownList“值” (C#/ASP.NET)

发布于 2024-09-28 08:16:35 字数 1099 浏览 0 评论 0原文

在我的页面上,每当更新 DetailsView 时,我都必须审核数据库中的更改。在大多数页面上,这都是完美的,因为只有文本框 - 但是在我现在正在处理的带有下拉列表的页面上,我的代码不起作用。

基本上,代码的作用是捕获 SqlDataSource 的 Updating 事件,创建一个数据读取器以在更新之前查看行的内容,将内容与 SqlDataSource 的 Update 查询中的参数进行比较,如果其中一个已更改为将其记录在审核表的字段中。我的代码摘录如下:

while (reader.Read())
            {
                for (int i = 0; i < reader.FieldCount; i++)
                {
           try {
                        if (reader[i].ToString() != e.Command.Parameters[i].Value.ToString())
                        {
                            fields[i] = reader.GetName(i) + ": " + reader[i].ToString() + " => " + e.Command.Parameters[i].Value.ToString();
                        }
           }
                    catch {
                        if (reader[i].ToString() != "")
                        {
                            fields[i] = reader.GetName(i) + ": " + reader[i].ToString() + " => ";
                        }
                    }


                }
            }

当控件位于下拉列表上时,即使字段不同,它似乎也永远不会“进入”IF。我需要做一些特殊的事情来比较下拉列表的值吗?注意我相信该值在我的表中存储为 int 。

谢谢

On my page, whenever a DetailsView is updated, I must audit the changes into a database. On most pages this works perfect, as there were just textboxes - however on a page I am working on right now, with dropdownlists, my code isn't working.

Basically, what the code does is captures the Updating event of the SqlDataSource, creates a datareader to look at the contents of the row before it's updated, compares the contents to the parameters in the Update query of the SqlDataSource, and if one has changed to log this in a field in the Audit table. An extract of my code is below:

while (reader.Read())
            {
                for (int i = 0; i < reader.FieldCount; i++)
                {
           try {
                        if (reader[i].ToString() != e.Command.Parameters[i].Value.ToString())
                        {
                            fields[i] = reader.GetName(i) + ": " + reader[i].ToString() + " => " + e.Command.Parameters[i].Value.ToString();
                        }
           }
                    catch {
                        if (reader[i].ToString() != "")
                        {
                            fields[i] = reader.GetName(i) + ": " + reader[i].ToString() + " => ";
                        }
                    }


                }
            }

When the control is on a dropdownlist, it seems to never go 'into' the IF even though the fields are different. Do I need to do something special to compare the values of dropdownlists? Note I believe the value is stored as an int in my table.

Thanks

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

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

发布评论

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

评论(1

许仙没带伞 2024-10-05 08:16:35

完全是我的错。命令中有一个额外的参数,我必须以某种方式排除它。

Totally my fault. There was an extra parameter on the command which I had to exclude somehow.

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