1

我有一个下拉列表,可以在其中选择人名。但是我需要禁用已使用的值,因为我想在一个事件的同一个 id 中有两个相同的值......在数据库中,我使用复合键解决了这个问题,但我需要以编程方式解决这个问题。

这是我较短的数据源:

<asp:SqlDataSource 
    ID="SqlDataSource1" 
    runat="server" 
    ConnectionString="<%$ ConnectionStrings:fotbalConnectionString %>"
    SelectCommand="SELECT table1.ID_person, table1.ID_event,FROM [table1] WHERE ([ID_event] = @ID_event)        
    <SelectParameters>
        <asp:QueryStringParameter 
            DefaultValue="0" 
            Name="ID_event" 
            QueryStringField="id" 
            Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>
4

1 回答 1

0

You can create a hash whose keys will be the identifiers of values you have in the dropDownList and the values will be a boolean which will indicate if it has been used.

You will need a method to update the Hash, so everytime you select some item from the DDL (dropDownList) you mark it as used or not (if it's the case). You can call this method from SelectedIndexChanged event.

Also will need a method to list values (key) from the Hash for your DDL where you just get unselected (value == false) items.

The property where you store data could have a getter like this:

private Hashtable SelectedValuesHash        
{
        get
        {
            object o = ViewState["SelectedValuesHash"];

            if (o == null)
                return new Hashtable();
            else
                return (Hashtable)o;
        }
    }

The method to add values could be like this:

 private void AddSelectedValuesToHash(int key, bool hasBeenSelected)
    {
        var rows = ViewState["SelectedValuesHash"] as Hashtable;

        if (rows == null)
        {
            rows = new Hashtable();
            ViewState["SelectedValuesHash"] = rows;
        }
        rows.Add(key, hasBeenSelected);
    }
于 2013-04-23T09:36:31.100 回答