DROPDOWNLIST重置为索引0负载 [英] DropdownList reset to to index 0 on load

查看:157
本文介绍了DROPDOWNLIST重置为索引0负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何重置我的ASP:DropDownList的元素(其中有一个=服务器),以每一个在Firefox页面被重载时间指数0(F5为pressed)

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).

如果你认为使用JavaScript ...请注意:我不使用形式和B:我不知道如何访问具有=服务器与JavaScript元素

If you suggest using javascript...please note that A: I am not using a form and B: I don't know how to access elements that have a runat="server" with javascript

如果这可以使用在.aspx页面上的脚本来完成....请解释的东西,你想一个ASP福利局不知道(即我,大声笑)

If this can be done using script on the .aspx page....please explain things you think an ASP newb would not know (ie. me, lol)

谢谢,
安德鲁:)

thanks, Andrew :)

推荐答案

把code中的的Page_Load事件做到这一点。

put code in the Page_Load event to do this

protected void Page_Load(object sender, EventArgs e)
{    
    myDropDownList.SelectedIndex =0;
}

编辑:

在回答您的意见,如果你把上面的逻辑里if语句来检查是否 Page.IsPostback = FALSE ,然后选定的索引将不会重新设置为0时刷新(它执行客户端回发)。作为一个例子来证明这一点,这里是设置在选择到自动回发一个下拉列表的页面

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>

下面是背后

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;
        }
    }
}

由于将证明,当页面最初跑了,在刷新时,所选择的指数将在下拉列表中保留;当如果语句但是注释掉,在刷新时,选定的索引设置为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