asp.net dropDownBox selectedIndex 未维护
我有一个奇怪的问题,下拉框 selectedIndex 在回发时始终设置为 0。 我不会意外地在我的代码中重新绑定它。 事实上,我已经在 page_load 事件的第一行放置了一个断点,并且该值已设置为零。 下拉菜单位于我项目的母版页中,我不知道这是否有影响。 我没有引用内容容器中的控件。
如果我设置 autoPostBack = 'true' 页面工作正常。 我不必更改任何代码,并且 selectedIndex 会得到维护。 我也尝试过设置enableViewState 打开和关闭,但没有什么区别。 此时我正在抓住救命稻草来弄清楚发生了什么事。 我以前从未遇到过这个问题。
这是我的 page_load 事件中的代码。
If CartEstablished Then
txtCustNum.Visible = False
btnCustSearch.Visible = False
lblCustNum.Visible = True
ddlSalesType.Visible = False
lblSalesType.Visible = True
ddlTerms.Visible = False
lblTerms.Visible = True
lblTerms.Text = TermsDescription
Else
txtCustNum.Visible = True
btnCustSearch.Visible = True
lblCustNum.Visible = False
lblSalesType.Visible = False
ddlSalesType.Visible = True
lblTerms.Visible = False
ddlTerms.Visible = True
End If
If Page.IsPostBack Then
GetUIValues()
Else
LoadTermCodes()
End If
LoadTermCodes 是我绑定导致我出现问题的下拉列表的地方。
I have a weird problem with a dropdownbox selectedIndex always being set to 0 upon postback. I'm not accidentally rebinding it in my code. In fact I've placed a breakpoint at the very first line of the page_load event and the value is already set to zero. The dropdown is in the master page of my project, I don't know if that makes a difference. I'm not referencing the control in my content holder.
If I set my autoPostBack = 'true' the page works fine. I don't have to change any code and the selectedIndex is maintained. I have also tried setting enableViewState on and off and it doesn't make a difference. At this point I'm grasping at straws to figure out what's going on. I've never had this problem before.
Here is the code in my page_load event.
If CartEstablished Then
txtCustNum.Visible = False
btnCustSearch.Visible = False
lblCustNum.Visible = True
ddlSalesType.Visible = False
lblSalesType.Visible = True
ddlTerms.Visible = False
lblTerms.Visible = True
lblTerms.Text = TermsDescription
Else
txtCustNum.Visible = True
btnCustSearch.Visible = True
lblCustNum.Visible = False
lblSalesType.Visible = False
ddlSalesType.Visible = True
lblTerms.Visible = False
ddlTerms.Visible = True
End If
If Page.IsPostBack Then
GetUIValues()
Else
LoadTermCodes()
End If
The LoadTermCodes is where I bind the dropdownlist that is causing me problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我发现同样的问题...在我的例子中,下拉列表是在另一个下拉列表 onchange 客户端事件之后由 javascript 函数填充的。 在 PageLoad 上,第二个下拉列表丢失了所有项目,因此其 selectedIndex 变为 0。有什么方法可以防止这种情况吗?
I'm finding the same problem... in my case, the dropdownlist is filled by a javascript function after another dropdownlist onchange client event. On PageLoad, the 2nd dropdownlist has lost all its items and so its selectedIndex turns to 0. Is there any way of preventing this?
您确定您正在执行回发而不是刷新吗? 如果没有更多的问题背景或代码块,就很难为您提供帮助。
Are you sure you are doing a postback and not a refresh? It is hard to help you without more context into the problem or a chunk of the code.
这可能是在树错树,但过去有几件事让我抓耳挠腮:
我发现当所有逻辑调试都没有结果时,我自己的愚蠢有时会产生像这样浪费时间的“神秘”错误。
This may be barking up the wrong tree, but a couple of things that have bitten me in the past that left me scratching my head:
I find that when all the logical debugging turns up nothing, my own dumbness has created time-wasting "mystery" bugs like this on occasion.
这可能只是一个语法错误,但不应该
看起来像这样
This may simply be a syntax error, but shouldn't
Look like this
您在页面生命周期的哪个阶段绑定下拉列表? 如果您在 page_init 中绑定,它应该可以工作,如果您在 page_load 中绑定,请确保在绑定命令周围包含 !IsPostBack。
如果您发布有问题的代码,那么故障排除会更容易。
At what stage in the page lifecycle are you binding the dropdownlist? If you're binding in page_init it should work, if you're binding in page_load make sure you wrap a !IsPostBack around the binding commands.
If you post the code in question it'd be easier to troubleshoot.