开发者

Problem with non data bound value in ASP.NET DropDownList

I'm trying to 'inject' an [All Regions] item into my regions dropdown, as follows:

    <asp:DropDownList ID="regionList" runat="server" AutoPostBack="true" AppendDataBoundItems="true" Width="200px" DataSourceID="regionDataSource" DataTextField="Desc"
        DataValueField="RegionId">
        <asp:ListItem Selected="True" Value="">[All Regions]</asp:ListItem>
    </asp:DropDownList>
    <asp:SqlDataSource ID="regionDataSource" r开发者_开发百科unat="server" ConnectionString="<%$ ConnectionStrings:ProVantageMediaManagement %>" SelectCommand="SELECT [RegionId], [Desc] FROM [ListRegions] ORDER BY [Desc]">
    </asp:SqlDataSource>

I also have a rank dropdown, for taxi ranks in a region, with the following select parameter:

<asp:ControlParameter ControlID="regionList" Name="RegionId" PropertyName="SelectedValue" Type="Int32" DefaultValue="" ConvertEmptyStringToNull="true" />

And the data source for ranks:

    <asp:SqlDataSource ID="rankDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ProVantageMediaManagement %>" SelectCommand="SELECT [RankId], [Name] FROM [ListRanks] WHERE [RegionId] = ISNULL(@RegionId, [RegionId]) ORDER BY [Name]">
        <SelectParameters>
            <asp:ControlParameter ControlID="regionList" Name="RegionId" PropertyName="SelectedValue" Type="Int32" DefaultValue="" ConvertEmptyStringToNull="true" />
        </SelectParameters>
    </asp:SqlDataSource>

So the expected behaviour is that when [All Regions] is selected, the ranks dropdown should get a null parameter, and mt SQL selects all ranks, for all regions. I know my SQL works with a null parameter, but the ranks dropdown is not data binding when I select [All Regions] on the region list. It binds when I select a region from the DB, but keeps that binding when I select [All Regions] again. What am I doing wrong?


Here is another way to make it work

     <asp:DropDownList ID="regionList" runat="server" AutoPostBack="true" AppendDataBoundItems="true" Width="200px" DataSourceID="regionDataSource" DataTextField="Desc"
    DataValueField="RegionId">
</asp:DropDownList>
<asp:SqlDataSource ID="regionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ProVantageMediaManagement %>" SelectCommand="SELECT null as [RegionId], '[All Regions]' as [Desc] UNION SELECT [RegionId], [Desc] FROM [ListRegions] ORDER BY [Desc]">
</asp:SqlDataSource>

Notice I took out the list item and used SQL to add the value to the datasource, I wrote it off the cuff so you may need to test it.


Try adding the all regions in code behind

here is one way

  protected void Page_Load(object sender, EventArgs e)
    {
        regionList.DataBound += new EventHandler(dl_DataBound);
    }

    void dl_DataBound(object sender, EventArgs e)
    {
        regionList.Items.Add(new ListItem("[All Regions]"));
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