数据绑定后下拉列表不呈现

发布于 2024-08-09 01:54:22 字数 806 浏览 1 评论 0原文

我绑定到一个下拉菜单。它适用于初始负载。在后续加载(回发)中,它不会刷新下拉列表中的项目。

using (DataView dv = dtProductGroup.DefaultView)
{
        dv.ApplyDefaultSort = false; 
        dv.Sort = "KVIGroupName ASC";

        ddlGroup.ClearSelection();
        ddlGroup.Items.Clear();

        string strAll = Localization.GetResourceValue("_strddlStatusLBAll");
        ddlGroup.DataValueField = "KVIGroupId";
        ddlGroup.DataTextField = "KVIGroupName";
        ddlGroup.DataSource = dv;
        ddlGroup.DataBind();

        ListItem item = new ListItem(strAll, "0");
        ddlGroup.Items.Insert(0, item); 
}

我已确认在回发时数据已绑定到下拉列表并且项目已成功添加。但是当页面呈现时,下拉列表没有任何新值。

我看到两种可能性:控件未呈现新值或值正在被清除。我不知道在哪里寻找可能的问题。

编辑

我发现了这个问题。下拉列表嵌入在条件更新面板中。只需调用“UpdatePanel.Update();”解决了问题。

I'm binding to a dropdown. It works on the initial load. On subsequent loads (postbacks) it doesn't refresh the items in the dropdown.

using (DataView dv = dtProductGroup.DefaultView)
{
        dv.ApplyDefaultSort = false; 
        dv.Sort = "KVIGroupName ASC";

        ddlGroup.ClearSelection();
        ddlGroup.Items.Clear();

        string strAll = Localization.GetResourceValue("_strddlStatusLBAll");
        ddlGroup.DataValueField = "KVIGroupId";
        ddlGroup.DataTextField = "KVIGroupName";
        ddlGroup.DataSource = dv;
        ddlGroup.DataBind();

        ListItem item = new ListItem(strAll, "0");
        ddlGroup.Items.Insert(0, item); 
}

I've confirmed that on the postbacks the data is being bound to the dropdown and items are successfully added. But when the page renders the dropdown doesn't have any of the new values.

I see two possibilities: The control isn't rendering the new values or the values are being cleared. I'm at a loss of where to look for possible problems.

Edit

I discovered the problem. The dropdownlist was embedded in an Conditional UpdatePanel. Simply calling "UpdatePanel.Update();" solved the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朕就是辣么酷 2024-08-16 01:54:22

回发后,视图状态将被重新应用+您说您正在尝试再次加载值。我建议让视图状态承担回发的所有重量。仅在第一次访问页面时加载值,方法是添加 if (!IsPostBack),如下所示

using (DataView dv = dtProductGroup.DefaultView)
{
   if (! IsPostBack) {

        dv.ApplyDefaultSort = false; 
        dv.Sort = "KVIGroupName ASC";

        ddlGroup.ClearSelection();
        ddlGroup.Items.Clear();

        string strAll = Localization.GetResourceValue("_strddlStatusLBAll");
        ddlGroup.DataValueField = "KVIGroupId";
        ddlGroup.DataTextField = "KVIGroupName";
        ddlGroup.DataSource = dv;
        ddlGroup.DataBind();

        ListItem item = new ListItem(strAll, "0");
        ddlGroup.Items.Insert(0, item); 
   }
}

编辑:
此外,您的语法可确保 dv 引用的 DataView 对象在代码块退出时被释放。我的第二个猜测是这会产生导致问题的副作用。

using (DataView dv = dtProductGroup.DefaultView)
{

相反,省略使用并编写如下所示的常规声明(当页面渲染完成时,DataView 将与其他所有内容一起被处置,因此实际上不需要自己执行此操作)。

DataView dv = dtProductGroup.DefaultView;

请参阅有关“使用”和 IDisposable 的 MSDN 文档 获取详细信息。

Upon Postback the viewstate is being reapplied + you said you're trying to load values again. I'd suggest letting viewstate carry all the weight on postback. Only load the values when the page is first hit by adding if (! IsPostBack) like so

using (DataView dv = dtProductGroup.DefaultView)
{
   if (! IsPostBack) {

        dv.ApplyDefaultSort = false; 
        dv.Sort = "KVIGroupName ASC";

        ddlGroup.ClearSelection();
        ddlGroup.Items.Clear();

        string strAll = Localization.GetResourceValue("_strddlStatusLBAll");
        ddlGroup.DataValueField = "KVIGroupId";
        ddlGroup.DataTextField = "KVIGroupName";
        ddlGroup.DataSource = dv;
        ddlGroup.DataBind();

        ListItem item = new ListItem(strAll, "0");
        ddlGroup.Items.Insert(0, item); 
   }
}

Edit:
Also, your syntax ensures the DataView object referenced by dv is Disposed of when the code block exits. My second guess is this causes a side-effect that causes the problem.

using (DataView dv = dtProductGroup.DefaultView)
{

Instead leave out the using and write a regular declaratoin like the following (The DataView is going to be Disposed along with everything else when the page is done rendering so there's not really any need to do it yourself).

DataView dv = dtProductGroup.DefaultView;

See the MSDN documentation about 'using' and IDisposable for detailed info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文