即使 VS 2008 意味着 True,DropDownList 中缺少 EnableViewState 是否默认为 False?
在调试中继器内的 DropDownList 的回发问题时,我发现即使一切看起来都设置正确,但用户在 DropDownList 上的选择并未从视图状态恢复。
我的 DropDownList 被定义为
<asp:DropDownList ID="EmployeeColumnDropDownList" runat="server" Visible="True" />
VS2008 属性检查器指示 EnableViewState 为 True 正如可以看到的那样,Visible 是 True,因此应该启用它。然而,使用调试器单步调试并跟踪 Page_load、OnItemDataBound 和我感兴趣的实际 click_event 表明数据并未从视图状态填充。
我遇到的问题是,我的事件处理代码没有拾取用户所做的选择,并且它似乎恢复到初始 page_load 上页面渲染开始时设置的内容(其中有 强制
if (!this.IsPostBack)
填充中继器和下拉列表的
包装器如下所示插入特定的enableViewState =“True”解决了问题。
<asp:DropDownList ID="EmployeeColumnDropDownList" runat="server" Visible="True" EnableViewState="True" />
记住VS2008在属性检查器上将其显示为True,
这是预期的行为吗?通过强制条件通过来解决问题?
我问这个问题是因为这看起来有点奇怪,我不希望这段代码稍后因为突然停止工作而困扰我
编辑以澄清我如何处理代码 。
我有一些使用转发器生成的下拉列表,并且全部从转发器每次循环的 OnItemDataBound 回调中填充。
我在转发器外部有一个按钮,该按钮连接到 onclick 事件处理程序。正是这个处理程序没有读取正确的用户选择。
编辑 - 视图状态似乎是一个红鲱鱼
经过几个小时的研究和调试,我认为视图状态是一个红鲱鱼。
我在这里的某个地方有一个错误,导致我的下拉列表在第一次回发时不保留其选定状态(由单击按钮引起),当表单返回时,它们丢失了选择状态(它们仍然具有正确的选择状态)内容)。
使用同一按钮的所有后续回发都会保留正确的选择。据我所知,所有回发的所有代码都是相同的。
就好像它没有在第一次回发时正确地将请求参数合并到视图状态中。
我试图建立一个非常简单的例子来深入研究这个问题,但到目前为止它已经让我损失了近一天的工作。我一直在考虑自己合并数据,但在我的情况下,中继器的存在并不让事情变得容易。
While debugging a postback problem with a DropDownList within a Repeater I discovered that even though everything looked like it was set up correctly the selections from the user on the DropDownList were not being restored from the view state.
My DropDownList was defined as
<asp:DropDownList ID="EmployeeColumnDropDownList" runat="server" Visible="True" />
VS2008 properties inspector indicates that EnableViewState is True
Visible is True as can be seen so it should be enabled. Yet stepping through with the debugger and tracing the Page_load, OnItemDataBound and the actual click_event I was interested in showed that the data wasn't being populated from the view state.
The problem I was experiencing was that the selection made by the user wasn't being picked up by my event handling code and it appeared to be reverting to what had been set up at the start of the page render on the initial page_load (which has the obligatory
if (!this.IsPostBack)
wrapper for populating the repeater and dropdown lists.
Inserting a specific enableViewState="True" into my definition as follows solved the problem.
<asp:DropDownList ID="EmployeeColumnDropDownList" runat="server" Visible="True" EnableViewState="True" />
Remember that VS2008 was showing this as True anyway on the properties inspector.
Is this expected behaviour? or have I fluked a solution by forcing a condition through?
I'm asking the question as this seems a little odd and I don't want this code to bite me later by suddenly stopping working.
EDIT to clarify how I am processing the code
I have a few dropdown lists that are generated using a repeater and are all populated from within the OnItemDataBound callback on each loop through the repeater.
I have a button outside the repeater that is hooked up to an onclick event handler. It is this handler which is not reading the correct user selection.
Edit - the viewstate appears to be a red herring
After hours of research and debugging I think the view state is a red herring.
I have a bug in here somewhere that is causing my drop down lists not to retain their selected state on the first postback (caused by a button click), When the form is returned they've lost their selection state (they still have the correct contents).
All subsequent postbacks, using the same button retain the correct selections. All code is the same as far as I can see for all postbacks.
it's as if it isn't merging the request parameters into the view state correctly for the very first postback.
I'm trying to set up a very simple example to dig into this, but it's already lost me nearly a days work so far. I've been looking at merging the data myself but the presence of a repeater in my case is not making things easy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果满足以下所有条件,则启用控件的 Viewstate:
页面的 EnableViewState 属性设置为 true。
控件的 EnableViewState 属性设置为 true。
控件的 ViewStateMode 属性设置为 Enabled 或继承 Enabled 设置。
如果需要,您可以设置 AutoPostBack = True,检查您是否正在使用 UpdatePanel,Visual Studio 2008 也有一个应该安装的服务包
编辑:
这是一个有效的代码(调试时 EnableViewState 始终为 true)。用户选择的值在 http 请求中保持不变。
在 .aspx 上:
Viewstate for a control is enabled if all of the following conditions are met:
The EnableViewState property for the page is set to true.
The EnableViewState property for the control is set to true.
The ViewStateMode property for the control is set to Enabled or inherits the Enabled setting.
you could set AutoPostBack = True if needed, check if you are using UpdatePanel, also Visual Studio 2008 has a service pack that should be installed
Edit:
Here's a code working (EnableViewState is always true when debugging). The user's selected value remains the same across http requests.
on .aspx:
OK 谜底解开了。
我将其发布在这里,以防将来对其他人有所帮助。
我有一小段旧代码,用于设置下拉列表,集中完成,以便我可以将其应用于同一页面上的三种类型的下拉列表。此代码仅在初始 Page_Load 事件期间调用,但它在下拉列表控件上设置新 ID,因此会损害由 Repeater 创建的唯一 ID。
在第一次回发时,视图状态中的条目与请求数据不匹配,这使运行时感到困惑。在随后的回传中,它似乎与自己步调一致。
所以我的建议是,如果您正在创建 Dropdownlists,或者我想象其他控件,在 Repeater 或类似的迭代器中,那么如果您希望内置功能按预期工作,请不要弄乱 ID 字段。
(这里有经验的人是否认为我应该将这个问题重命名为“如果在中继器中创建,则不要覆盖下拉列表的 ID”)
OK Solved the mystery.
I'm posting this here in case it helps someone else out in the future.
I had a tiny piece of old code where I set up my drop down list, done centrally so I could apply it to three types of dropdownlist on the same page. This code was only called during the initial Page_Load event, however it was setting a new ID on the dropdownlist control therefore compromising the unique ids being created by the Repeater.
On the first postback the entries in the viewstate weren't matching up with the request data and this confused the runtime. On subsequent postbacks it seemed to get in step with itself.
So my advice would be that if you are creating Dropdownlists, or I imagine other controls, within a Repeater or similar iterator then don't mess with the ID fields if you want the built in functionality to work as expected.
(Do any of the experienced people here think I should rename this question to something like "Don't override the ID of a dropdownlist if created in a repeater")