我有一个将值插入DropDownList的表单。当我说
DropDownList.DataBind() ;
保存函数中的- 它会附加我刚刚输入的新字段以及DropDownList上所有其他已存在的选项。关于如何制止这个的任何建议? DropDownList由一个单独的数据源填充。
protected void BtnNewTugSave_Click(object sender, EventArgs e)
{
try
{
//saving stuff
con.Close();
DropDownListX.DataBind();
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
<asp:DropDownList ID="DropDownList" runat="server" DataSourceID="SqlDataSourceT"
DataTextField="T_Name" DataValueField="T_ID" AutoPostBack="True" AppendDataBoundItems="True"
OnSelectedIndexChanged="ShowNewRateBtn">
<asp:ListItem Value="0" Text="<Select>" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="NewTug" runat="server" Text="New Tug" OnClick="NewTug_Click" CausesValidation="False" />
<asp:SqlDataSource ID="SqlDataSourceT" runat="server" ConnectionString="<%$ ConnectionStrings X %>"
SelectCommand="SELECT [A, [B] FROM [C]"></asp:SqlDataSource>
答案 0 :(得分:1)
在检索数据之前,您应该写
try
{
DropDownList.Items.Clear();
//saving stuff
DropDownList.DataBind();
}
使用DropDownList.Items.Clear();在您获取新数据并提供下拉列表之前,您的下拉列表中的项目将被删除。