This is a migrated thread and some comments may be shown as answers.

[Solved] Textbox in GridTemplateColumn ignored in postback

1 Answer 159 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael Diaz
Top achievements
Rank 1
Michael Diaz asked on 31 Dec 2009, 10:16 PM
Hi;

I'm having an issue with a usercontrol I created. I've used this exact same method before atleast twice and it's always worked, but for some reason this isn't working as expected. I have a RadGrid inside the user control which contains 4 GridTemplateColumns. For some reason when I change the text in these columns the TextChanged event does not fire. Here is the code for the ascx file.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CmlDetailsGrid.ascx.cs" 
    Inherits="UserControls_CmlDetailsGrid" %> 
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> 
<link href="../Styles/Styles.css" rel="stylesheet" type="text/css" /> 
<telerik:RadAjaxManagerProxy ID="RadAjaxManager_DetailsGrid" runat="server">  
    <AjaxSettings> 
        <telerik:AjaxSetting AjaxControlID="RadGrid_CMLDetails">  
            <UpdatedControls> 
                <telerik:AjaxUpdatedControl ControlID="RadGrid_CMLDetails" /> 
            </UpdatedControls> 
        </telerik:AjaxSetting> 
    </AjaxSettings> 
</telerik:RadAjaxManagerProxy> 
 
<script type="text/javascript">  
 
    function setFocus(caller, up, down, left, right) {  
        //alert(caller + ' ~ ' + up + ' ~ ' + down + ' ~ ' + left + ' ~ ' + right);  
        var keynum;  
        if (window.event) // IE  
        {  
            keynum = window.event.keyCode;  
        }  
        else if (e.which) // Netscape/Firefox/Opera  
        {  
            keynum = window.event.which;  
        }  
 
        if (keynum == 38 && up != "noControl") {  
 
            var control = $get(up);  
            control.focus();  
        }  
        else if (keynum == 40 && down != "noControl") {  
            var control = $get(down);  
            control.focus();  
        }  
        else if (keynum == 37 && left != "noControl") {  
 
            var control = $get(left);  
            control.focus();  
        }  
        else if (keynum == 39 && right != "noControl") {  
            var control = $get(right);  
            control.focus();  
        }  
    }  
      
