简体   繁体   English

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

[英]Force DropDown list SelectMethod at PageLoad

I'm developing a little web-form based web application. 我正在开发一个基于Web表单的Web应用程序。 It uses some DropDownList asp controls. 它使用一些DropDownList asp控件。 This is the control which I've appended a default empty value --- seleziona --- 这是我附加了默认空值的控件--- 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>

The control uses SelectMethod property with GetQuestionari() method that retrieves all the items needed to populate the list. 该控件将SelectMethod属性与GetQuestionari()方法一起使用,该方法检索填充列表所需的所有项目。

It work fine, but I need to check if the drop-down list is empty (excluding the default item) and change the default object text if so. 它工作正常,但是我需要检查下拉列表是否为空(不包括默认项),如果是,请更改默认对象文本。 I tried like this (in code behind): 我这样尝试过(在后面的代码中):

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;
        }
    }
}

It seems that the list is not populated at page-load and dropQuestionari.Items.Count always returns 1 (obviously it counts only the default item). 似乎该列表未在dropQuestionari.Items.CountdropQuestionari.Items.Count处填充,始终返回1 (显然,它仅计算默认项)。 How can I force the list to be populated in the PageLoad? 如何强制将列表填充到PageLoad中?

You're checking the count when the page ISN'T a post back, meaning it's the first time the page is loaded. 当页面没有回发时,您正在检查计数,这是页面第一次加载。 If it's the first time, the DDL would not be populated. 如果是第一次,则不会填充DDL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM