我創建了一個自定義控件,它是具有指定項目的DropDownList。我設計了AutoPostback
和SelectedCategoryId
作爲屬性,並將SelectedIndexChanged
設計爲我的自定義控件的事件。
這是我的ASCX文件中的代碼背後:
自定義控件中DropDownList的索引沒有變化!
private int _selectedCategoryId;
private bool _autoPostback = false;
public event EventHandler SelectedIndexChanged;
public void BindData()
{
//Some Code...
}
protected void Page_Load(object sender, EventArgs e)
{
BindData();
DropDownList1.AutoPostBack = this._autoPostback;
}
public int SelectedCategoryId
{
get
{
return int.Parse(this.DropDownList1.SelectedItem.Value);
}
set
{
this._selectedCategoryId = value;
}
}
public string AutoPostback
{
get
{
return this.DropDownList1.AutoPostBack.ToString();
}
set
{
this._autoPostback = Convert.ToBoolean(value);
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (SelectedIndexChanged != null)
SelectedIndexChanged(this, EventArgs.Empty);
}
我想使用的更新面板,以更新文本框字段根據村落下拉列表中選擇指標。
這是我在ASPX頁面代碼:
<asp:Panel ID="PanelCategory" runat="server">
<p>
Select Product Category:
<myCtrl:CategoryDDL ID="CategoryDDL1" AutoPostback="true" OnSelectedIndexChanged="CategoryIndexChanged"
SelectedCategoryId="0" runat="server" />
</p>
<hr />
</asp:Panel>
<asp:UpdatePanel ID="UpdatePanelEdit" runat="server">
<ContentTemplate>
<%--Some TextBoxes and Other Controls--%>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="CategoryDDL1" />
</Triggers>
</asp:UpdatePanel>
但總是CategoryDDL1所選擇的指數是0(默認值一樣)。這意味着只有零值會傳遞給事件來更新文本框數據。我的代碼有什麼問題?爲什麼選擇的索引不更改?幫幫我?
您需要確保已經在控件中顯示了下拉列表的SelectedIndex。 'CategoryDDL1'是一個用戶控件,並且不會立即顯示嵌套控件的SelectedIndex屬性。所以,就像你有'SelectedCategoryId'一樣,你應該有'SelectedIndex'返回'DropDownList1.SelectedIndex' – 2010-06-15 23:05:25