</script> 
<div id="CMLDetailsGrid" style="height: 100%; width: 100%;">  
        <telerik:RadGrid ID="RadGrid_CMLDetails" runat="server" AllowSorting="True" Height="100%" 
        Width="100%" AutoGenerateColumns="False" AllowAutomaticUpdates="True" AllowFilteringByColumn="True" 
        OnPreRender="RadGrid_CMLDetails_PreRender">  
        <MasterTableView TableLayout="Fixed" EditMode="InPlace" DataKeyNames="cmlId" ItemStyle-Height="10px">  
            <HeaderStyle Wrap="false" Width="110px" HorizontalAlign="Center" /> 
            <Columns> 
                <telerik:GridBoundColumn DataField="cmlId" DataType="System.Int64" HeaderText="CML Id" 
                    SortExpression="cmlId" UniqueName="CML_ID" DataFormatString="<nobr>{0}</nobr>" 
                    Visible="false" /> 
                <telerik:GridBoundColumn DataField="partNum" DataType="System.String" HeaderText="Part #" 
                    SortExpression="partNum" UniqueName="partNum" DataFormatString="<nobr>{0}</nobr>" 
                    AutoPostBackOnFilter="true" /> 
                <telerik:GridBoundColumn DataField="cumPct" DataType="System.Int64" HeaderText="Cum%" 
                    SortExpression="cumPct" UniqueName="cumPct" DataFormatString="<nobr>{0}</nobr>" 
                    AutoPostBackOnFilter="true" /> 
                <telerik:GridBoundColumn DataField="extCost" DataType="System.Int64" HeaderText="Ext Cost" 
                    SortExpression="extCost" UniqueName="extCost" DataFormatString="<nobr>{0}</nobr>" 
                    AutoPostBackOnFilter="true" /> 
                <telerik:GridBoundColumn DataField="totalQty" DataType="System.Int64" HeaderText="Total Qty" 
                    SortExpression="totalQty" UniqueName="totalQty" DataFormatString="<nobr>{0}</nobr>" 
                    AutoPostBackOnFilter="true" /> 
                <telerik:GridBoundColumn DataField="moe" DataType="System.String" HeaderText="MOE" 
                    SortExpression="moe" UniqueName="moe" DataFormatString="<nobr>{0}</nobr>" /> 
                <telerik:GridBoundColumn DataField="source" DataType="System.String" HeaderText="Source" 
                    SortExpression="source" UniqueName="source" DataFormatString="<nobr>{0}</nobr>" /> 
                <telerik:GridBoundColumn DataField="structureQty" DataType="System.Int64" HeaderText="Part Req Qty" 
                    SortExpression="structureQty" UniqueName="structureQty" DataFormatString="<nobr>{0}</nobr>" /> 
                <telerik:GridBoundColumn DataField="unitPrice" DataType="System.Int64" HeaderText="Unit Price" 
                    SortExpression="unitPrice" UniqueName="unitPrice" DataFormatString="<nobr>{0}</nobr>" /> 
                <telerik:GridBoundColumn DataField="nre" DataType="System.Int64" HeaderText="NRE" 
                    SortExpression="nre" UniqueName="nre" DataFormatString="<nobr>{0}</nobr>" /> 
                <telerik:GridBoundColumn DataField="nreDesc" DataType="System.String" HeaderText="NRE Desc" 
                    SortExpression="nreDesc" UniqueName="nreDesc" DataFormatString="<nobr>{0}</nobr>" /> 
                <telerik:GridTemplateColumn DataField="arQty" DataType="System.Int64"   
                    HeaderText="AR Qty" SortExpression="arQty" UniqueName="gtcArQty">  
                    <ItemTemplate> 
                        <asp:TextBox runat="server" ID="txtArQty" Text='<%# DataBinder.Eval(Container.DataItem,"arQty") %>' 
                            AutoPostBack="true" OnTextChanged="CmlGridFieldsTextChanged" /> 
                    </ItemTemplate> 
                </telerik:GridTemplateColumn> 
                <telerik:GridTemplateColumn DataField="minBuyQty" DataType="System.Int64"   
                    HeaderText="MB Qty" SortExpression="minBuyQty" UniqueName="minBuyQty">  
                    <ItemTemplate> 
                        <asp:TextBox runat="server" ID="txtMinBuyQty" Text='<%# DataBinder.Eval(Container.DataItem,"minBuyQty") %>' 
                            AutoPostBack="true" OnTextChanged="CmlGridFieldsTextChanged" /> 
                    </ItemTemplate> 
                </telerik:GridTemplateColumn> 
                <telerik:GridTemplateColumn DataField="shrinkagePct" DataType="System.Int64"   
                    HeaderText="Shrk %" SortExpression="shrinkagePct" UniqueName="shrinkagePct">  
                    <ItemTemplate> 
                        <asp:TextBox runat="server" ID="txtShrinkagePct" Text='<%# DataBinder.Eval(Container.DataItem,"shrinkagePct") %>' 
                            AutoPostBack="true" OnTextChanged="CmlGridFieldsTextChanged" /> 
                    </ItemTemplate> 
                </telerik:GridTemplateColumn>                  
                <telerik:GridBoundColumn DataField="shrinkageQty" DataType="System.Int64" HeaderText="Shrk Qty" 
                    SortExpression="shrinkageQty" UniqueName="shrinkageQty" DataFormatString="<nobr>{0}</nobr>" /> 
                <telerik:GridTemplateColumn DataField="ssAsInd" DataType="System.Int64"   
                    HeaderText="SS/AS" SortExpression="ssAsInd" UniqueName="ssAsInd">  
                    <ItemTemplate> 
                        <asp:TextBox runat="server" ID="txtSsAsInd" Text='<%# DataBinder.Eval(Container.DataItem,"ssAsInd") %>' 
                            AutoPostBack="true" OnTextChanged="CmlGridFieldsTextChanged" /> 
                    </ItemTemplate> 
                </telerik:GridTemplateColumn>                          
                <telerik:GridBoundColumn DataField="expDate" DataType="System.String" HeaderText="Exp Date" 
                    SortExpression="expDate" UniqueName="expDate" DataFormatString="<nobr>{0}</nobr>" /> 
                <telerik:GridBoundColumn DataField="suplExpDate" DataType="System.String" HeaderText="Sup Exp Date" 
                    SortExpression="suplExpDate" UniqueName="suplExpDate" DataFormatString="<nobr>{0}</nobr>" /> 
                <telerik:GridBoundColumn DataField="qteOrdType" DataType="System.String" HeaderText="Quote /Order Type" 
                    SortExpression="qteOrdType" UniqueName="qteOrdType" DataFormatString="<nobr>{0}</nobr>" /> 
                <telerik:GridBoundColumn DataField="priceRecQty" DataType="System.Int64" HeaderText="Price Rec Qty" 
                    SortExpression="priceRecQty" UniqueName="priceRecQty" DataFormatString="<nobr>{0}</nobr>" /> 
                <telerik:GridBoundColumn DataField="frzInd" DataType="System.Int64" HeaderText="FRZ" 
                    SortExpression="frzInd" UniqueName="frzInd" DataFormatString="<nobr>{0}</nobr>" /> 
            </Columns> 
            <NoRecordsTemplate> 
                <div> 
                    There are no records to display.</div> 
            </NoRecordsTemplate> 
        </MasterTableView> 
        <GroupingSettings CaseSensitive="false" /> 
        <ClientSettings AllowRowsDragDrop="false" AllowKeyboardNavigation="true">  
            <Selecting AllowRowSelect="True" EnableDragToSelectRows="True" /> 
            <Scrolling AllowScroll="true" UseStaticHeaders="true" FrozenColumnsCount="1" SaveScrollPosition="true" /> 
            <Resizing AllowColumnResize="true" ClipCellContentOnResize="true" /> 
        </ClientSettings> 
    </telerik:RadGrid> 
