0

I have a drop down list, which has first item hard coded and other are bind by sql data source. Now on C# code I want this drop down list's items count which I'm getting 1 always (the first hard coded list item). while this drop down list is properly showing all list item on browser. I'm not able to understand the exact problem.

<asp:DropDownList ID="ddlGroup" runat="server" DataSourceID="dsGroupListByUserId"
     Width="100px" DataTextField="GroupName" DataValueField="GroupID" AppendDataBoundItems="True">
    <asp:ListItem Value="0">N/A</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsGroupListByUserId" runat="server" ConnectionString="<%$ ConnectionStrings:EMSsql %>" SelectCommand="GetGroup_ByEventID" SelectCommandType="StoredProcedure" >
    <SelectParameters>
        <asp:SessionParameter DefaultValue="0" Name="EventID" SessionField="EventID" Type="Int64" />
    </SelectParameters>
</asp:SqlDataSource>

And this is how i'm trying to fetch items count-

int ItemsCount = ddlGroup.Items.Count;
4

2 回答 2

3

将您的数据绑定放在 !Page.IsPostBack 中。

每次页面回发时都会刷新所有内容,这就是您的计数为 1 的原因,因为它是唯一的列表项客户端。任何填充的服务器端都需要处于非回发状态。

If (!Page.IsPostBack)
{
ddlGroup.DataBind();
}
于 2013-07-11T12:38:07.660 回答
1

我认为这是因为您没有将Page.IsPostBack属性用于page_Load.

使用IsPostBack到 Page_load 之类的

private void Page_Load()
{
    if (!IsPostBack)
    {
         // Bind your dropdown.
    }
}

希望对你有效。

于 2013-07-11T12:44:47.910 回答