奇怪的ASP下拉列表问题 [英] Strange ASP Drop Down List Problem

查看:70
本文介绍了奇怪的ASP下拉列表问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我手上有一个很奇怪的问题,我敢肯定我只是忽略了一个很明显的问题.

我正在尝试将某些值绑定到下拉列表. ViewState肯定已启用.如果我像这样将值添加到ddl中:

Hi have quite a weird problem on my hands and I am sure I am just overlooking something quite obvious.

I am trying to bind some values to a drop down list. ViewState is definitely enabled. If I add the values to the ddl like this:

<asp:DropDownList ID="ddlUsersList" runat="server" AutoPostBack="true">
<asp:ListItem Text="Bob" />
<asp:ListItem Text="George" />
</asp:DropDownList>



或像这样:



or like this:

ddlUsersList.Items.Add(new ListItem("Bob", "0"));
ddlUsersList.Items.Add(new ListItem("George", "0"));



我没有问题.当我选择一个新用户时,回发后仍选择该用户.但是,当我这样绑定时:



I dont have an issue. When I select a new user, after the postback that user is still selected. However, when I bind like this:

IEnumerable<GetUsers_Result> result = context.GetUsers("fred", (int)UserTypes.All, (int)UserStatus.Active, false);
foreach (GetUsers_Result r in result)
        ddlUsersList.Items.Add(new ListItem(r.Surname + ", " + r.Forename, ""));



每当我选择一个新值时,回发后它都会选择第一个结果?

请有人帮我解决这个问题.

更新:BindUsers()方法仅被调用一次(我已使用断点进行检查),并且在页面加载事件中被调用,如下所示:



Whenever I select a new value, after the postback it selects the first result?

Please can someone help me figure this out.

Update: The BindUsers() method is called only once (I have used a break point to check) and it is called on the page load event and like this:

if (!Page.IsPostBack)
    BindUsers();

推荐答案

绑定在何时何地进行?如果您在回发后重新绑定,它将不会保持所选状态,这一点也不奇怪.
Where and when is the binding taking place? It isn''t strange at all that if you rebind after a postback it will not maintain the selected state.


这篇关于奇怪的ASP下拉列表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