页面重新加载时下拉列表不会重置

发布于 2024-07-16 01:41:49 字数 529 浏览 9 评论 0 原文

嘿,...非常简单的问题

我通常编写 php,但现在碰巧使用 ASP.NET 3.0 框架进行编程,并且不知道如何解决这个问题。

我有一个 DropdownList,如果我选择了某个值,...我后面的代码会执行一堆操作并输出数据......

现在,...如果我重新加载页面(即按 F5 或浏览器上很少重新加载东西)....我的所有数据都会重置,就好像我的 selectedIndex 是 0....但实际显示的值是我选择的前一个值!...我尝试在后面的代码中重置索引在“if ispostback = false”语句中......但没有任何作用......就像值被缓存一样,我无能为力改变它............

任何帮助将不胜感激:)

(进一步解释: ....如果从我的页面加载子中打印到屏幕 mydropdownlist.selectedIndex ...它将返回 0 ....但是屏幕上选定的索引显然不是 0 ......我明白为什么这种情况正在发生...我只需要阻止它...或者至少有一种方法来确定视图状态中的索引...)

安德鲁

Hey,...very simple question

I usually write php but happen to be programming with ASP.NET 3.0 framework now and can't figure out how to fix this.

I have a DropdownList and if I selected a some value,...my code behind does a bunch of stuff and outputs the data......

Now,...if I reload the page (ie. press F5 or the little reload thing on the browser)....all my data resets as if my selectedIndex is 0....but the actual showing Value is the previous one I selected!...i try to reset the index in my code behind in the "if ispostback = false" statement...but nothing works......it's like the Value is cached and nothing I can do changes it...

...any help would be appreciated :)

(further explanation: ....if print to screen a mydropdownlist.selectedIndex from my Page load Sub...It will return a 0....however the selected index on the screen is clearly not the 0 one....I understand WHY this is happening...i just need to stop it....or at least have a way of determining the index in the view state...)

Andrew

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

夜还是长夜 2024-07-23 01:41:49

这是表单字段的正常行为。 浏览器通常会尝试在页面重新加载和后退/前进等事件中记住文本字段的内容、复选框的状态以及下拉列表中的选定项目。

因此,在脚本初始化期间,您不应假设表单字段的值将与您在 HTML 中提供的内容相匹配。 让脚本嗅探它们的当前值,并在页面加载时相应地设置其变量和 DOM 状态。

如果您确实想在重新加载/导航时放弃所有用户表单字段更改,请致电 form.reset() 将其返回到 HTML 状态。 但在正常情况下这可能对用户非常不友好。

This is normal behaviour for form fields. Browsers generally try to remember the contents of text fields, the states of checkboxes and the selected items in dropdowns over events like page reload and back/forwards.

For this reason you should not assume during script initialisation that the values of your form fields will match the content you served up in the HTML. Have the script sniff their current values and set its variables and DOM state up accordingly when the page loads.

If you really want to throw away all user form field changes on reload/navigation, call form.reset() in the script initialisation to return it to the HTML state. But this can be quite user-unfriendly in normal circumstances.

静水深流 2024-07-23 01:41:49

听起来好像当您按 f5 时,您并没有重新加载新页面。 您说,当您从下拉列表中选择某些内容时,您会在后面执行一堆代码。 嗯,这个动作是一个回发。 因此,在该操作之后点击 f5 不会被视为对页面的新调用,因此 if (IsPostBack == false) 将失败,因为它是回发。

您的第一个操作导致它成为回发,因此之后按 f5 将成为回发。

听起来您可能也有一些视图状态问题,因为您的其他控件正在重置。 尽管如果您使用客户端脚本进行隐藏/显示,它们无论如何都不会通过视图状态持久化。

It sounds like when you hit f5 you are NOT doing a new page reload. You say that when you select something from your dropdownlist that you do a bunch code behind stuff. Well that action IS a postback. So hitting f5 AFTER that action would not be considered a new call to the page so the the if (IsPostBack == false) would fail because it is a postback.

Your first action caused it to be a postback so hitting f5 after that, will be a postback.

It sounds like you may have some viewstate issues as well since your other controls are resetting. Although if you are doing the hide / shows with client side scripting, they will not be persisted through the viewstate anyways.

