Drop down list on each GridView row

  • Thread starter Thread starter McGeeky
  • Start date Start date
M

McGeeky

Hi. I have a read only GridView. For each row I want to display a drop down
list which contains a list of actions the user can select from for that
particular row. E.g. "view details". When they select an action in the drop
down list I want to do a post back. How will I know which row they made the
selection in?

Is there a way to embed the row Id in the drop down list so it gets sent
back to the server?

Thanks
 
Hi,

you don't need the row ID. Instead handle DropDownList's
SelectedIndexChanged event and in that get reference to the DropDownList
raising the event, you'd get that via sender argument (first argument to the
event handling method)

[VB.NET]
Dim ddl As DropDownList = CType(sender, DropDownList)

[C#]
DropDownList ddl = (DropDownList)sender;

Then trick is to know how databound controls work. The GridViewRow is naming
container of your DropDownList

[VB.NET]
Dim row As GridViewRow = CType(ddl.NamingContainer, GridViewRow)

[C#]
GridViewRow row = (GridViewRow)ddl.NamingContainer;

I've explained some background for this type of scenarios:

Understanding the naming container hierarchy of ASP.NET databound controls
http://aspadvice.com/blogs/joteke/a...-hierarchy-of-ASP.NET-databound-controls.aspx
 
Cool. Thanks so much for that!!

Teemu Keiski said:
Hi,

you don't need the row ID. Instead handle DropDownList's
SelectedIndexChanged event and in that get reference to the DropDownList
raising the event, you'd get that via sender argument (first argument to
the event handling method)

[VB.NET]
Dim ddl As DropDownList = CType(sender, DropDownList)

[C#]
DropDownList ddl = (DropDownList)sender;

Then trick is to know how databound controls work. The GridViewRow is
naming container of your DropDownList

[VB.NET]
Dim row As GridViewRow = CType(ddl.NamingContainer, GridViewRow)

[C#]
GridViewRow row = (GridViewRow)ddl.NamingContainer;

I've explained some background for this type of scenarios:

Understanding the naming container hierarchy of ASP.NET databound controls
http://aspadvice.com/blogs/joteke/a...-hierarchy-of-ASP.NET-databound-controls.aspx

--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net



McGeeky said:
Hi. I have a read only GridView. For each row I want to display a drop
down list which contains a list of actions the user can select from for
that particular row. E.g. "view details". When they select an action in
the drop down list I want to do a post back. How will I know which row
they made the selection in?

Is there a way to embed the row Id in the drop down list so it gets sent
back to the server?

Thanks
 
Back
Top