How to set selection in dropdownlist included in a Gridview <EditItemTemplate>

  • Thread starter Thread starter WT
  • Start date Start date
W

WT

Hello,

I can figure how to set selectIndex or Value, I have tried this

<EditItemTemplate>

<asp:DropdownList ID="DDLSector3" DataSource='<%#
BindSector(this,(int)DataBinder.Eval(Container.DataItem,"wt_SectorId"))%>'
OnPreRender='SetSectorIndex' DataTextField='wt_SectorName'
DataValueField='wt_SectorId' runat="server" >

</asp:DropdownList>

</EditItemTemplate>

method BindSector returns a DataTable, it is in codebehind

but :

OnPrerender doesnt knows about the current item

BindSector(this, etc) set the page object into this, it succesfully receive
the selected index evaluated by eval, but it has no acces to the
DropDownList to select it

I have also tried to use declarative <ItemList />



Any help welcome.

CS
 
I don't think PreRender is the place you want that code. Probably just
"SelectedIndex='<% SomeBindingExpression %>'".

But you may simplify all of this, or add clarity, by simply handling
the RowDataBound event of the grid in the code behind. This is
GridView, not DataGrid? I usually simply reference the dropdown in
question with something like :

"e.Row.Cells[x].Controls[x] as DropDownList"

Then set its SelectedValue or SelectedIndex based on some column in
"e.Row.DataItem"

Atleast this way you can set a breakpoint and solve problems.

HTH
 
Back
Top