</div> 
 

Here is the codebehind information.

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using Harris.Common.ModelStateWrapper_ASPNET;  
using Harris.Project.Mammoth.Domain;  
using Harris.Project.Mammoth.ServiceManager;  
using Telerik.Web.UI;  
 
public partial class UserControls_CmlDetailsGrid : System.Web.UI.UserControl  
{  
    Common _common;  
    private CmlDetailsService _cmldSvc;  
    ModelStateWrapper _modelStateWrapper;  
 
    /// <summary>  
    /// Handles Page_Load event of CMLDetailsGrid control.  
    /// </summary>  
    /// <param name="sender"></param>  
    /// <param name="e"></param>  
    protected void Page_Load(object sender, EventArgs e)  
    {  
 
    }  
 
    /// <summary>  
    /// Fills the CML Details grid from the database..  
    /// </summary>  
    /// <param name="cmlId">cmlId of type long</param>  
    public void FilterCmlDetailsData(long cmlId)  
    {  
 
        if (_common == null) _common = new Common(Session, Request);  
        if (_modelStateWrapper == null) _modelStateWrapper = new ModelStateWrapper(_common.GetDictionary());  
        if (_cmldSvc == null) _cmldSvc = new CmlDetailsService(_modelStateWrapper);  
 
        RadGrid_CMLDetails.DataSource = _cmldSvc.GetCmlDetailsByCmlId(cmlId);  
        RadGrid_CMLDetails.Rebind();  
        CmlDetails_SetToolTip();  
 
    }
    #region Tooltip creation for textboxes to enable excel like grid.  
 
    /// <summary>  
    /// Prerender call to add javascript which allows tabbing through textboxes in grid.  
    /// </summary>  
    /// <param name="sender"></param>  
    /// <param name="e"></param>  
    protected void RadGrid_CMLDetails_PreRender(object sender, EventArgs e)  
    {  
        CmlDetails_SetToolTip();  
    }  
 
