无法在转发器内的文本框中捕获Dropdown选定值

时间:2012-11-01 05:10:32

标签: c# asp.net

我在转发器中有一个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;
}

1 个答案:

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