我在转发器中有一个DropDownList
,每当更改所选文本时,我都必须在TextBox
中显示它,但我收到Object reference not set to an instance of an object
错误
protected void Ddl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList) sender;
RepeaterItem item = (RepeaterItem) ddl .NamingContainer;
TextBox txt = (TextBox) item.FindControl("TextBox4");
txt.Text = ddl.SelectedItem.Text;
}
答案 0 :(得分:1)
只需启用Ddl dropdown autopostback属性为true,只需添加以下代码行:
protected virtual void RepeaterItemCreated(object sender, RepeaterItemEventArgs e)
{
DropDownList MyList = (DropDownList)e.Item.FindControl("ddl");
MyList.SelectedIndexChanged += ddl_SelectedIndexChanged;
}
protected void Ddl_SelectedIndexChanged(object sender, EventArgs e)
{
RepeaterItem item = (RepeaterItem) Page.FindControl("repeatorid");
TextBox txt = (TextBox) item.FindControl("TextBox4");
txt.Text = ddl.SelectedItem.Text;
}