Thursday, March 13, 2008

Handling OnSelectedIndexChange event of DropDownList in Data Repeater

Few days ago, I faced a problem while registering OnSelectedIndexChanged event of a DropDownList (which is Embedded in a Data Repeater). Problem is, when I change Selected Index of that Drop Down List, it fires the event but do not run the Method registered to that event. the Scenario is described below.

<asp:Repeater ID="PrescriptionRepeater" runat="server">
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddlStatus" OnSelectedIndexChange="ddlStatus_SelectedIndexChanged" autopostback="true">
<asp:ListItem Text="Prescribed"></asp:ListItem>
<asp:ListItem Text="Cancelled"></asp:ListItem>
<asp:ListItem Text="Lost"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:Repeater>


I have Placed a DropDownList(id="ddlStatus") in a DataRepeater and set
OnSelectedIndexChange="
ddlStatus_SelectedIndexChanged" autopostback="true". Now when I run the Page and change selected index of drop down list, it posts back but does not run the method ddlStatus_SelectedIndexChanged .


Then I have tried to register the Event by Hard coding.
ddlStatus.SelectedIndexChanged += new EventHandler(
ddlStatus_SelectedIndexChanged);
But still there is no change. Finally, my senior coleage find out a solution

That is Page.Request.Params.Get("__EVENTTARGET") . This method returns name of the control that caused PostBack. Then what we did was, find that control and call a method for further processing

if (Page.IsPostBack)
{
DropDownList ddlStatus = (DropDownList)Page.FindControl(Page.Request.Params["__EVENTTARGET"]);
if (ddlStatus != null)
{
DDLStateSelectedIndexChangedEvent(ddlStatus);
}
}


*I have not checked yet what to do If you have more than one dropdownlist
causing Postback in a single row of DataRepeater.
*Tested on Visual Studio 2005 and .Net 2.0
Want to know details? click below
Determining the Control that Caused a PostBack


bdnews24.com | Bangla Version

The Daily Star