回发后的DropDownList空 [英] DropDownList empty after postback

查看:98
本文介绍了回发后的DropDownList空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的ASP.NET 4.0和我有在一个DropDownList保存选定值的问题。

我初始化它在Web控件的preRender方法如下:

 如果(!Page.IsPostBack)
        LoadCountryList();

在哪里LoadCountryList的code是:

 私人无效LoadCountryList()
{
    VAR VR =新CountryRepository();
    变种countryList = vr.GetAllForList();    DdlCountry.EnableViewState = TRUE;
    DdlCountry.DataValueField =code;
    DdlCountry.DataTextField =名;
    DdlCountry.DataSource = countryList;
    DdlCountry.DataBind();
}

我提交表格后,我注意到DropDownList的是空的(这不是回发之前)。如果你看看我的code,我已经启用此控件的ViewState的,但我没有得到任何结果呢。有谁知道这是怎么回事?

编辑:

下面是ASPX文件控制的简单code:

 < ASP:DropDownList的=服务器ID =DdlCountry>
                < / ASP:DropDownList的>


解决方案

如果它在prerender,那么这就是ViewState的初始化之前,这样你就不会重装回发数据而你也无法从ViewState中得到因为它不是摆在那里。与下拉菜单的最好的事情是加载'举起手甚至在回传,但也有一些涉及到缓存。这样,你的ViewState不臃肿,但它仍然在处理回发的事件,你不骂你的数据库。

更新:

校正。 @StriplingWarrior是正确的低于约preRender评论(它会加载到ViewState中时preRender)。不过,我看不出你如何能得到比我的建议简单。对于下拉列表,请执行以下操作:


  1. 在控制/页初始化加载数据(不检查的IsPostBack标志),而不是preRender。

  2. 完成。

不过,您可以通过确保您的数据缓存(因为它现在将每一个你的页面/控制初始化运行时命中)改善这一点,但除非它的高流量,这是不是太concerning..or在至少,需要将其与所有其它高速缓存/数据库关注沿评价。

这是(从固定您的问题除外),一般的方式更好的解决方案最大的原因,那是你的ViewState没有得到不必要的臃肿与您的所有项目(Base64编码恩$ C $光盘启动),同时仍然能够跟踪在下拉列表中选择的项目(下拉菜单关闭ViewState中使它所以你不能,而不必诉诸直接查看表单岗位价值跟踪所选项目)。

I'm working on ASP.NET 4.0 and I'm having problems for saving the selected value in a DropDownList.

I initialize it in the Web Control's PreRender method as follows:

    if (!Page.IsPostBack)
        LoadCountryList();

Where LoadCountryList's code is:

private void LoadCountryList()
{
    var vr = new CountryRepository();
    var countryList = vr.GetAllForList();

    DdlCountry.EnableViewState = true;
    DdlCountry.DataValueField = "code";
    DdlCountry.DataTextField = "name";
    DdlCountry.DataSource = countryList;
    DdlCountry.DataBind();
}

After I submit the form, I've noticed the DropDownList is empty(it's not before postback). If you take a look at my code, I've enabled the ViewState for this control, but I'm not getting any results yet. Does anybody know what's going on?

EDIT:

Here is the simple code of the control in the aspx file:

                <asp:DropDownList Runat="server" ID="DdlCountry">
                </asp:DropDownList>

解决方案

If it's in prerender, then that's before ViewState is initialized, so you're not reloading the data on a postback and you're also not able to get from ViewState because it's not put in there. The best thing with dropdowns is to load 'em up even on postback, but with some caching involved. That way your ViewState isn't bloated, it still handles your event on postback, and you're not hammering your database.

UPDATE:

Correction. @StriplingWarrior is correct in the comment below about PreRender (it will load into ViewState when in PreRender). However, I don't see how you can get simpler than my suggestion. For dropdowns, do the following:

  1. Load data in control/page init (do not check the IsPostBack flag) instead of PreRender.
  2. Done.

However, you can improve this by making sure your data is cached (as it will now be hit every time your page/control init is run), but unless it's high traffic, this isn't overly concerning..or at least, it needs to be evaluated along with all other caching/database concerns.

The biggest reason this is a way better solution in general (aside from fixing your issue), is that your ViewState doesn't get bloated unnecessarily with all your items (Base64-encoded to boot) while still being able to track the selected item in the dropdown (turning off ViewState for dropdowns makes it so you can't track your selected item without having to resort to viewing the form post value directly).

这篇关于回发后的DropDownList空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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