及其郁闷的DropDownList数据邦定,怎么办呀?唧唧唧……

lovcal 2006-10-25 09:23:31
我的FormView中用到了DropDownList,并且使用数据邦定
发觉假如数据一有问题,就会提示DropDownList有一个无效的SelectedValue的错误(也就是FormView给DropDownList赋值时,DropDownList中没有这个值)
及其郁闷!

请问大家在使用数据邦定控件比如说FormView时,怎么处理DropDownList的?
...全文
210 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
lhcoolhacker 2006-10-26
  • 打赏
  • 举报
回复
我看到你的短信了,已经回复!另外在cs文件中使用的话加上“名称空间”即可!

如:你的控件的名称空间是:Hugh.Web.UI.WebControls

string sMeiChang_no;
sMeiChang_no = ((Hugh.Web.UI.WebControls.DropDownList)this.FormView1.FindControl("ddlMeiChang")).SelectedValue.ToString();

或者使用别名!
using hwuw = Hugh.Web.UI.WebControls

使用hwuw.DropDownList 代替 Hugh.Web.UI.WebControls.DropDownList
lovcal 2006-10-25
  • 打赏
  • 举报
回复
楼上大哥请问怎么把原来的DropDownList替换了呢?用户自定义控件?还是将那段代码加到我的程序中即可?
我上面是直接加的,下面的ddlMeiChang就是原来的DropDownList,所以转换的时候提示无法将类型为“System.Web.UI.WebControls.DropDownList”的对象强制转换为类型“DropDownList”。

string sMeiChang_no;
sMeiChang_no = ((DropDownList)this.FormView1.FindControl("ddlMeiChang")).SelectedValue.ToString();--->
lovcal 2006-10-25
  • 打赏
  • 举报
回复
默认就是应用了的呀……
superliu1122 2006-10-25
  • 打赏
  • 举报
回复
引如System.Web.UI.WebControls名称空间
HJS1820 2006-10-25
  • 打赏
  • 举报
回复
pd
hegang888888888 2006-10-25
  • 打赏
  • 举报
回复
ding
lovcal 2006-10-25
  • 打赏
  • 举报
回复
======================================
string sMeiChang_no;
sMeiChang_no = ((DropDownList)this.FormView1.FindControl("ddlMeiChang")).SelectedValue.ToString();--->

无法将类型为“System.Web.UI.WebControls.DropDownList”的对象强制转换为类型“DropDownList”。
=======================================
我在代码中的怎么使用正确呢?
superliu1122 2006-10-25
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfsystemint32classparsetopic1.asp
superliu1122 2006-10-25
  • 打赏
  • 举报
回复
那些类是System.ComponentModel里面的
stormwen 2006-10-25
  • 打赏
  • 举报
回复
学习学习,顶一下
lovcal 2006-10-25
  • 打赏
  • 举报
回复
我加在aspx.cs 文件的PageLoad()方法的后面
显示这些错误
=======================================
错误 43 找不到类型或命名空间名称“Browsable”(是否缺少 using 指令或程序集引用?) D:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\O\Order\OrderInfo3.aspx.cs 60 10 D:\...\O\
错误 44 找不到类型或命名空间名称“Description”(是否缺少 using 指令或程序集引用?) D:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\O\Order\OrderInfo3.aspx.cs 60 27 D:\...\O\
错误 45 找不到类型或命名空间名称“Category”(是否缺少 using 指令或程序集引用?) D:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\O\Order\OrderInfo3.aspx.cs 60 48 D:\...\O\
错误 46 找不到类型或命名空间名称“Bindable”(是否缺少 using 指令或程序集引用?) D:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\O\Order\OrderInfo3.aspx.cs 60 70 D:\...\O\
错误 47 找不到类型或命名空间名称“DefaultValue”(是否缺少 using 指令或程序集引用?) D:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\O\Order\OrderInfo3.aspx.cs 60 111 D:\...\O\
错误 48 找不到类型或命名空间名称“DesignerSerializationVisibility”(是否缺少 using 指令或程序集引用?) D:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\O\Order\OrderInfo3.aspx.cs 60 129 D:\...\O\
lovcal 2006-10-25
  • 打赏
  • 举报
回复
太感谢lhcoolhacker(hugh-lin)老兄了,还真能这么做呀?我之前就是想能不能提了或者重载这个控件,看来老兄的方法还真行!

不过,再请教一下,怎么个替法?
ldw701 2006-10-25
  • 打赏
  • 举报
回复
顶一下
lhcoolhacker 2006-10-25
  • 打赏
  • 举报
回复
将该自定义控件,替换微软的dropdownlist进行使用即可!
lhcoolhacker 2006-10-25
  • 打赏
  • 举报
回复
呵呵,以前我也有同样的苦恼。要实现不报错误,要扩展DropDownList控件!代码如下:
/// <summary>
/// 屏蔽被选值不匹配错误的下拉列表控件
/// </summary>
public class DropDownList : System.Web.UI.WebControls.DropDownList
{
/// <summary>
/// 获取或设置被选择的值
/// </summary>
[Browsable(true), Description("被选择值"), Category("Behavior"), Bindable(true, BindingDirection.TwoWay), DefaultValue(""), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Themeable(false)]
public override string SelectedValue
{
get
{
return base.SelectedValue;
}
set
{
try
{
base.SelectedValue = value;
}
catch
{
base.SelectedIndex = -1;
}
}
}

protected override void PerformDataBinding(IEnumerable dataSource)
{
try
{
base.PerformDataBinding(dataSource);
}
catch
{
}
}
}
lovcal 2006-10-25
  • 打赏
  • 举报
回复
没有其他办法了么?
amstar 2006-10-25
  • 打赏
  • 举报
回复
先判断数据源有没有问题再手动绑定。如果有问题,在dropdownlist上打条信息:没有数据!!

62,242

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