Current filter:
                                You should refresh the page.
                                Support Center
                                1
                                  • There is a commonly asked question as to what is the best place to perform any manipulation with a control. Under manipulations I mean the control's properties: changing, runtime binding, conditional showing/hiding and so on. Due to the unchanged Page LifeCycle, each control fires appropriate events at early stages. These events should be used for the controls initialization: Init and Load.

                                You must  log in  or  register  to leave comments

                                1 Solution

                                1

                                The main difference between them lies in the following: the first event fires before the ViewState loading (and the PostData applying), the second one fires when all client-side data is loaded into the control.

                                Let's examine the advantage of using the suggested technique in simple examples:

                                Example #1:
                                The classic task is to bind editors in the grid's EditForm template. This can be done with Data-Binding expressions, but also you can use the Init/Load event handlers:

                                	
                                [ASPx]
                                <dxe:ASPxTextBox ID="txtName" runat="server" Width="170px" OnInit="txtName_Init"> </dxe:ASPxTextBox> <dxe:ASPxTextBox ID="txtDesc" runat="server" Width="170px" OnLoad="txtDesc_Load"> </dxe:ASPxTextBox>
                                	
                                [C#]
                                ASPxTextBox txtName; ASPxTextBox txtDesc; protected void txtName_Init(object sender, EventArgs e){ txtName = (ASPxTextBox)sender; GridViewEditFormTemplateContainer container = txtName.NamingContainer as GridViewEditFormTemplateContainer; // You can remove the if statement, and try to insert a new record. You'll catch an exception, because the DataBinder returns null reference if (!container.Grid.IsNewRowEditing) txtName.Text = DataBinder.Eval(container.DataItem, "CategoryName").ToString(); } protected void txtDesc_Load(object sender, EventArgs e){ txtDesc = (ASPxTextBox)sender; GridViewEditFormTemplateContainer container = txtDesc.NamingContainer as GridViewEditFormTemplateContainer; // You can remove the if statement, and try to insert a new record. You'll catch an exception, because the DataBinder returns null reference if (!container.Grid.IsNewRowEditing) txtDesc.Text = DataBinder.Eval(container.DataItem, "Description").ToString(); } protected void grid_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) { e.NewValues["CategoryName"] = txtName.Text; e.NewValues["Description"] = txtDesc.Text; }
                                	
                                [VB.NET]
                                Private txtName As ASPxTextBox Private txtDesc As ASPxTextBox Protected Sub txtName_Init(ByVal sender As Object, ByVal e As EventArgs) txtName = CType(sender, ASPxTextBox) Dim container As GridViewEditFormTemplateContainer = TryCast(txtName.NamingContainer, GridViewEditFormTemplateContainer) ' You can remove the if statement, and try to insert a new record. You'll catch an exception, because the DataBinder returns null reference If (Not container.Grid.IsNewRowEditing) Then txtName.Text = DataBinder.Eval(container.DataItem, "CategoryName").ToString() End If End Sub Protected Sub txtDesc_Load(ByVal sender As Object, ByVal e As EventArgs) txtDesc = CType(sender, ASPxTextBox) Dim container As GridViewEditFormTemplateContainer = TryCast(txtDesc.NamingContainer, GridViewEditFormTemplateContainer) ' You can remove the if statement, and try to insert a new record. You'll catch an exception, because the DataBinder returns null reference If (Not container.Grid.IsNewRowEditing) Then txtDesc.Text = DataBinder.Eval(container.DataItem, "Description").ToString() End If End Sub Protected Sub grid_RowUpdating(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataUpdatingEventArgs) e.NewValues("CategoryName") = txtName.Text e.NewValues("Description") = txtDesc.Text End Sub

                                As you see, there is no difference between Init and Load events. This code works fine in the Init or Load event handler, and doesn't rewrite the text written by the user. During binding, the ASPxGridView recreates its controls hierarchy (when it needs this) and restores all the data obtained from the client side. If you don't want to be confused, you should always use the Init event handler and don't worry about that. Please note, that we don't use the ASPxGridView.HtmlRowCreated or ASPxGridView.HtmlEditFormCreated event handler for controls searching in the grid template (to assign default values).
                                You've probably asked, why the Init/Load event handler is better than others? The answer is simple: you don't need to iterate through all controls hierarchy to find the required controls: e.q. you should find the ASPxPageControl in the EditForm template, and then find a control in the ASPxPageControl with the FindControl method.

                                Each control is placed within any NamingContainer, whose properties can be used, e.g. for obtaining a key value or the DataItem used in binding.

                                To know which container contains controls, you can debug the project and search for the NamingContainer property of the required control. You should put a breakpoint in the Init event handler, and check the required property. In our case, this is a GridViewDataItemTemplateContainer.

                                Example #2:
                                Let's assume you have the ASPxGridView, which has the ASPxComboBox and the ASPxTextBox in each row. The required scenario is the following: when the ASPxComboBox changes its selected index, the ASPxTextBox changes its text. This task (and many other similar tasks) can be easily solved using client-side scripts and names definition at runtime in the Init event handler.

                                	
                                [ASPx]
                                <dxwgv:GridViewDataTextColumn ... <DataItemTemplate> <dxe:ASPxComboBox ID="cb" runat="server" OnInit="cb_Init"> </dxe:ASPxComboBox> </DataItemTemplate> </dxwgv:GridViewDataTextColumn> <dxwgv:GridViewDataTextColumn ... <DataItemTemplate> <dxe:ASPxTextBox ID="txt" runat="server" Width="170px" OnInit="txt_Init"> </dxe:ASPxTextBox> </DataItemTemplate> </dxwgv:GridViewDataTextColumn>

                                Control names should have a compound value: a constant with a postfix equal to the row index. Let's create an appropriate script, which will change the textbox Text value based on the value of the combobox. In our example, we simply apply the text field:

                                	
                                [JScript]
                                function OnSelectedIndexChanged (s, e, txt){ var text = s.GetText(); txt.SetText(text); }

                                The last argument requires the ASPxTextBox client-side identifier. We will assign it to each ASPxTextBox in the following manner:

                                	
                                [C#]
                                protected void txt_Init(object sender, EventArgs e){ ASPxTextBox txt = sender as ASPxTextBox; GridViewDataItemTemplateContainer container = txt.NamingContainer as GridViewDataItemTemplateContainer; txt.ClientInstanceName = String.Format("txt{0}", container.VisibleIndex); }
                                	
                                [VB.NET]
                                Protected Sub txt_Init(ByVal sender As Object, ByVal e As EventArgs) Dim txt As ASPxTextBox = TryCast(sender, ASPxTextBox) Dim container As GridViewDataItemTemplateContainer = TryCast(txt.NamingContainer, GridViewDataItemTemplateContainer) txt.ClientInstanceName = String.Format("txt{0}", container.VisibleIndex) End Sub

                                The ASPxComboBox.Init event handler will be slightly different: we assign the client-side SelectedIndexChanged event handler, and fill its Items collection:

                                	
                                [C#]
                                protected void cb_Init(object sender, EventArgs e){ ASPxComboBox cb = sender as ASPxComboBox; GridViewDataItemTemplateContainer container = cb.NamingContainer as GridViewDataItemTemplateContainer; cb.ClientInstanceName = String.Format("cb{0}", container.VisibleIndex); cb.ClientSideEvents.SelectedIndexChanged = String.Format("function (s, e) {{ OnSelectedIndexChanged(s, e, txt{0}); }}", container.VisibleIndex); // Add some data cb.ValueType = typeof(System.Int32); cb.Items.Add("Text1", 1); cb.Items.Add("Text2", 2); cb.Items.Add("Text3", 3); cb.Items.Add("Text4", 4); cb.Items.Add("Text5", 5); }
                                	
                                [VB.NET]
                                Protected Sub cb_Init(ByVal sender As Object, ByVal e As EventArgs) Dim cb As ASPxComboBox = TryCast(sender, ASPxComboBox) Dim container As GridViewDataItemTemplateContainer = TryCast(cb.NamingContainer, GridViewDataItemTemplateContainer) cb.ClientInstanceName = String.Format("cb{0}", container.VisibleIndex) cb.ClientSideEvents.SelectedIndexChanged = String.Format("function (s, e) {{ OnSelectedIndexChanged(s, e, txt{0}); }}", container.VisibleIndex) ' Add some data cb.ValueType = GetType(System.Int32) cb.Items.Add("Text1", 1) cb.Items.Add("Text2", 2) cb.Items.Add("Text3", 3) cb.Items.Add("Text4", 4) cb.Items.Add("Text5", 5) End Sub

                                See Also:
                                General information about Template containers

                                Show all comments
                                • Harika Gandham 06.06.2012

                                  thanks

                                • Jürgen Kessler 11.14.2012

                                  Great article.
                                  Thank you.

                                • Steve Knowles 01.12.2013

                                  Is it possible to assign the OnInit event at runtime to a dynamically generated control in a dynamically generated dataitemtemplate ? Thanks

                                • Hello Steve,

                                  I am not sure that this issue is directly related to the theme described in this article. I have created a separate ticket on your behalf to discuss the issue there: Q370991

                                • Paul Serrano 1 05.24.2013

                                  Hi Vest:

                                  Nice article .. I've implemented a similar procedure but i've 4 ASPxComboBox in cascade into EditForm on ASPxGridView (First ASPxComboBox changes to second ASPxComboBox. The second ASPxComboBox changes to third ASPxComboBox and The third ASPxComboBox changes last ASPxComboBox).

                                  When I do Edit, The 4 ASPxComboBoxs work ok, but when i do New my Container.VisibleIndex is negative (-2147483648) when first ASPxComboBox call to second ASPxComboBox and i get an error.

                                  What can be happening?

                                • Hi,

                                  I have created a separate ticket for you question. Please refer to the following link:

                                  Container.VisibleIndex is negative

                                • Salman Khatri 12.20.2013

                                  Thanks you so much Vest this is very help full for me
                                  Thanks Again Friend

                                You must  log in  or  register  to leave comments

                                To start a chat you should create a support ticket


                                If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

                                FOLLOW US

                                DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, Silverlight, ASP.NET, WinForms, HTML5 or Windows 8, DevExpress tools help you build and deliver your best in the shortest time possible.

                                Copyright © 1998-2014 Developer Express Inc.
                                All trademarks or registered trademarks are property of their respective owners