Drop down list on each GridView row

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
 
T

Teemu Keiski

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
 
M

McGeeky

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top