Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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# 如何从代码隐藏更新DropDownList的SQLDataSource_C#_Asp.net_Drop Down Menu - Fatal编程技术网

C# 如何从代码隐藏更新DropDownList的SQLDataSource

C# 如何从代码隐藏更新DropDownList的SQLDataSource,c#,asp.net,drop-down-menu,C#,Asp.net,Drop Down Menu,我有两个下拉列表: <td style="width: 20%;"> <!-- TASK NAME --> <asp:DropDownList ID="ddlTaskName" DataSourceID="dsPopulateTaskName" AutoPostBack="true" DataValueField="Task Name" runat="server" AppendDataBoundItems="true" OnSelectedInde

我有两个下拉列表:

<td style="width: 20%;">
    <!-- TASK NAME -->
    <asp:DropDownList ID="ddlTaskName" DataSourceID="dsPopulateTaskName" AutoPostBack="true" DataValueField="Task Name" runat="server" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlTaskName_onSelectIndexChanged">
        <asp:ListItem Text="All" Value="%"></asp:ListItem>
    </asp:DropDownList>
    <asp:SqlDataSource ID="dsPopulateTaskName" runat="server" ConnectionString="<%$ ConnectionStrings:gc %>" SelectCommand=""></asp:SqlDataSource>
</td>
<td style="width: 20%;">
    <!-- SERVICE -->
    <asp:DropDownList ID="ddlService" DataSourceID="dsPopulateService" AutoPostBack="true" DataValueField="Service" runat="server" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlService_onSelectIndexChanged">
        <asp:ListItem Text="All" Value="%"></asp:ListItem>
    </asp:DropDownList>
    <asp:SqlDataSource ID="dsPopulateService" runat="server" ConnectionString="<%$ ConnectionStrings:gc %>" SelectCommand=""></asp:SqlDataSource>
</td>
加载页面时,所有内容都以
%
开头,并正确填充下拉列表。如果我从
ddlService
dropdownlist中选择一个选项,则
dsPopulateTaskName.SelectCommand
将显示正确的查询以重新填充
ddlstaskname
dropdownlist,但不会更新它


请帮我解决这个问题。

设置
SelectCommand
后,您应该调用
ddlService.DataBind()
ddlstaskname.DataBind()
,我的意思如下:

dsPopulateTaskName.SelectCommand = 
@"SELECT DISTINCT [tcol] 'Task Name'" + strForDropDownList + strWhere;
ddlService.DataBind();

dsPopulateService.SelectCommand = 
@"SELECT DISTINCT scol 'Service'" + strForDropDownList + strWhere;
ddlTaskName.DataBind();

希望对您有所帮助

谢谢。我的代码是这样的:
ddlstaskname.Items.Clear();ddlstaskname.DataBind(),它工作正常,只是我丢失了应该始终存在的
所有
。您不必使用该行:ddlstaskname.Items.Clear();它将在databindingwell上刷新,如果要更新SelectIndexChanged中的dropdownlist,请调用ddlTaskName.DataBind();在SelectIndex中,设置SelectCommand后交换
dsPopulateTaskName.SelectCommand = 
@"SELECT DISTINCT [tcol] 'Task Name'" + strForDropDownList + strWhere;
ddlService.DataBind();

dsPopulateService.SelectCommand = 
@"SELECT DISTINCT scol 'Service'" + strForDropDownList + strWhere;
ddlTaskName.DataBind();