1

カスタム DropDownList コントロールがあります。

<cc1:CountriesControl ID="DdlCountry" TabIndex="69" runat="server" DefaultCountry="USA" OnSelectedIndexChanged="DdlCountryIndexChanged"
                                CssClass="DefaultDropdown" AutoPostBack="true" />

DropDownList には、DefaultCountry というカスタム プロパティがあります。ご覧のとおり、デフォルト値は「USA」に設定されています。ただし、私のサブクラスでは、DefaultCountry は常に null です。

DefaultCountry を ASP.NET マークアップにあるものに設定するにはどうすればよいですか?

[DefaultProperty("Text"), ToolboxData("<{0}:CountriesControl runat=server></{0}:CountriesControl>")]
public class CountriesControl : DropDownList
{
    [Bindable(true), Category("Appearance"), DefaultValue("")]

    private String defaultCountry;
    [
    Category("Behavior"),
    DefaultValue(""),
    Description("Sets the default country"),
    NotifyParentProperty(true)
    ]
    public String DefaultCountry
    {
        get
        {
            return defaultCountry;
        }
        set
        {
            defaultCountry = value;
        }
    }

    public CountriesControl()
    {                        

        this.DataSource = CountriesDataSource();
        this.DataTextField = "CountryName";
        this.DataValueField = "Country";
        this.SelectedIndex = 0;

        this.DataBind();

        // DefaultCountry is always null?
        this.Items.Insert(0, new ListItem(this.DefaultCountry, "--"));

    }
// more code here
}
4

2 に答える 2

0

解決策は、コンストラクターでデータをバインドするのではなく、自分のページのコード ビハインドから呼び出すことでした。コントロールで RenderControl メソッドが呼び出されるまで、属性 (@DefaultCountry など) の値が設定されないようです。

于 2012-01-29T14:06:35.730 に答える
0

Selected property デフォルトとして設定したい項目に対して trueを使用する必要があります。例についてはこちらをご覧ください

 // this is the normal syntax. to get the default value for dropdownlist 
 <asp:DropDownList ID="DropDownList1" runat="server" width="145px">

<asp:ListItem Text="SomeText" Value="SomeValue" Selected="true"></asp:ListItem>

</asp:DropDownList>

しかし、あなたの場合。あなたはこのように試すかもしれませんが、よくわかりませんが、推測です。

 this.Selected=true
于 2012-01-28T19:49:15.950 に答える