DropdownList 在加载时重置为索引 0
每次在 Firefox 中“重新加载”页面(按 F5)时,如何将我的 asp:DropDownList
元素(具有 runat="server"
)重置为索引 0 )?
如果您建议使用 JavaScript,请注意,
- 我没有使用表单,
- 我不知道如何使用 JavaScript 访问具有
runat="server"
的元素,
如果可以使用 JavaScript 上的脚本来完成此操作.aspx
页面请解释一下。
How would I reset my asp:DropDownList
element (which has a runat="server"
) to index 0 every time the page is "reloaded" in Firefox (F5 is pressed)?
If you suggest using JavaScript, please note that
- I am not using a form
- I don't know how to access elements that have a
runat="server"
with JavaScript
If this can be done using script on the .aspx
page then please explain.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将代码放入 Page_Load 事件中以执行此操作
编辑:< /strong>
针对您的评论,如果您将上述逻辑放在 if 语句中以检查是否
Page.IsPostback = false
,则刷新时所选索引将不会设置回 0 (执行客户端回发)。 作为演示这一点的示例,这是一个下拉列表设置为选择后自动回发的页面这是后面的代码
正如将要演示的,当页面最初运行时,刷新后,所选索引将保留在下拉列表中; 然而,当
if
语句被注释掉时,刷新后,所选索引将设置为 0(在本例中为 Cheese)。put code in the Page_Load event to do this
EDIT:
In response to your comments, If you have put the above logic inside of an if statement to check whether
Page.IsPostback = false
, then the selected index will not be set back to 0 upon refresh (which performs a client postback). As an example to demonstrate this, here is a page with a dropdown list set to autopostback upon selectionHere is the code behind
As will be demonstrated, when the page is originally ran, upon refresh, the selected index will be retained within the dropdown list; When the
if
statement is commented out however, upon refresh, the selected index is set to 0 (which in this case is Cheese).只需将此代码添加到您的 Page_Load 事件中:
Just add this code to your Page_Load event :
在您的脚本中,在 HTML 代码下:
Happy Coding ^^
In your script under code HTML:
Happy Coding ^^
阻止 Firefox 保留视图状态并重新填充表单:
stop Firefox from retaining the viewstate and repopulating the form: