![]() |
Dropdownlist in template column to fire a SelectedIndexChanged event?
I have a ddl in a template column of a data grid. The ddl displays
fine. I'm having no luck though getting an event to fire when the user selects a new value in a row's ddl. I can put a button in the template and get the command event for the grid, but nothing for the ddl. I have the ddl set to use view state and to autopostback. Thanks Mark |
RE: Dropdownlist in template column to fire a SelectedIndexChanged event?
Hi Mark,
As for the Datagrid's nested dropdownlist's postback issue, I think we can check the following things first: 1. Has the dropdownlist been set as AutoPostBack= true? 2. How do you wireup the selectedIndexchanged event handler for the dropdownlist? Here is a test page , you may have a look to see whether there are any difference: =============aspx============= <HTML> <HEAD> <title>ddlgrid</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body> <form id="Form1" method="post" runat="server"> <table width="100%" align="center"> <tr> <td> <asp:DataGrid id="dgMain" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateColumn HeaderText="DropDownList"> <ItemTemplate> <asp:DropDownList AutoPostBack="True" id="lstItems" runat="server" OnSelectedIndexChanged="lstItems_SelectedIndexChan ged" SelectedIndex='<%# Container.DataItem %>'> <asp:ListItem Value="aaaa" Selected="True">aaaa</asp:ListItem> <asp:ListItem Value="bbbb">bbbb</asp:ListItem> <asp:ListItem Value="cccc">cccc</asp:ListItem> <asp:ListItem Value="dddd">dddd</asp:ListItem> </asp:DropDownList> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> </td> </tr> <tr> <td></td> </tr> </table> </form> </body> </HTML> ======code behind============== public class ddlgrid : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid dgMain; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if(!IsPostBack) { int[] values = new int[]{0,1,2,3,2,3,1,2}; dgMain.DataSource = values; dgMain.DataBind(); } } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion protected void lstItems_SelectedIndexChanged(object sender, System.EventArgs e) { System.Web.UI.WebControls.DropDownList lst = (System.Web.UI.WebControls.DropDownList)sender; Response.Write("<br>New selected index of " + lst.ID + "is " + lst.SelectedIndex ); } } ================================================== = Thanks. Regards, Steven Cheng Microsoft Online Support Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.) Get Preview at ASP.NET whidbey http://msdn.microsoft.com/asp.net/whidbey/default.aspx |
Re: Dropdownlist in template column to fire a SelectedIndexChanged event?
Hi Steven,
Your example works. Is there a way though that I can get data from the datagrid row to the event handler? That's why I was looking a getting a datagrid event to fire. I guess I could encode what I need into the list for each row. BTW, I'm not using the grid's datasource/dataadapter for doing updates. Thanks, Mark |
Re: Dropdownlist in template column to fire a SelectedIndexChanged event?
Hi Mark,
Since the dropdownlist control is nested in the each DataGridItem's certain cell. We can use the "Control" 's "Parent" property to reference its parent control. Then in the dropdownlist's selectedindexchanged event, we can use the following code to reference the DataGridItem : protected void lstItems_SelectedIndexChanged(object sender, System.EventArgs e) { System.Web.UI.WebControls.DropDownList lst = (System.Web.UI.WebControls.DropDownList)sender; DataGridItem dgi = lst.Parent.Parent as DataGridItem; if(dgi != null) { Response.Write("<br>ItemIndex: " + dgi.ItemIndex ); Response.Write("<br>Cells Count: " + dgi.Cells.Count); } } Then, if you want to retrieve the values in other cells, just search the certain SubControl in the DataGridItem's other Cells. Thanks.' Regards, Steven Cheng Microsoft Online Support Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.) Get Preview at ASP.NET whidbey http://msdn.microsoft.com/asp.net/whidbey/default.aspx |
Re: Dropdownlist in template column to fire a SelectedIndexChanged event?
Thanks again! That does the trick.
|
Re: Dropdownlist in template column to fire a SelectedIndexChanged event?
My Pleasure!:)
|
| All times are GMT. The time now is 04:36 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.