0

我有一个舞蹈学院网站,用户可以在其中注册和添加/删除舞蹈课程。

在网页中放置特定舞蹈,对于特定用户,下拉列表显示她注册的舞蹈。

现在我想从列表中删除其中一个舞蹈。

因此,我将从表中以及下拉列表中删除该行。问题是每次具有最低 ID(索引)的项目都被删除,无论用户选择哪一个。

我想我错误地存储了下拉列表的 DataTextField 和 DataValueField。有人可以帮我吗?代码是:

private void PopulateDanceDropDown()
        {



            var registereddanceList = from dd in context.DANCER_AND_DANCE
                                        where dd.UserId == dancerId
                                        select new
                                        {
                                            Text = dd.DanceName,
                                             Value = dd.DanceId
                                        };

            dances.DataSource = registereddanceList;
            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);
            }
            PopulateDanceDropDown();

        }


<asp:DropDownList ID = "dances" runat="server">
    </asp:DropDownList>

<asp:Button ID="dropDance" runat="server" OnClick="dropthedance" Text="Drop Class" BackColor="Maroon" ForeColor="#FFCC99"/>
4

1 回答 1

0

As your question says you want to access records without storing it in Linq.

So below one could be an alternative for you.

First store all the data from Database to, lets say List<NameIDPair>

Heres what NameIDPair could be:

class NameIDPair 
{
public NameIDPair(){}
public string NameValue{get;set;}
public string IDValue{get;set;}
} 

Then populate drop down list as follows:

List<NameIDPair> storeInMe = From Database
foreach(NameIDPair pair in storeInMe )
{
ListItem item = new ListItem();
item.Value = pair.IDValue;
item.Text = pair.NameValue;
ddldancesList.Items.Add(item);
}

Then for droppingTheDance, you can call the delete stored procedure to delete the same...

Hope this helps...

于 2012-04-07T07:27:16.320 回答