检索 DropDownList“值” (C#/ASP.NET)
在我的页面上,每当更新 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
完全是我的错。命令中有一个额外的参数,我必须以某种方式排除它。
Totally my fault. There was an extra parameter on the command which I had to exclude somehow.