.NET FormView 中的 DropDownList 第一次完美加载,再也不会加载
我构建了一个 DataTable
并用国家/地区缩写和国家/地区扩展填充它。我将其作为 FormView
中 DropDownList
控件的数据源。要查看此 FormView
,用户单击表单上的按钮。 DropDownList 总是第一次正确填充。处理此 FormView
(向数据存储添加一条记录)后,用户必须单击之前单击的同一个按钮才能添加另一条记录,但 DropDownList
是从未填充。我已确认每次调用时都会填充提供此列表的数据表。当然,代码也是一模一样的。
表单代码如下所示:
<asp:DropDownList ID="lstBornIn" runat="server" />
隐藏代码如下所示:
tblCountry = (DataTable) Session["tblCountry"];
DropDownList lstBorn = (DropDownList)
FormView1.FindControl("lstBornIn");
lstBorn.DataSource = tblCountry;
lstBorn.DataValueField = "Abbrev";
lstBorn.DataTextField = "Country";
lstBorn.DataBind();
如前所述,这第一次工作得很好,但再也不会了。这似乎是一个绑定问题,因为后续调用时项目列表为空。
有什么建议吗?
I build a DataTable
and populate it with a country abbreviation and country expansion. I make this the DataSource for a DropDownList
control within a FormView
. To see this FormView
, the user clicks a button on the form. The DropDownList is always correctly populated the first time. After dealing with this FormView
(to add a record to the data store), the user must click the same button he previously clicked, to add another record, but the DropDownList
is never populated. I have confirmed that the DataTable feeding this list is populated at each invocation. Of course, the code is also exactly the same.
The form code looks like this:
<asp:DropDownList ID="lstBornIn" runat="server" />
and the code-behind looks like this:
tblCountry = (DataTable) Session["tblCountry"];
DropDownList lstBorn = (DropDownList)
FormView1.FindControl("lstBornIn");
lstBorn.DataSource = tblCountry;
lstBorn.DataValueField = "Abbrev";
lstBorn.DataTextField = "Country";
lstBorn.DataBind();
As noted, this works perfectly the first time, but never again. It appears to be a binding problem, as the Items list is empty upon subsequent invocations.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我正在放弃这个,只想报告我还做了什么来解决这个问题。
一种想法是,我看到的问题仅发生在 Visual Studio 中,因此我在 IIS7 上建立了相关文件,并在三种主要浏览器中进行了尝试。全部都失败了。
最令人难以置信的事情(对我来说)是,当我使用 VS 调试器查看其中一个列表时,它整齐地隐藏了所有 67 个项目。它只是不会向他们展示。
我的解决方法很简单:对 ASPX 文件中的列表项进行硬编码。看起来很丑,但工作得很好。
我会寻找任何建议不同方法的东西。
I'm giving up on this, and just want to report on what else I've done to resolve this.
One thought was that the problem I am seeing occurs only within Visual Studio, so I established the relevant files on IIS7, and tried it with the three major browsers. All failed identically.
The most incredible thing (to me) is that when I look at one of the lists using the VS debugger, it has all 67 items neatly tucked away. It just won't show them.
My workaround is simple enough: hard-code the list items in the ASPX file. Looks ugly but works just fine.
I'll be on the lookout for anything that suggests a different approach.
页面或 DropDownList 或 DDL 层次结构中的其他控件的 ViewState 是否已关闭?如果是这样,您要么必须重新打开它,要么在回发时重新绑定。
Is the ViewState turned off for the page or the DropDownList or another control in the DDL's hierarchy? If so, you will either have to turn it back on or else rebind on postbacks.