DropdownList在加载时重置为索引0 [英] DropdownList reset to to index 0 on load
问题描述
如何重置我的 asp:DropDownList
元素(它有一个 runat =server
)来索引Firefox中的每一次重新加载页面都是0?
如果您建议使用JavaScript,请注意
- 我没有使用表单
- 我不知道如何访问具有
的元素runat = server
with JavaScript
如果可以使用 .aspx
页面然后请解释。
将代码放在 Page_Load 事件执行此操作
protected void Page_Load(object sender,EventArgs e)
{
myDropDownList.SelectedIndex = 0;
}
编辑:
为了回应您的意见,如果您将上述逻辑放在if语句中,以检查 Page.IsPostback = false
,那么所选索引在刷新时不会被设置为0(执行客户端回发)。举一个例子来说明这一点,这里是一个页面,下拉列表设置为autopostback选择
<%@页面语言=C#AutoEventWireup =trueCodeFile =Default.aspx.csInherits =_ Default%>
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional。 DTD>
< html xmlns =http://www.w3.org/1999/xhtml>
< head runat =server>
< title>我的示例< / title>
< / head>
< body>
< form id =form1runat =server>
< div>
< asp:DropDownList ID =ddlrunat =serverAutoPostBack =true>
< / asp:DropDownList>
< / div>
< / form>
< / body>
< / html>
以下是
public partial class _Default:System.Web.UI.Page
{
protected void Page_Init(object sender,EventArgs e)
{
//道歉为Dairy Produce启发清单
ddl.Items.Add(new ListItem(Cheese));
ddl.Items.Add(new ListItem(Yoghurt));
ddl.Items.Add(new ListItem(Milk));
ddl.Items.Add(new ListItem(Butter));
}
protected void Page_Load(object sender,EventArgs e)
{
//首先运行该页面,然后注释掉
/ / if语句只留下ddl.SelectedIndex = 0;
if(!Page.IsPostBack)
{
ddl.SelectedIndex = 0;
}
}
}
页面最初运行,刷新时,所选索引将保留在下拉列表中;当 if
语句被注释掉时,刷新时,所选索引设置为0(在这种情况下为 Cheese )。 p>
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.
put code in the Page_Load event to do this
protected void Page_Load(object sender, EventArgs e)
{
myDropDownList.SelectedIndex =0;
}
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 selection
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>My Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddl" runat="server" AutoPostBack="true" >
</asp:DropDownList>
</div>
</form>
</body>
</html>
Here is the code behind
public partial class _Default : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
//Apologies for Dairy Produce inspired list
ddl.Items.Add(new ListItem("Cheese"));
ddl.Items.Add(new ListItem("Yoghurt"));
ddl.Items.Add(new ListItem("Milk"));
ddl.Items.Add(new ListItem("Butter"));
}
protected void Page_Load(object sender, EventArgs e)
{
//Run the Page with this in first, then comment out
//the if statement to leave only ddl.SelectedIndex = 0;
if (!Page.IsPostBack)
{
ddl.SelectedIndex = 0;
}
}
}
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).
这篇关于DropdownList在加载时重置为索引0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!