简体   繁体   English

使用 linq 查询将值存储在下拉列表的 DataValueField 和 DataTextField 中

[英]Storing values in the DataValueField and DataTextField of a dropdownlist using a linq query

I have a website for dance academy where Users can register and add/drop dance classes.我有一个舞蹈学院网站,用户可以在其中注册和添加/删除舞蹈课程。

In the web page to drop a particular dance, for a particular user, the dropdown displays her registered dances.在 web 页面中,对于特定用户,下拉列表显示了她注册的舞蹈。

Now I want to delete one of the dances from the list.现在我想从列表中删除其中一个舞蹈。 So I'll remove the row from the table and also from the dropdownlist.所以我将从表中和下拉列表中删除该行。 The problem is that everytime the item with the lowest ID (index) is getting deleted, no matter which one the user selects.问题是每次删除具有最低 ID(索引)的项目时,无论用户选择哪一个。 I think I am storing the DataTextField and DataValueField for the dropdown incorrectly.我想我错误地为下拉菜单存储了 DataTextField 和 DataValueField。 The code is:代码是:

private void PopulateDanceDropDown()
{       
        // Retrieve the username
        MembershipUser currentUser = Membership.GetUser();
        var username = currentUser.UserName;

        // Retrive the userid of the curren user
        var dancerIdFromDB = from d in context.DANCER
                             where d.UserName == username
                             select d.UserId;

        Guid dancerId = new Guid();
        var first = dancerIdFromDB.FirstOrDefault();
        if (first != null)
        {
            dancerId = first;
        }

        dances.DataSource = (from dd in context.DANCER_AND_DANCE
                                    where dd.UserId == dancerId
                                    select new
                                    {
                                        Text = dd.DanceName,
                                         Value = dd.DanceId
                                    }).ToList();

        dances.DataTextField = "Text";
        dances.DataValueField = "Value";
        dances.DataBind();
    }

    protected void dropthedance(object o, EventArgs e)
    {
        String strDataValueField = dances.SelectedItem.Value;
        int danceIDFromDropDown = Convert.ToInt32(strDataValueField);
        var dancer_dance = from dd in context.DANCER_AND_DANCE
                           where dd.DanceId == danceIDFromDropDown
                           select dd;

        foreach (var dndd in dancer_dance)
        {
            context.DANCER_AND_DANCE.DeleteOnSubmit(dndd);

        }

        try
        {
            context.SubmitChanges();
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }       
    }

The problem is in the line:问题在于:

String strDataValueField = dances.SelectedItem.Value;

The strDataValueField is always getting the minimum id from the list of dance item ids in the dropdown (which happens by default). strDataValueField 始终从下拉列表中的舞蹈项目 ID 列表中获取最小 ID(默认情况下发生)。 I want this to hold the id of the dance selected by the user.我希望它保存用户选择的舞蹈的 ID。

I would ensure the dropdownlist items are generated only if there is no postback - if (.IsPostback).我会确保仅在没有回发时才生成下拉列表项 - 如果(.IsPostback)。

Also I would check the viewstate to ensure it is enabled.我也会检查视图状态以确保它已启用。 If you are disabling the viewstate and then rebinding the control on postback, it might be losing the selected item.如果您禁用视图状态,然后在回发时重新绑定控件,它可能会丢失所选项目。

I would also try setting the dropdownlist to autopostback and attach a selected index changed event to see if the changes are at least being sent that way.我还会尝试将下拉列表设置为自动回发并附加一个选定的索引更改事件以查看更改是否至少以这种方式发送。

you have to clear all the items of dropdown whenever you bind the dropdown in PopulateDanceDropDown() method.每当您在 PopulateDanceDropDown() 方法中绑定下拉列表时,您都必须清除下拉列表的所有项目。

dances.Items.Clear();

Try that might be helps you..试试这可能对你有帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM