views:

138

answers:

1

I've added Dropdownlist in Gridview at RowDataBound event. The code is:

if (e.Row.RowType == DataControlRowType.DataRow) { DropDownList ddlSeason = new DropDownList();

ddlSeason.DataSourceID = "odsRoomSeason"; ddlSeason.DataTextField = "SeasonTittle"; ddlSeason.DataValueField = "SeasonID"; ddlSeason.AutoPostBack = true; ddlSeason.SelectedIndexChanged += new EventHandler(ddlSeason_SelectedIndexChanged);

TableCell tcSeason= new TableCell();

tcSeason.Controls.Add(ddlSeason); e.Row.Cells.AddAt(e.Row.Cells.Count, tcSeason); }

The event handler I've added is: protected void ddlSeason_SelectedIndexChanged(object sender, EventArgs e) { // }

But the problem is that the event handler function doesn't catch the event. Please tell me how to write the correct event handler, also I need to get the row from which the Dropdownlist's event has fired.

A: 

Hi hotcoder,

have you managed to solve this in the meanwhile?

Make sure to DataBind() the grid before the events are going to be fired, because ASP.NET can only dispatch the event corrently if the dynamically created controls are there in that moment. The page life cycle after a post back looks roughly like this:

  1. initialization
  2. Page_Load()
  3. process events
  4. PreRender()
  5. Render()

Therefore I had similar problems when I generated the dynamic controls on PreRender or later only, because events are processed before those steps. It might be necessary to DataBind() the grid even twice during the page life cycle: Once in Page_Load(), then after having processed events and updated the model, another DataBind() in PreRender() so to reflect the new model state.

chiccodoro