asp.net dropDownBox selectedIndex 未维护

发布于 2024-07-05 01:34:05 字数 1197 浏览 10 评论 0原文

我有一个奇怪的问题,下拉框 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

等你爱我 2024-07-12 01:34:05

我发现同样的问题...在我的例子中,下拉列表是在另一个下拉列表 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?

柠栀 2024-07-12 01:34:05

您确定您正在执行回发而不是刷新吗? 如果没有更多的问题背景或代码块,就很难为您提供帮助。

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.

初见 2024-07-12 01:34:05

这可能是在树错树,但过去有几件事让我抓耳挠腮:

  • 将输入元素命名为重复/保留的单词(思考“名称”、“方法”、“重置”)等)
  • 将表单元素物理地置于正在提交的表单之外,

我发现当所有逻辑调试都没有结果时,我自己的愚蠢有时会产生像这样浪费时间的“神秘”错误。

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:

  • Naming the input element a duplicated/reserved word (thinking "name", "method", "reset", etc.)
  • having the form element physically outside of the form being submitted

I find that when all the logical debugging turns up nothing, my own dumbness has created time-wasting "mystery" bugs like this on occasion.

梦开始←不甜 2024-07-12 01:34:05

这可能只是一个语法错误,但不应该

 If Page.IsPostBack Then
             GetUIValues()
        Else

看起来像这样

  If  NOT Page.IsPostBack Then
         GetUIValues()
    Else

This may simply be a syntax error, but shouldn't

 If Page.IsPostBack Then
             GetUIValues()
        Else

Look like this

  If  NOT Page.IsPostBack Then
         GetUIValues()
    Else
淡淡绿茶香 2024-07-12 01:34:05

您在页面生命周期的哪个阶段绑定下拉列表? 如果您在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文