Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在页面加载时删除数据绑定下拉列表项?_C#_Asp.net_Drop Down Menu_Listitem_Databound - Fatal编程技术网

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