[英]Drop down list items count is working
I have a drop down list, which has first item hard coded and other are bind by sql data source. 我有一个下拉列表,其中具有硬编码的第一项,其他均由sql数据源绑定。 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).
现在,在C#代码上,我希望此下拉列表的项始终计数为1(第一个硬编码列表项)。 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;
Put your databind in a !Page.IsPostBack. 将您的数据绑定放在!Page.IsPostBack中。
Everything gets refreshed everytime the page is postback, which is why your count is 1 because it is the only list item client side. 每当页面回发时,所有内容都会刷新,这就是为什么您的计数为1的原因,因为它是唯一的客户端列表项。 Anything populated server side will need to be in a not post back.
任何填充的服务器端都需要保留在一个不回发的位置。
If (!Page.IsPostBack)
{
ddlGroup.DataBind();
}
I think this is because you are not using Page.IsPostBack property into page_Load
. 我认为这是因为您没有在
page_Load
使用Page.IsPostBack属性。
Use IsPostBack
into Page_load like 使用
IsPostBack
像Page_load
private void Page_Load()
{
if (!IsPostBack)
{
// Bind your dropdown.
}
}
Hope it works for you. 希望对你有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.