我有一个从DB获取数据的复选框列表。用户将选中/复选框并单击删除。删除后,我正在重新绑定新数据,但更改没有反映出来。该页面仍显示所选的复选框。但是在DB中删除了记录。我错过了什么。
ASPX:
<asp:CheckBoxList ID="cblLanguages" runat="server" AppendDataBoundItems="true"
DataTextField="LanguageName" DataValueField="PKLanguageId" >
</asp:CheckBoxList>
<asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_Click" Text="Delete" />
C#:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
BindData();
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
DeleteLanguages();
BindData();
}
private void BindData()
{
DataSet dsData = LanguagesDB.GetLanguages();
cblLanguages.DataSource = dsData.Tables[0];
cblLanguages.DataBind();
}
private void DeleteLanguages()
{
//code to delete selected values
}
答案 0 :(得分:3)
每次加载CheckboxList都会清除项目并加载。
private void BindData()
{
if(cblLanguages.Items.Count > 0)
cblLanguages.Items.Clear();
DataSet dsData = LanguagesDB.GetLanguages();
cblLanguages.DataSource = dsData.Tables[0];
cblLanguages.DataBind();
}