从DROPDOWNLIST填充数据源 - 值从code人迹罕至的背后 [英] Dropdownlist populated from datasource - values are inaccessible from code behind

查看:94
本文介绍了从DROPDOWNLIST填充数据源 - 值从code人迹罕至的背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp的Dropbox这是我从数据库填充:

I have an asp dropbox which I populate from database:

这是ASPX

<asp:DropDownList runat="server" ID="ddl_last" AppendDataBoundItems="true">

和这是在CS:

  ddl_last.DataSource = eggsContext.Customers;
        ddl_last.DataValueField = "last_name";
        ddl_last.DataTextField = "last_name";
        ddl_last.DataBind();

当我尝试从c#code我什么也没有得到ddl_last.SelectedValue。 INFACT ddl_last.Items计数为0!如果我不从code填充它,但在ASPX写

When I try to get ddl_last.SelectedValue from the c# code I get nothing. infact ddl_last.Items count is 0! When I don't populate it from code, but write in the aspx

<asp:ListItem Text="example" />

一切正常。

我可以写得到的javascript值
的document.getElementById('ddl_last')值;

I can get the value from javascript by writing document.getElementById('ddl_last').value;

不过,我想知道什么做微软搞砸了这个时候,这是不是从code工作背后?

But I was wondering what did microsoft screw up this time that this is not working from the code behind?

推荐答案

看到您的问题在注释之后,我觉得这可能会帮助您解决问题:

After seeing your issue in comment, I think this may help you resolve it:

要为页面禁用视图状态,并使其能够在页面上特定的控制,设置页面的EnableViewState属性控制为true,页面的ViewStateMode属性设置为禁用,并设置控件的ViewStateMode属性设置为Enabled(启用)。

To disable view state for a page and to enable it for a specific control on the page, set the EnableViewState property of the page and the control to true, set the ViewStateMode property of the page to Disabled, and set the ViewStateMode property of the control to Enabled.

http://msdn.microsoft.com/en-us/library/system.web.ui.control.viewstatemode.aspx

另外请注意,与ViewState中被禁用,仍可以检索的发布的由

Note also, with ViewState being disabled, you can still retrieve the posted value by

if (Request["ddl_last"] != null)
    val = Request["ddl_last"];

这篇关于从DROPDOWNLIST填充数据源 - 值从code人迹罕至的背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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