我不在是我 2024-07-23 01:41:49

尝试在复选框上禁用 veiwstate如果你不需要它。

Try disabling veiwstate on the checkbox if you don't need it.

虚拟世界 2024-07-23 01:41:49

您是否尝试清除浏览器中的缓存? 大多数浏览器支持 Control+F5 作为硬刷新,这将清除当前页面的缓存并从服务器重新加载整个页面。

Did you try clearing the cache in your browser? Most browsers support Control+F5 as a hard-refresh that will clear the cache for the current page and reload the entire page from the server.

锦上情书 2024-07-23 01:41:49

问候,
一旦选择了另一个下拉列表中的值,我也试图清除下拉列表。 当用户提交表单时,应该只有一个具有值的下拉列表。 我已启动代码,如下所示,但当选择其他下拉列表中的值时,下拉列表不会自行重置。 我确信我错过了一些非常简单的东西或者我的命名错误。 谢谢。

enter code

`<%@ 页面语言=“C#” AutoEventWireup=“true” CodeFile=“homepagestate.aspx.cs”继承=“_homepagestate”%>

< form id="form1" runat="server">

< asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="sel">
< asp:ListItem Value="-选择一个或多个状态-">< /asp:ListItem>
< asp:ListItem Value="阿拉巴马州">< /asp:ListItem>

    < /asp:DropDownList>

< asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true">
< asp:ListItem Value="-选择一个-">< /asp:ListItem>
< /asp:下拉列表>

      < asp:Button ID="Button1" runat="server" Text="Submit" /></div>
</form>

使用系统;

使用系统数据;

使用系统配置;

使用系统集合;

使用系统.Web;

使用系统.Web.安全;

使用 System.Web.UI;

使用 System.Web.UI.WebControls;

使用 System.Web.UI.WebControls.WebParts;

使用 System.Web.UI.HtmlControls;

公共部分类 _homepagestate : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

    if ( !IsPostBack )

    {

       // sel();

        DropDownList2.SelectedIndex = 0;

    }

}

protected void sel(object sender, EventArgs e)

{

    Response.Write(DropDownList2.SelectedIndex);

    //DropDownList2.selecteditem = 0;

}

}

'

Greetings,
I too am trying to clear a drop-down list once a value from the other drop-down is selected. When the user goes to submit the form, there should be only one drop-down which has a value. I have started the code as shown below but the drop-down will not reset itself when a value from the other drop-down is selected. I'm sure I'm missing something very simple or my naming is off. Thanks.

enter code

`<%@ Page Language="C#" AutoEventWireup="true" CodeFile="homepagestate.aspx.cs" inherits="_homepagestate"%>

< form id="form1" runat="server">

< asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="sel">
< asp:ListItem Value="-Select one or more states-">< /asp:ListItem>
< asp:ListItem Value="ALABAMA">< /asp:ListItem>

    < /asp:DropDownList>

< asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true">
< asp:ListItem Value="-Select One-">< /asp:ListItem>
< /asp:DropDownList>

      < asp:Button ID="Button1" runat="server" Text="Submit" /></div>
</form>

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class _homepagestate : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

    if ( !IsPostBack )

    {

       // sel();

        DropDownList2.SelectedIndex = 0;

    }

}

protected void sel(object sender, EventArgs e)

{

    Response.Write(DropDownList2.SelectedIndex);

    //DropDownList2.selecteditem = 0;

}

}

'

欢烬 2024-07-23 01:41:49

这实际上是来自 parrkid 关于另一个 stackoverflow 问题的内容,但请在页面末尾添加此内容:

<script language="javascript">
  var B01 = document.getElementById('<%=YourDropDownList.ClientID%>');
  B01.selectedIndex = 0;
</script>

是的...对可能是 asp.net 的完全破解,但恕我直言 :-P

This is actually from parrkid on a different stackoverflow question, but add this at the end of your page:

<script language="javascript">
  var B01 = document.getElementById('<%=YourDropDownList.ClientID%>');
  B01.selectedIndex = 0;
</script>

Yep...total hack for what's probably an asp.net but IMHO :-P

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