    /// <summary>  
    /// Returns a string used to dynamically assign the javascript for excel like movement in grid.  
    /// </summary>  
    /// <param name="self">textbox making the call.</param>  
    /// <param name="leftBox">textbox to the left of caller, "noControl" if none.</param>  
    /// <param name="rightBox">textbox to the right of caller, "noControl" if none.</param>  
    /// <param name="dataItem">current dataitem from grid.</param>  
    /// <param name="location">either top or bottom, used to determine if this is the first or last item ingrid.</param>  
    /// <returns>strSelf + "','" + strUp + "','" + strDown + "','" + strLeft + "','" + strRight;</returns>  
    private string GetTexboxAttributes(string self, string leftBox, string rightBox, GridDataItem dataItem, string location)  
    {  
 
        // Cast self as textbox since it will always have something in it.  
        TextBox txtSelf = RadGrid_CMLDetails.MasterTableView.Items[dataItem.ItemIndex].FindControl(self) as TextBox;  
        string strSelf = txtSelf.ClientID;  
 
        // Get what should be above.  
        string strUp = string.Empty;  
        if (location != "top")  
        {  
            TextBox txtUp = RadGrid_CMLDetails.MasterTableView.Items[dataItem.ItemIndex - 1].FindControl(self) as TextBox;  
            strUp = txtUp.ClientID;  
        }  
        else 
        {  
            strUp = "noControl";  
        }  
 
        // Get what should be below.  
        string strDown = string.Empty;  
        if (location != "bottom")  
        {  
            TextBox txtDown = RadGrid_CMLDetails.MasterTableView.Items[dataItem.ItemIndex + 1].FindControl(self) as TextBox;  
            strDown = txtDown.ClientID;  
        }  
        else 
        {  
            strDown = "noControl";  
        }  
 
        // Need a string for the left box in grid.  
        string strLeft = string.Empty;  
        if (leftBox == "noControl")  
        {  
            strLeft = "noControl";  
        }  
        else 
        {  
            TextBox txtLeft = RadGrid_CMLDetails.MasterTableView.Items[dataItem.ItemIndex].FindControl(leftBox) as TextBox;  
            strLeft = txtLeft.ClientID;  
        }  
 
        // Need a string for the right box in grid.  
        string strRight = string.Empty;  
        if (rightBox == "noControl")  
        {  
            strRight = "noControl";  
        }  
        else 
        {  
            TextBox txtRight = RadGrid_CMLDetails.MasterTableView.Items[dataItem.ItemIndex].FindControl(rightBox) as TextBox;  
            strRight = txtRight.ClientID;  
        }  
 
        string strAttributes = strSelf + "','" + strUp + "','" + strDown + "','" + strLeft + "','" + strRight;  
 
        return strAttributes;  
    }  
 
