DropDownList重复项

时间:2012-12-07 17:08:56

标签: c# asp.net

您好我正在尝试将数据重新绑定到DataList,以便从SqlDataSource获取数据,但我不断收到重复的项目。这是我的代码:

    

<asp:SqlDataSource ID="CategoryDataSource" runat="server" 
    ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True" 
    ProviderName="System.Data.SqlClient" SelectCommand="SELECT [CategoryName] FROM [forum_categories]">
</asp:SqlDataSource>

protected void Button6_Click( object sender , EventArgs e )
{
    string categoryToCreate = CreateCategory.Text;

    if(categoryToCreate != string.Empty)
    {
        CategoryCreateName.Visible = false;
        DataAccess.insertDataItem(categoryToCreate);
        CategoryList.DataBind(); 
    }
    else
    {
        CategoryCreateName.Visible = true;
    }
}

insertDataItem 将新类别插入数据库。

如何更新DropDownList中的项目,以便我不会收到重复的项目?

1 个答案:

答案 0 :(得分:1)

试试这个:

    protected void Button6_Click( object sender , EventArgs e )
{
    string categoryToCreate = CreateCategory.Text;

    if(categoryToCreate != string.Empty)
    {
        CategoryCreateName.Visible = false;
        DataAccess.insertDataItem(categoryToCreate);
        CategoryList.Items.Clear();
        CategoryList.DataBind(); 
    }else
    {
        CategoryCreateName.Visible = true;
    }
}