我创建了一个自定义控件,它是具有指定项目的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