    /// <summary>  
    /// Called by preRender to set tool tips on each textbox in grid.  
    /// </summary>  
    private void CmlDetails_SetToolTip()  
    {  
        int itemsInGrid = RadGrid_CMLDetails.Items.Count;  
        foreach (GridDataItem dataItem in RadGrid_CMLDetails.Items)  
        {  
 
            var txtArQty = (dataItem.FindControl("txtArQty"as TextBox);  
            var txtMinBuyQty = (dataItem.FindControl("txtMinBuyQty"as TextBox);  
            var txtShrinkagePct = (dataItem.FindControl("txtShrinkagePct"as TextBox);  
            var txtSsAsInd = (dataItem.FindControl("txtSsAsInd"as TextBox);  
            //  
 
            var rowToolTip = string.Empty;  
 
            // Need to get the StructureID from the hidden gridfield  
            // so I attached it to the tooltip part of the textbox.  
            for (int ix = 0; ix < dataItem.Cells.Count; ix++)  
            {  
                if (dataItem.Cells[ix].Text != "&nbsp;")  
                {  
                    rowToolTip = dataItem.Cells[ix].Text;  
                    break;  
                }  
            }  
 
            // Set tooltips for all four textboxes.  
            txtArQty.ToolTip = rowToolTip;  
            txtMinBuyQty.ToolTip = rowToolTip;  
            txtShrinkagePct.ToolTip = rowToolTip;  
            txtSsAsInd.ToolTip = rowToolTip;  
 
            // string self, string leftBox, string rightBox, GridDataItem dataItem, string location  
            // Top row in grid.  
            if (dataItem.ItemIndex == 0)  
            {  
                // Get locations for first box left to right.  
                string strArQtyAttributes = GetTexboxAttributes(txtArQty.ID, "noControl", txtMinBuyQty.ID, dataItem, "top");  
                txtArQty.Attributes.Add("onkeydown""setFocus('" + strArQtyAttributes + "');");  
 
                // Get locations for second box left to right.  
                string strMinBuyQtyAttributes = GetTexboxAttributes(txtMinBuyQty.ID, txtArQty.ID, txtShrinkagePct.ID, dataItem, "top");  
                txtMinBuyQty.Attributes.Add("onkeydown""setFocus('" + strMinBuyQtyAttributes + "');");  
 
                // Get locations for third box left to right.  
                string strShrinkagePctAttributes = GetTexboxAttributes(txtShrinkagePct.ID, txtMinBuyQty.ID, txtSsAsInd.ID, dataItem, "top");  
                txtShrinkagePct.Attributes.Add("onkeydown""setFocus('" + strShrinkagePctAttributes + "');");  
 
                // Get locations for third box left to right.  
                string strSsAsIndAttributes = GetTexboxAttributes(txtSsAsInd.ID, txtShrinkagePct.ID, "noControl", dataItem, "top");  
                txtSsAsInd.Attributes.Add("onkeydown""setFocus('" + strSsAsIndAttributes + "');");  
            }  
            // Used to handle middle rows in grid.  
            else if (dataItem.ItemIndex > 0 && dataItem.ItemIndex < RadGrid_CMLDetails.MasterTableView.Items.Count - 1)  
            {  
                // Get locations for first box left to right.  
                string strArQtyAttributes = GetTexboxAttributes(txtArQty.ID, "noControl", txtMinBuyQty.ID, dataItem, "mid");  
                txtArQty.Attributes.Add("onkeydown""setFocus('" + strArQtyAttributes + "');");  
 
                // Get locations for second box left to right.  
                string strMinBuyQtyAttributes = GetTexboxAttributes(txtMinBuyQty.ID, txtArQty.ID, txtShrinkagePct.ID, dataItem, "mid");  
                txtMinBuyQty.Attributes.Add("onkeydown""setFocus('" + strMinBuyQtyAttributes + "');");  
 
                // Get locations for third box left to right.  
                string strShrinkagePctAttributes = GetTexboxAttributes(txtShrinkagePct.ID, txtMinBuyQty.ID, txtSsAsInd.ID, dataItem, "mid");  
                txtShrinkagePct.Attributes.Add("onkeydown""setFocus('" + strShrinkagePctAttributes + "');");  
 
                // Get locations for third box left to right.  
                string strSsAsIndAttributes = GetTexboxAttributes(txtSsAsInd.ID, txtShrinkagePct.ID, "noControl", dataItem, "mid");  
                txtSsAsInd.Attributes.Add("onkeydown""setFocus('" + strSsAsIndAttributes + "');");  
            }  
            else if (dataItem.ItemIndex == RadGrid_CMLDetails.MasterTableView.Items.Count - 1)  
            {  
                // Get locations for first box left to right.  
                string strArQtyAttributes = GetTexboxAttributes(txtArQty.ID, "noControl", txtMinBuyQty.ID, dataItem, "bottom");  
                txtArQty.Attributes.Add("onkeydown""setFocus('" + strArQtyAttributes + "');");  
 
                // Get locations for second box left to right.  
                string strMinBuyQtyAttributes = GetTexboxAttributes(txtMinBuyQty.ID, txtArQty.ID, txtShrinkagePct.ID, dataItem, "bottom");  
                txtMinBuyQty.Attributes.Add("onkeydown""setFocus('" + strMinBuyQtyAttributes + "');");  
 
                // Get locations for third box left to right.  
                string strShrinkagePctAttributes = GetTexboxAttributes(txtShrinkagePct.ID, txtMinBuyQty.ID, txtSsAsInd.ID, dataItem, "bottom");  
                txtShrinkagePct.Attributes.Add("onkeydown""setFocus('" + strShrinkagePctAttributes + "');");  
 
                // Get locations for third box left to right.  
                string strSsAsIndAttributes = GetTexboxAttributes(txtSsAsInd.ID, txtShrinkagePct.ID, "noControl", dataItem, "bottom");  
                txtSsAsInd.Attributes.Add("onkeydown""setFocus('" + strSsAsIndAttributes + "');");  
            }  
 
        }  
    }
    #endregion  
 
    /// <summary>  
    /// Handles textchange event updates the quantity of the structure, and updates the tree.  
    /// This is used by both the WhereUsed grid and the ChildGrid.  
    /// </summary>  
    /// <param name="sender"></param>  
    /// <param name="e"></param>  
    protected void CmlGridFieldsTextChanged(object sender, EventArgs e)  
    {  
 
        TextBox txtChangeQuantity = ((TextBox)sender);  
        Int32 newQuantity = Int32.Parse(txtChangeQuantity.Text);  
        long structureId = long.Parse(txtChangeQuantity.ToolTip);  
 
        // get the structure record from the database  
        //var structureToUpdate = _structSvc.GetStructure(structureId);  
 
        //if (structureToUpdate != null)  
        //{  
        //    structureToUpdate.Qty = newQuantity;  
        //    var modStructure = _structSvc.EditStructure(structureToUpdate, _userName);  
 
        //    if (modStructure != null)  
        //    {  
        //        // Update tree with new quantity.  
        //        RadTreeView myStructureView = (RadTreeView)Parent.FindControl("StructureView");  
 
        //        _cgtree.ProcessTreeInstructions(_structSvc.TreeInstructions, myStructureView);  
        //    }  
        //    else  
        //    {  
        //        _modelStateWrapper.AddError("Structure Error", "Error updating structure data {" + structureId + "}");  
 
        //    }  
        //}  
    }  
}  
 

I've placed a break point in the CmlGridFieldsTextChanged event but it never reaches that point when the boxes are changed.

I've also tried several different RadAjaxManagerProxy settings but no luck. What I have above is almost identical to two other controls I've written, which both trip the text changed event without any problem.
<telerik:RadAjaxManagerProxy ID="RadAjaxManager_DetailsGrid" runat="server">  
    <AjaxSettings> 
        <telerik:AjaxSetting AjaxControlID="RadGrid_CMLDetails">  
            <UpdatedControls> 
                <telerik:AjaxUpdatedControl ControlID="RadGrid_CMLDetails" /> 
                <telerik:AjaxUpdatedControl ControlID="txtArQty" /> 
            </UpdatedControls> 
        </telerik:AjaxSetting> 
                <telerik:AjaxSetting AjaxControlID="txtArQty">  
            <UpdatedControls> 
                <telerik:AjaxUpdatedControl ControlID="txtArQty" /> 
                <telerik:AjaxUpdatedControl ControlID="RadGrid_CMLDetails" /> 
            </UpdatedControls> 
        </telerik:AjaxSetting> 
    </AjaxSettings> 
</telerik:RadAjaxManagerProxy> 

1 Answer, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 04 Jan 2010, 04:06 PM
Hi Michael,

I went through your code and it looks fine to me. I cannot find any reason for the unexpected behavior there. However, I suggest that you try disabling ajax by setting the EnableAjax property of the RadAjaxManager to false and see if the problem persist. Also, please make sure you are not receiving any javascript or server errors.

Let me know how it goes.

Greetings,
Iana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Michael Diaz
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Share this question
or