简体   繁体   中英

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.

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. I think I am storing the DataTextField and DataValueField for the dropdown incorrectly. 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). I want this to hold the id of the dance selected by the user.

I would ensure the dropdownlist items are generated only if there is no postback - if (.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.

dances.Items.Clear();

Try that might be helps you..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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