When do I assign a SelectedIndexChanged event handler for DropDownLists inside a DataGrid?

U

Uri

Hi,

Remember my data-bound DropDownList embedded in a TemplateColumn
inside a data-bound DataGrid?

<asp:DataGrid id="grdProducts" runat="server" DataSource="<%#
myPortfolio.Products %>"
AutoGenerateColumns="False" DataKeyField="Id">
<Columns>
<asp:TemplateColumn HeaderText="Vendor">
<ItemTemplate>
<asp:DropDownList id=ddlVendor runat="server"
DataValueField="Id" DataTextField="Name"
DataSource="<%# BL.Vendor.GetAll() %>"
AutoPostBack="True">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

Now I need to assign a specific event handler to handle the
SelectedIndexChanged
event on the DropDownLists in each row of the DataGrid.

When should I do this? I've tried doing in in the Page_Load handler, in the
DataGrid_ItemBound handler and the DataGrid_ItemCreated handler, but my
SelectedIndexChanged event handler is
never triggered.

Can someone point me in the right direction?

Also, I guess that if my DataGrid has 3 items (rows) then there will be 3
different drop down
lists all with the same name - "ddlVendor". Is this a problem and how can
I overcome it?


Thanks in advance!
Uri from Israel
 
E

Elton Wang

Hi Uri,

In order to trigger selectedIndexChanged event you should
have OnSelectedIndexChanged="MathodName" in DropDownList
tags.

HTH

Elton Wang
(e-mail address removed)
 
E

Eliyahu Goldin

Uri,

Assigning an event handler:

<asp:DropDownList id=ddlVendor runat="server"
DataValueField="Id" DataTextField="Name"
DataSource="<%# BL.Vendor.GetAll() %>"
AutoPostBack="True"
SelectedIndexChanged=myHandler>
</asp:DropDownList>

If you want to define myHandler in code behind, it should have a signature:

protected void myHandler (object sender, System.EventArgs e)

In parameter sender the handler will receive a reference to the ddl
initiated the event.

You don't need to overcome the same id problem, it doesn't exist. If you
view the html source of your page when it is viewed in a browser you will
find that every ddl is rendered as an html <select> element with it's own
unique id. You may want to look into help on web control's UniqueId and
ClientId properties.


Eliyahu
 
E

Eliyahu Goldin

Correction:
Elton is right, it should be
OnSelectedIndexChanged=myHandler>


Eliyahu
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top