![]() |
|
|
|||||||
![]() |
ASP Net - RE: How to capture SelectedIndexchanged event of dropdownlist in Datag |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
The main problem is that the DropDownList's events couldn't be handled
directly by the page because it is in the DataGrid. The "cleanest" solution is : 1/ Create a custom dropdownlist by inherist from the System.Web.UI.WebControls.DropDownList. 2/ Add the CommandName and CommandArgument properties 3/ Override the OnSelectIndexChanged method 4/ In your .aspx Page : a. Set the DropDownList AutoPostback property to true b. Set the CommandName and the CommandArgument of your DropDownList b. Handle the ItemCommand of the DataGrid c. Write the code that you must be execute when the event is fire Sample : public class CustomDropDownList : System.Web.UI.WebControls.DropDownList, IPostBackEventHandler { #region Properties [Bindable(false), Category("Behavior")] public string CommandName { get { object o = ViewState["CommandName"]; if (o != null) return (string) o; return ""; } set { ViewState["CommandName"] = value; } } [Bindable(true), Category("Behavior")] public string CommandArgument { get { object o = ViewState["CommandArgument"]; if (o != null) return (string) o; return ""; } set { ViewState["CommandArgument"] = value; } } #region Methods protected override void OnSelectedIndexChanged(EventArgs e) { base.RaiseBubbleEvent(this, new CommandEventArgs(this.CommandName, this.CommandArgument)); base.OnSelectedIndexChanged (e); } #endregion } The RaiseBubbleEvent methods allows you to propagate the Command event to the DataGrid control, that raise the ItemCommand. =?Utf-8?B?Vmtv?= |
|
|
|
|
#2 |
|
Posts: n/a
|
oups, do not implements the IPostBackEventHandler interface.
"Vko" wrote: > The main problem is that the DropDownList's events couldn't be handled > directly by the page because it is in the DataGrid. > > The "cleanest" solution is : > > 1/ Create a custom dropdownlist by inherist from the > System.Web.UI.WebControls.DropDownList. > 2/ Add the CommandName and CommandArgument properties > 3/ Override the OnSelectIndexChanged method > 4/ In your .aspx Page : > a. Set the DropDownList AutoPostback property to true > b. Set the CommandName and the CommandArgument of your DropDownList > b. Handle the ItemCommand of the DataGrid > c. Write the code that you must be execute when the event is fire > > Sample : > > public class CustomDropDownList : System.Web.UI.WebControls.DropDownList, > IPostBackEventHandler > { > #region Properties > [Bindable(false), Category("Behavior")] > public string CommandName > { > get > { > object o = ViewState["CommandName"]; > if (o != null) > return (string) o; > return ""; > } > set > { > ViewState["CommandName"] = value; > } > } > [Bindable(true), Category("Behavior")] > public string CommandArgument > { > get > { > object o = ViewState["CommandArgument"]; > if (o != null) > return (string) o; > return ""; > } > set > { > ViewState["CommandArgument"] = value; > } > } > > #region Methods > protected override void OnSelectedIndexChanged(EventArgs e) > { > base.RaiseBubbleEvent(this, new CommandEventArgs(this.CommandName, > this.CommandArgument)); > base.OnSelectedIndexChanged (e); > } > > #endregion > } > > The RaiseBubbleEvent methods allows you to propagate the Command event to > the DataGrid control, that raise the ItemCommand. |
|