Access other control in inputbox

2 posts, 0 answers
  1. Peter
    Peter avatar
    20 posts
    Member since:
    Sep 2018

    Posted 12 Dec 2018 Link to this post

    Dear all, 

               I have a radgrid like follows:

               <telerik:RadGrid ID="uiGrid" runat="server"
                                            AutoGenerateColumns="false" ItemStyle-BackColor="White" AlternatingItemStyle-BackColor="White"
                                            OnDataBound="uiGrid_DataBound" OnItemDataBound="uiGrid_ItemDataBound" OnNeedDataSource="uiGrid_NeedDataSource"
                                            ClientSettings-ClientEvents-OnKeyPress="keyPressInGrid"
                                            Width="100%" Height="500">
                                            <ClientSettings>
                                                <Scrolling CountGroupSplitterColumnAsFrozen="false" AllowScroll="true" UseStaticHeaders="True" SaveScrollPosition="true" FrozenColumnsCount="3"></Scrolling>

                                            </ClientSettings>
                                            <MasterTableView>
                                                <GroupByExpressions>
                                                    <telerik:GridGroupByExpression>
                                                        <SelectFields>
                                                            <telerik:GridGroupByField FieldAlias="<%$ Resources:Resource,Group %>" FieldName="IndicatorParentName"></telerik:GridGroupByField>
                                                        </SelectFields>
                                                        <GroupByFields>
                                                            <telerik:GridGroupByField FieldName="IndicatorParentName" SortOrder="Ascending"></telerik:GridGroupByField>
                                                        </GroupByFields>
                                                    </telerik:GridGroupByExpression>
                                                </GroupByExpressions>
                                                <Columns>
                                                    <telerik:GridBoundColumn DataField="IndicatorName" HeaderText="<%$ Resources:Resource,Indicator %>" UniqueName="IndicatorName"
                                                        SortExpression="IndicatorName" DataType="System.String" ItemStyle-Wrap="false">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridTemplateColumn>
                                                        <ItemTemplate>
                                                            <asp:HiddenField ID="uiIndicatorID" runat="server" Value='<%#Eval("IndicatorID")%>' />
                                                            <asp:HiddenField ID="uiIndicatorName" runat="server" Value='<%#Eval("IndicatorName")%>' />
                                                            <asp:ImageButton ID="uiTipsButton" runat="server" ImageUrl="images/information-icon.png?20170703" Width="16" Height="16" />
                                                        </ItemTemplate>
                                                    </telerik:GridTemplateColumn>
                                                    <telerik:GridTemplateColumn HeaderText="<%$ Resources:Resource,Unit %>">
                                                        <ItemTemplate>
                                                            <asp:Label ID="uiUnit" runat="server" />
                                                        </ItemTemplate>
                                                    </telerik:GridTemplateColumn>
                                                </Columns>
                                            </MasterTableView>
                                        </telerik:RadGrid>

     

    And I have functions to find controls value like this :

     

               foreach (GridDataItem item in uiGrid.MasterTableView.Items)
                {
                    if (item.ItemType == GridItemType.Item || item.ItemType == GridItemType.AlternatingItem)
                    {
                        HiddenField uiIndicatorID = item.FindControl("uiIndicatorID") as HiddenField;
                        for (int i = 0; i < uiGrid.MasterTableView.Columns.Count; i++)
                        {
                            HiddenField uiLocationID = item.FindControl("uiID_" + i.ToString()) as HiddenField;
                            RadNumericTextBox uiCurrencyBox = item.FindControl("uiCurrencyBox_" + i.ToString()) as RadNumericTextBox;
                            RadNumericTextBox uiInputBox = item.FindControl("uiInputBox_" + i.ToString()) as RadNumericTextBox;
                            HiddenField uiRemarksBox = item.FindControl("uiRemarksBox_" + i.ToString()) as HiddenField;

     

    Now I want is not to loop the GridIitem to do the processing, I want to do the processing when user leave each inputbox (inside the grid) event happened. So I added the follows , in the InstantiateIn I add a delegation txt_TextChanged to the TextChanged event of _inputbox.

    public void InstantiateIn(System.Web.UI.Control container)

                 _inputBox = new RadNumericTextBox();
                _inputBox.ID = "uiInputBox_" + _controlIDNumber;
                _inputBox.Width = Unit.Pixel(100);

                UsageEdit ue = new UsageEdit(); //UsageEdit is another class
                _inputBox.TextChanged += ue.txt_TextChanged;

    So in UsageEdit Class, I have the function 

                public void txt_TextChanged(object sender, EventArgs e)
            {
                
            }

    My question is, how can I get the control value in txt_TextChanged function. like something:

        public void txt_TextChanged(object sender, EventArgs e)
            {

                 HiddenField uiIndicatorID = sender.FindControl("uiIndicatorID") as HiddenField;
                        for (int i = 0; i < uiGrid.MasterTableView.Columns.Count; i++)
                        {
                            HiddenField uiLocationID = sender.FindControl("uiID_" + i.ToString()) as HiddenField;
                            RadNumericTextBox uiCurrencyBox = sender.FindControl("uiCurrencyBox_" + i.ToString()) as RadNumericTextBox;
                            RadNumericTextBox uiInputBox = sender.FindControl("uiInputBox_" + i.ToString()) as RadNumericTextBox;
                            HiddenField uiRemarksBox = sender.FindControl("uiRemarksBox_" + i.ToString()) as HiddenField;            
            }

    Can I do that, can I access the control of the grid item in leave event of the inputbox that is in the grid item?

     

  2. Eyup
    Admin
    Eyup avatar
    4072 posts

    Posted 17 Dec 2018 Link to this post

    Hi Peter,

    Yes, this requirement is supported you can achieve it. Before going to that, however, allow me to suggest some modifications:

    1. When the are template columns defined, the grid should be created entirely on the code-behind, using the Page_Init event handler as demonstrated in the following section:
    https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/defining-structure/creating-a-radgrid-programmatically#creating-template-columns-programmatically

    2. There is no need to set different IDs to the controls, you can leave them as uiInputBox, uiCurrencyBox, uiRemarksBox, etc. This is because every grid row is a separate INamingContainer and the IDs are automatically changed to something like xxx_xxx_xxx_uiInputBox. Additionally, if the names are not the same, it will further complicate accessing them later on when we need them.

    3. Now, for the actual requirement - inside the TextChanged event handler you can first access the grid item. It will look like something similar to this - GridDataItem item = (sender as RadNumericTextBox).NamingContainer as GridDataItem.

    4. Once you have the grid item, you can use the item.FindControl("uiRemarksBox") approach to access the other controls and execute your desired logic.

    5. If you need some unique ID from the database at this point, you can use the GetDataKeyValue method to do that:
    http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-raw-field-data-and-key-values

    I hope this will prove helpful.

    Regards,
    Eyup
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top