从DropDownList中删除项目

时间:2012-03-30 13:16:08

标签: c# asp.net drop-down-menu

我有两个DropDownLists,一个用于士兵类型,另一个用于玩家允许购买的士兵数量。我在aspx页面中使用LinqDataSource填充ddlSoldiers,如下所示:

<asp:DropDownList ID="ddlSoldiers" runat="server" 
                    DataSourceID="LinqDataSource2" DataTextField="type" 
                    DataValueField="troopid" AutoPostBack="True">
                </asp:DropDownList>
                <asp:LinqDataSource ID="LinqDataSource2" runat="server" 
                    ContextTypeName="BrowserSpill.LinqClass1DataContext" EntityTypeName="" 
                    Select="new (type, troopid)" TableName="Troops">
                </asp:LinqDataSource>

另一个列表ddlSoldierNumber在页面加载中填充,如下所示:

protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["userRole"] == null || Session["userRole"].Equals("admin"))
        {
            Response.Redirect("Login.aspx");
        }

        int userid = Convert.ToInt32(Session["userid"]);

        if(string.IsNullOrEmpty(ddlSoldiers.SelectedValue))
        {
            var varTroopType = dc.Troops.Single(t => t.troopid == 1).type;
            string troopType = Convert.ToString(varTroopType);

            var varBuildingId = dc.Buildings.Single(b => b.soldierType == troopType).buildingid;
            int buildingId = Convert.ToInt32(varBuildingId);

            var varNumberOfBuildings =
            dc.Towns.Single(t => (t.buildingid == buildingId) && (t.userid == userid)).number;

            int numberOfBuildings = Convert.ToInt32(varNumberOfBuildings);

            for (int i = 1; i < numberOfBuildings + 1; i++)
            {
                ddlSoldierNumber.Items.Add(i.ToString());
            }
        }
        else
        {

            ddlSoldierNumber.Items.Clear();

            string troopType = ddlSoldiers.SelectedItem.Text;

            var varBuildingId = dc.Buildings.Single(b => b.soldierType == troopType).buildingid;
            int buildingId = Convert.ToInt32(varBuildingId);

            var varNumberOfBuildings =
                dc.Towns.Single(t => (t.buildingid == buildingId) && (t.userid == userid)).number;

            int numberOfBuildings = Convert.ToInt32(varNumberOfBuildings);


            for(int i = 1; i < numberOfBuildings+1; i++)
            {
                ddlSoldierNumber.Items.Add(i.ToString());
            }

        }
    }

但是当我想从ddlSoldierNumber获取值时,我只获得该列表中的第一个值。我尝试通过点击这样的按钮来获取数字:

protected void btnBuySoldier_Click(object sender, EventArgs e)
    {
        string numbertobuy = ddlSoldierNumber.SelectedItem.Value;
        lblAntall.Text = numbertobuy;
    }

我试过这句话:

ddlSoldierNumber.Items.Clear();

其他地方,但没有任何运气。在按下按钮之后和ddlSoldierNumber重新填充之前,有谁知道如何清除数字列表?

2 个答案:

答案 0 :(得分:3)

如果要根据ddlSoldiers中的选择填充ddlSoldierNumber,则无法在页面加载事件中向ddlSoldierNumber添加值。为此,您必须在(ddlSoldiers)选定的索引更改事件中添加页面加载事件代码。

protected void ddlSoldiers_SelectedIndexChanged(object sender, EventArgs e)
{
    if (Session["userRole"] == null || Session["userRole"].Equals("admin"))
    {
        Response.Redirect("Login.aspx");
    }

    int userid = Convert.ToInt32(Session["userid"]);

    if (string.IsNullOrEmpty(ddlSoldiers.SelectedValue))
    {
        var varTroopType = dc.Troops.Single(t => t.troopid == 1).type;
        string troopType = Convert.ToString(varTroopType);

        var varBuildingId = dc.Buildings.Single(b => b.soldierType == troopType).buildingid;
        int buildingId = Convert.ToInt32(varBuildingId);

        var varNumberOfBuildings =
        dc.Towns.Single(t => (t.buildingid == buildingId) && (t.userid == userid)).number;

        int numberOfBuildings = Convert.ToInt32(varNumberOfBuildings);

        for (int i = 1; i < numberOfBuildings + 1; i++)
        {
            ddlSoldierNumber.Items.Add(i.ToString());
        }
    }
    else
    {

        ddlSoldierNumber.Items.Clear();

        string troopType = ddlSoldiers.SelectedItem.Text;

        var varBuildingId = dc.Buildings.Single(b => b.soldierType == troopType).buildingid;
        int buildingId = Convert.ToInt32(varBuildingId);

        var varNumberOfBuildings =
            dc.Towns.Single(t => (t.buildingid == buildingId) && (t.userid == userid)).number;

        int numberOfBuildings = Convert.ToInt32(varNumberOfBuildings);


        for (int i = 1; i < numberOfBuildings + 1; i++)
        {
            ddlSoldierNumber.Items.Add(i.ToString());
        }

    }

}

答案 1 :(得分:0)

如果我没记错的话,页面加载在按钮的事件处理程序之前运行,你是否尝试过放置ddlSolderNumber.Items.Clear();更改标签后在Click事件处理程序中? (我知道这不能解决你的第一个问题)

为此你可以尝试:(如果我很困惑,我道歉,我不好解释自己) 您可以为ddlSoldierNumber列表创建一个“填充”方法,在单击事件后调用该方法。 在你的页面加载中,你可以检查“if(!PostBack)”运行你的默认启动逻辑。