Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 页面加载时强制下拉列表SelectMethod_C#_Asp.net_Webforms_Selectmethod - Fatal编程技术网

C# 页面加载时强制下拉列表SelectMethod

C# 页面加载时强制下拉列表SelectMethod,c#,asp.net,webforms,selectmethod,C#,Asp.net,Webforms,Selectmethod,我正在开发一个基于web表单的小型web应用程序。它使用了一些DropDownList asp控件。这是我附加了一个默认空值的控件--seleziona-- <asp:DropDownList ID="dropQuestionari" runat="server" SelectMethod="GetQuestionari"

我正在开发一个基于web表单的小型web应用程序。它使用了一些DropDownList asp控件。这是我附加了一个默认空值的控件--seleziona--

                <asp:DropDownList
                    ID="dropQuestionari"
                    runat="server"
                    SelectMethod="GetQuestionari"
                    AppendDataBoundItems="true"
                    ItemType="Models.Questionario"
                    DataTextField="Questionariointestazione"
                    DataValueField="idQuestionario">
                    <asp:ListItem Text="--- seleziona ---" Value="" Selected="true"></asp:ListItem>
                </asp:DropDownList>

似乎在页面加载时没有填充列表,并且
dropQuestionari.Items.Count
始终返回
1
(显然,它只统计默认项)。如何强制在页面加载中填充列表?

当页面不是回发时,您正在检查计数,这意味着这是第一次加载页面。如果是第一次,DDL将不会被填充。

即使在没有
If
条件的情况下尝试,并且在控件上设置
AutoPostBack=“true”
属性,DDL仍然具有相同的行为。您是否尝试在首次访问时检查控件?或者这是后续的回发?
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        //...

        if (dropQuestionari.Items.Count == 1) 
        {
            dropQuestionari.Items[0].Text = "Nessun questionario per l'utente corrente";
            this.confirmButton.Enabled = false;
        }
    }
}