C# 如何在页面加载时删除数据绑定下拉列表项?
我有一个下拉列表,它是从表中绑定的数据 我想在页面加载时删除其中的一项,但问题是这段代码没有发生任何变化: 页面加载:C# 如何在页面加载时删除数据绑定下拉列表项?,c#,asp.net,drop-down-menu,listitem,databound,C#,Asp.net,Drop Down Menu,Listitem,Databound,我有一个下拉列表,它是从表中绑定的数据 我想在页面加载时删除其中的一项,但问题是这段代码没有发生任何变化: 页面加载: protected void Page_Load(object sender, EventArgs e) { ListItem itemToRemove = DropDownList1.Items.FindByText("compiler"); //just want to remove this value if (itemToRemove != null)
protected void Page_Load(object sender, EventArgs e)
{
ListItem itemToRemove = DropDownList1.Items.FindByText("compiler"); //just want to remove this value
if (itemToRemove != null)
{
DropDownList1.Items.Remove(itemToRemove);
}
}
**dropdownlist code on aspx page**:
<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server" DataSourceID="SqlDataSource1" DataTextField="qpname" DataValueField="qpname" Height="16px" Width="116px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
<Items>
<asp:ListItem Text="Select" Value="" />
</Items>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:projectConnectionString %>" SelectCommand="SELECT [qpname] FROM [A1_quespapers]"></asp:SqlDataSource>
受保护的无效页面加载(对象发送方,事件参数e)
{
ListItemToRemove=DropDownList1.Items.FindByText(“编译器”);//只想删除此值
if(itemToRemove!=null)
{
DropDownList1.Items.Remove(itemToRemove);
}
}
**aspx页面**上的下拉列表代码:
您必须使用AppendDataBoundItems=“False”
并在PageLoad事件中设置DataSource
。接下来,您将能够删除您的项目
像这样更改DropDownList
<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="false" runat="server" DataTextField="qpname" DataValueField="qpname" Height="16px" Width="116px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
您必须使用AppendDataBoundItems=“False”
并在PageLoad事件中设置DataSource
。接下来,您将能够删除您的项目
像这样更改DropDownList
<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="false" runat="server" DataTextField="qpname" DataValueField="qpname" Height="16px" Width="116px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
您可以尝试在预渲染事件中删除
protected void Page_PreRender(object sender, EventArgs e)
{
ListItem itemToRemove = DropDownList1.Items.FindByText("compiler"); //just want to remove this value
if (itemToRemove != null)
{
DropDownList1.Items.Remove(itemToRemove);
}
}
您可以尝试在预渲染事件中删除
protected void Page_PreRender(object sender, EventArgs e)
{
ListItem itemToRemove = DropDownList1.Items.FindByText("compiler"); //just want to remove this value
if (itemToRemove != null)
{
DropDownList1.Items.Remove(itemToRemove);
}
}
或设置DropDownList1.DataSource=null当想要绑定到不同的数据源时,代码>然后重新分配也会起作用..或者设置DropDownList1.DataSource=null当想要绑定到不同的数据源时,代码>然后重新分配也可以。尝试将代码放入!iPostBack尝试将代码放入!isPostback