Datagrid - Dropdown OnSelectedIndexChanged

A

Andy

Hello,

I have a datagrid which contains DropDownList as follow:

<Columns>
<asp:TemplateColumn >
<ItemTemplate>
<asp:DropDownList id=ProductName
onSelectedIndexChanged=ProductName_IndexChange>....

Now, in the method ProductName_IndexChange I want to write codes to
handle when user select the dropdown list. Assume there are 10 rows in
my datagrid, how do I know which row the user select from? Is there a
property in datagrid that gives me the item index when an event
happens?

Thanks so much.
 
G

Guest

Hi Andy,

Adding a DropDownlist in the ItemTemplate (instead of the EditItemTemplate)
means that you have to add the event handler for the SelectedIndexChanged in
one of the following datagrid item events (e.g. OnItemCreated
OnItemDataBound). Which in turn means that upon postback you have to re-bind
the datagrid during the initialization of the page, as in those 2 examples:
http://www.societopia.net/Samples/DataGrid_6c.aspx
http://www.societopia.net/Samples/DataGridEventDelegates.aspx

This however will prove challenging in 3 scencarios:

1- If you want to edit the grid by adding a EditCommandColumn because you
will lose the ViewState of the textboxes within the EditItemTemplate.

2- If data in the dropdownlists are repeated then you might consider a
client side approach by replacing the dropdownlist by a Textbox as in this
example http://www.societopia.net/samples/textbox.htm

3- Because the dropdownlist is displayed on each row (instead of only upon
edit), the page size might be large (imagine the size of the ViewState if you
display 100 records in each dropdownlist in a datagrid that has 20 rows)
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top