回发后失去DROPDOWNLIST指数 [英] DropDownList lose index after PostBack

查看:114
本文介绍了回发后失去DROPDOWNLIST指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉菜单,我充满了从SQL Server的数据。

I have a dropdown which I fill with data from a SQL server.

我填写动态下拉列表中的 Page_Init()事件。
根据不同的价值,一个列表项被选择。

I fill the dropdown dynamically in the Page_Init() Event. Depending on the Value, a ListItem is selected.

现在的问题是,当我选择下拉另一个项目,该回发后选择重置为在DropDownList中的第一项。

Now the problem is, that when I select another Item in the dropdown, that after the postback the selection is reset to the first item in the dropdownlist.

这这里是我的code的基本版本不工作:

This here is a basic version of my code which does not work:

        ArrayList AD_Group_Members = ActiveDirectory.GetMemberOfGroup("AD-Group");
        ArrayList ListMachines = SQLQuery.Read("Database", "SELECT idVM, RandomString, Computername, Owner, FROM VM ORDER BY Computername");

        for (int i = 0; i < ListMachines.Count; i++)
        {
            String RandomString = ((Hashtable)ListMachines[i])["RandomString"].ToString();
            String Owner = ((Hashtable)ListMachines[i])["Owner"].ToString();
            DropDownList DropDownList_Owner = new DropDownList();
            DropDownList_Owner.ID = "DropDownList_Owner_" + RandomString;
            DropDownList_Owner.Width = Unit.Percentage(95);
            DropDownList_Owner.AutoPostBack = true;
            DropDownList_Owner.EnableViewState = true;
            DropDownList_Owner.SelectedIndexChanged += DropDownList_Owner_SelectedIndexChanged;
            Div_Test.Controls.Add(DropDownList_Owner);
            for (int y = 0; y < AD_Group_Members.Count; y++)
            {
                ListItem ListItem = new ListItem();
                ListItem.Value = Owner;
                ListItem.Text = ((Hashtable)AD_Group_Members[y])["GivenName"].ToString() + " " + ((Hashtable)AD_Group_Members[y])["Surname"].ToString();
                if (((Hashtable)AD_Group_Members[y])["Username"].ToString().Equals(Owner))
                {
                    ListItem.Selected = true;
                }
                DropDownList_Owner.Items.Add(ListItem);
            }
        }

哪里是我的code中的问题,它不工作,但这个例子。
预先感谢

Where is the issue in my code, that it doesn't work but the example. Thank in Advance

推荐答案

您必须以填充页面加载此条件下,您的DropDownList。
因为在每一个岗位你回来DDL再次填充并失去其选定的索引。

You have to populate your dropdownlist under this condition on pageload. Because on every post back your ddl is populated again and loses its selected index.

if (!IsPostBack)
{
    //PopulateYourDDL here
}

这篇关于回发后失去DROPDOWNLIST指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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