Acess textbox values in template on grid

6 posts, 0 answers
  1. David Penny
    David Penny avatar
    174 posts
    Member since:
    Jun 2006

    Posted 02 Aug 2010 Link to this post

    Hi,

    I am trying to update the use of a RadGrid to make it more efficient.  I have a series of textboxes in template columns on the grid so I can use editing of all templated cells (like in Excel).  But I need to update each record on data entry.  This all works OK if I only access each template column separately, but I'd like to read all text fields in the template row at once to keep the code in one place.

    I have grid columns defined as:

    <telerik:GridTemplateColumn UniqueName="Day1">
        <ItemTemplate>
            <asp:TextBox ID="Day1TextBox" runat="server" Width="40px" Enabled="true" OnTextChanged="Day1_TextChanged"
                AutoPostBack="true"></asp:TextBox>
        </ItemTemplate>
    </telerik:GridTemplateColumn>
    <telerik:GridTemplateColumn UniqueName="Day2">
        <ItemTemplate>
            <asp:TextBox ID="Day2TextBox" runat="server" Width="40px" Enabled="true" OnTextChanged="Day2_TextChanged"
                AutoPostBack="true"></asp:TextBox>
        </ItemTemplate>
    </telerik:GridTemplateColumn>
    <telerik:GridTemplateColumn UniqueName="Day3">
        <ItemTemplate>
            <asp:TextBox ID="Day3TextBox" runat="server" Width="40px" Enabled="true" OnTextChanged="Day3_TextChanged"
                AutoPostBack="true"></asp:TextBox>
        </ItemTemplate>
    </telerik:GridTemplateColumn>

    In code behind I can access each coumn using:
    Protected Sub Day2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim textbx As TextBox = sender
        Dim item As GridDataItem = textbx.NamingContainer
        Dim keyValues As String = item.KeyValues
        Dim p As Integer = InStr(keyValues, ":")
        Dim p2 As Integer = InStr(keyValues, "}")
        Dim strUniqueID As String = keyValues.Substring(p + 1, (p2 - p) - 3)
        Dim UniqueID As Integer = CInt(strUniqueID)

    But what I really want to do is read each of the values from Day1TextBox, Day2TextBox etc when any of the values change.  I have tried the following but it is not right:
    Protected Sub Day3_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim editedItem As GridEditableItem = CType(CType(sender, TextBox).NamingContainer, GridEditableItem)
        Dim txtDay1 As TextBox = CType(editedItem("Day1TextBox").Controls(0), TextBox)
        Dim txtDay2 As TextBox = CType(editedItem("Day2TextBox").Controls(0), TextBox)
        Dim txtDay3 As TextBox = CType(editedItem("Day1TextBox").Controls(0), TextBox)
        CalculateLine(3, editedItem)

    What I want to do is get a reference to each of the textbox fields in the templates.  I thought GridEditableItem might have worked but seems wrong.

    Are there any suggestions how I can go about achieving this?  Thanks.

    David Penny
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 02 Aug 2010 Link to this post

    Hello David Penny,

    You need to use FindControl() method to access the corresponding TextBox in the row. Here is an example.

    C#:
    protected void Day1_TextChanged(object sender, EventArgs e)
    {
        GridDataItem item = (GridDataItem)(sender as TextBox).NamingContainer;
        TextBox textBox2 = (TextBox)item.FindControl("Day2TextBox");
        textBox2.Text = "Hai";
    }


    -Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. David Penny
    David Penny avatar
    174 posts
    Member since:
    Jun 2006

    Posted 02 Aug 2010 Link to this post

    Hi Shinu,

    Thanks for the fast and great response.  Exactly what I needed.  I guess I knew this, but forgot I knew it!

    David Penny
  5. Yeroon
    Yeroon avatar
    87 posts
    Member since:
    Oct 2012

    Posted 18 Oct 2010 Link to this post

    Hi,

    I use the same idea as you. Having textboxes with autopostback and ontextchanged event handler. However the event never fires. There actually is a postback, but the event itself not firing. Any ideas?

    <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
        CodeFile="MainC1.aspx.cs" Inherits="MainC1" %>
     
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc2" %>
    <asp:Content ContentPlaceHolderID="cphOpties" ID="content1" runat="server">
        <cc2:RoundedCornersExtender ID="RoundedCornersExtender1" runat="server" BorderColor="187, 187, 187"
            Radius="10" TargetControlID="divCorners1" Enabled="True">
        </cc2:RoundedCornersExtender>
        <div style="padding: 8px 8px 8px 8px;">
            <div runat="server" id="divCorners1">
                <table runat="server" id="Table8" cellpadding="3" cellspacing="3" width="100%">
                    <tr>
                        <td style="width: 100px">
                            <b>Filteren:</b>
                        </td>
                        <td>
                              
                        </td>
                    </tr>
                    <tr>
                        <td style="width: 100px">
                            Startdatum:
                        </td>
                        <td>
                            <telerik:RadDatePicker ID="dp4" runat="server" MinDate="2006-01-01" AutoPostBack="true"
                                OnSelectedDateChanged="dp4_SelectedDateChanged" Width="100px">
                                <Calendar ID="cal1" runat="server" />
                                <DateInput ID="dip1" runat="server">
                                </DateInput>
                            </telerik:RadDatePicker>
                        </td>
                    </tr>
                    <tr>
                        <td style="width: 100px">
                            Zoekterm:
                        </td>
                        <td>
                            <asp:TextBox ID="tbZoek4" runat="server" Width="200"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <asp:Button ID="btnZoek4" runat="server" OnClick="btnZoek4_Click" CausesValidation="false"
                                Text="Zoek" />
                            <asp:Button ID="btnReset" runat="server" OnClick="btnReset_Click" CausesValidation="false"
                                Text="Reset" />
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </asp:Content>
    <asp:Content ContentPlaceHolderID="cphHelp" ID="content3" runat="server">
        <cc2:RoundedCornersExtender ID="RoundedCornersExtender2" runat="server" BorderColor="187, 187, 187"
            Radius="10" TargetControlID="divCorners2" Enabled="True">
        </cc2:RoundedCornersExtender>
        <div style="padding: 8px 8px 8px 8px;">
            <div runat="server" id="divCorners2">
                <table runat="server" id="Table1" cellpadding="3" cellspacing="3" width="100%">
                    <tr style="width: 100%">
                        <td style="width: 120px">
                            <b>Help:</b>
                        </td>
                        <td>
                              
                        </td>
                    </tr>
                    <tr style="width: 100%">
                        <td style="width: 120px">
                            Klik op regel:
                        </td>
                        <td>
                            Open afbeeldingen in Paint.Net
                        </td>
                    </tr>
                    <tr style="width: 100%">
                        <td style="width: 120px">
                            Shift+Klik op regel:
                        </td>
                        <td>
                            Open Map in Verkenner
                        </td>
                    </tr>
                    <tr style="width: 100%">
                        <td style="width: 100px">
                            Rechtsklik op regel:
                        </td>
                        <td>
                            Open context menu met meer opties
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </asp:Content>
    <asp:Content ContentPlaceHolderID="headJavaScript" ID="contentHeadJS" runat="server">
     
        <script type="text/javascript">
            function RowContextMenu(sender, eventArgs) {
                 
                if (eventArgs.get_domEvent().target.cellIndex == 20 || eventArgs.get_domEvent().target.nodeName == "IMG")
                    var menu = $find("<%=RadMenu2.ClientID %>");
                else
                    var menu = $find("<%=RadMenu1.ClientID %>");
     
                var evt = eventArgs.get_domEvent();
     
                if (evt.target.tagName == "INPUT" || evt.target.tagName == "A") {
                    return;
                }
     
                var index = eventArgs.get_itemIndexHierarchical();
                document.getElementById("radGridClickedRowIndex").value = eventArgs.getDataKeyValue("OID");
     
                sender.get_masterTableView().selectItem(sender.get_masterTableView().get_dataItems()[index].get_element(), true);
     
                menu.show(evt);
     
                evt.cancelBubble = true;
                evt.returnValue = false;
                if (evt.stopPropagation) {
                    evt.stopPropagation();
                    evt.preventDefault();
                }
     
            }
            function RowSelected_2(sender, eventArgs) { }
     
     
     
            function rowClick(sender, eventArgs) {
                debugger;
                if (eventArgs.get_domEvent().target.offsetLeft == 8) {
                    return;
                }
                if (eventArgs.get_domEvent().target.offsetLeft == 20) {
                    return;
                }
             
                if (eventArgs.get_domEvent().shiftKey)
                    launchVerkenner(eventArgs.getDataKeyValue("OID"));
                else if(eventArgs.get_domEvent().ctrlKey)
                    launchApp(eventArgs.getDataKeyValue("OID"));
            }
     
            function launchVerkenner(i) {
                var myfile = "F:\\ARCHIEF\\Archief_Originelen\\" + i;
     
                var strCmd = '"C:\\Windows\\explorer.exe" "' + myfile + '"';
                var obj = new ActiveXObject("LaunchinIE.Launch");
                obj.LaunchApplication(strCmd);
            }
     
            function launchApp(oid) {
     
                $.ajax({
                    type: "POST",
                    url: "wsGetImagePaths.asmx/getPaths",
                    data: "{'OID': '" + oid + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) {
                        AjaxSucceeded(msg);
                    },
                    error: AjaxFailed
                });
     
                var masterTable = $find("<%= RadGrid4.ClientID %>").get_masterTableView();
                masterTable.rebind();
            }
     
     
     
            function setImageClickedID(sender, args) {
                document.getElementById('ImageClickedID').value = document.getElementById("radGridClickedRowIndex").value;
     
            }
     
            function AjaxSucceeded(result) {
     
                var myfile = result.d;
                var strCmd = '"C:\\Program Files\\Paint.NET\\PaintDotNet.exe" ' + myfile;
                var obj = new ActiveXObject("LaunchinIE.Launch");
     
                obj.LaunchApplication(strCmd);
     
            }
     
            function AjaxFailed(result) {
     
                alert(result.status + ' ' + result.statusText);
     
            }
            function InitiateAjaxRequest() {
                var ajaxManager = $find("<%= RadAjaxManager2.ClientID %>");
                ajaxManager.ajaxRequest('');
            }
        </script>
     
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
         
        <telerik:RadAjaxManager ID="RadAjaxManager2" runat="server" UpdatePanelsRenderMode="Inline" OnAjaxRequest="ram_request">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadAjaxManager2">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid4" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
                <telerik:AjaxSetting AjaxControlID="RadGrid4">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid4" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <telerik:RadGrid ID="RadGrid4" runat="server" AllowPaging="False" EnableViewState="false"
            OnItemCreated="RadGrid4_ItemCreated" AllowSorting="True" GridLines="None" EnableEmbeddedSkins="true"
            OnNeedDataSource="RadGrid4_NeedDataSource" AutoGenerateColumns="False" OnItemDataBound="RadGrid1_ItemDataBound"
            Width="99%" Height="99%">
            <ClientSettings Scrolling-AllowScroll="true" Scrolling-UseStaticHeaders="true" AllowColumnsReorder="False"
                ReorderColumnsOnClient="False" EnableRowHoverStyle="true">
                <ClientEvents OnRowDblClick="RowSelected_2" OnRowClick="rowClick" />
                <Selecting AllowRowSelect="True" />
                <ClientEvents OnRowContextMenu="RowContextMenu"></ClientEvents>
            </ClientSettings>
            <MasterTableView ClientDataKeyNames="OID, Aanpassing, ParentOpdracht" Width="100%"
                TableLayout="Fixed" ShowFooter="true">
                <ExpandCollapseColumn Visible="False" Resizable="False">
                    <HeaderStyle Width="20px" />
                </ExpandCollapseColumn>
                <RowIndicatorColumn Visible="False">
                    <HeaderStyle Width="20px" />
                </RowIndicatorColumn>
                <FilterItemStyle Height="16px" Width="25px" />
                <Columns>
                    <telerik:GridBoundColumn AllowFiltering="False" DataField="AL" HeaderText="C0" UniqueName="AL"
                        HeaderStyle-Width="40px" ItemStyle-Width="40px">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn AllowFiltering="False" DataField="C1" HeaderText="C1." UniqueName="C1"
                        HeaderStyle-Width="40px" ItemStyle-Width="40px">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn AllowFiltering="False" DataField="PR" HeaderText="Pr." UniqueName="Pr"
                        HeaderStyle-Width="40px" ItemStyle-Width="40px">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn AllowFiltering="False" DataField="C2" HeaderText="C2." UniqueName="C2"
                        HeaderStyle-Width="40px" ItemStyle-Width="40px">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn AllowFiltering="False" DataField="A" HeaderText="Ar." UniqueName="AR"
                        HeaderStyle-Width="40px" ItemStyle-Width="40px">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn AllowFiltering="False" HeaderStyle-Width="80px" DataField="StartDate"
                        HeaderText="Start Date" DataFormatString="{0:dd-MM-yy}" UniqueName="column1">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn AllowFiltering="False" DataField="nVest" HeaderText="Vestiging"
                        HeaderStyle-Width="120px" DataFormatString="<nobr>{0}</nobr>" UniqueName="Vestiging">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn AllowFiltering="False" DataField="Adres" HeaderText="Adres"
                        HeaderStyle-Width="120px" DataFormatString="<nobr>{0}</nobr>" UniqueName="column1">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn AllowFiltering="False" DataField="Plaats" HeaderText="Plaats"
                        HeaderStyle-Width="120px" DataFormatString="<nobr>{0}</nobr>" UniqueName="column1">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn AllowFiltering="False" DataField="Duplicates" HeaderText="Duplicates"
                        UniqueName="dups">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn AllowFiltering="False" DataField="Aanpassing" HeaderText="Adjust"
                        UniqueName="Aanpassing" HeaderStyle-Width="60px">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn AllowFiltering="False" DataField="Producten" HeaderText="Products"
                        UniqueName="Producten" HeaderStyle-Width="110px">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn AllowFiltering="False" DataField="Opties" HeaderText="Options"
                        Aggregate="Count" FooterAggregateFormatString="Opdrachten: {0}" UniqueName="Opties">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn AllowFiltering="False" DataField="IsChecked" HeaderText="Checked"
                        UniqueName="IsChecked" HeaderStyle-Width="60px">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn AllowFiltering="False" DataField="Floors" HeaderText="Floors"
                        UniqueName="Floors" HeaderStyle-Width="60px">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn AllowFiltering="False" DataField="price" HeaderText="Price"
                        UniqueName="price" HeaderStyle-Width="60px">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn AllowFiltering="False" DataField="kortingen" HeaderText="Discount"
                        UniqueName="kortingen" HeaderStyle-Width="60px">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn AllowFiltering="False" DataField="PrijsExtraWerk" HeaderText="Morework"
                        UniqueName="Morework" HeaderStyle-Width="60px">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn AllowFiltering="False" DataField="price" HeaderText="Total"
                        UniqueName="Total" HeaderStyle-Width="60px">
                    </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn UniqueName="numcol" HeaderText="Prd.Huis">
                        <ItemTemplate>
                            <asp:TextBox ID="tbProdHuis" runat="server" Width="20" AutoPostBack="true" OnTextChanged="tbProdHuis_Changed"></asp:TextBox>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                     
     
                    <telerik:GridBoundColumn AllowFiltering="False" DataField="OID" Display="False" HeaderText="OID"
                        UniqueName="OID">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
        <telerik:RadContextMenu ID="RadMenu1" OnClientShowing="setImageClickedID" runat="server"
            Skin="Vista" OnItemClick="RadMenu1_ItemClick" CausesValidation="false">
            <Items>
                <telerik:RadMenuItem Text="Geen" ImageUrl="~/Images/status_wit.gif" Value="Geen" />
                <telerik:RadMenuItem Text="(RK) Retour Klant" Value="(RK)" ImageUrl="~/Images/status_oranje.gif" />
                <telerik:RadMenuItem Text="(RP) Retour Productie" Value="(RP)" ImageUrl="~/Images/status_oranje.gif" />
                <telerik:RadMenuItem Text="(EP) Eigen productie" Value="(EP)" ImageUrl="~/Images/status_groen.gif" />
                <telerik:RadMenuItem Text="(MF) Wachten op missing files" Value="(MF)" ImageUrl="~/Images/status_blauw.gif" />
                <telerik:RadMenuItem Text="(AP) Aanpassen files" Value="(AP)" ImageUrl="~/Images/status_rood.gif" />
                <telerik:RadMenuItem Text="(CP) Productie Controleren" Value="(CP)" ImageUrl="~/Images/status_wit.gif" />
                <telerik:RadMenuItem Text="(CP) Aanpassing intern" Value="(AI)" ImageUrl="~/Images/status_paars.gif" />
                <telerik:RadMenuItem Text="(FLPL) Wachten op FML" Value="(FL)" ImageUrl="~/Images/status_blauw.gif" />
                <telerik:RadMenuItem Text="Open de map" ImageUrl="~/Images/folder_out.png" Value="0"
                    PostBack="false" onClick="launchVerkenner(1);" />
            </Items>
        </telerik:RadContextMenu>
        <telerik:RadContextMenu ID="RadMenu2" OnClientShowing="setImageClickedID" runat="server"
            Skin="Vista" OnItemClick="RadMenu2_ItemClick" CausesValidation="false">
            <Items>
                <telerik:RadMenuItem Text="Productiehuis 1" Value="P1" ImageUrl="~/Images/status_rood.gif" />
                <telerik:RadMenuItem Text="Productiehuis 2" Value="P2" ImageUrl="~/Images/status_blauw.gif" />
                <telerik:RadMenuItem Text="Productiehuis 3" Value="P3" ImageUrl="~/Images/status_groen.gif" />
                <telerik:RadMenuItem Text="Productiehuis 4" Value="P4" ImageUrl="~/Images/status_oranje.gif" />
                <telerik:RadMenuItem Text="Productie Intern" Value="PI" ImageUrl="~/Images/status_wit.gif" />
            </Items>
        </telerik:RadContextMenu>
        <input type="hidden" id="ImageClickedID" name="ImageClickedID" />
     
        <input type="hidden" id="radGridClickedRowIndex" name="radGridClickedRowIndex" />
     
    </asp:Content>
  6. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 19 Oct 2010 Link to this post

    Hello Yeroon,

    The event is not firing because you have set the EnableViewState property of RadGrid as 'False'. So enabling ViewState will eliminate this issue.

    [Basically setting EnableViewState=false on controls disables view state for the Page object, meaning that neither view-state information for the page nor any controls contained by the page are saved.!]

    Thanks,
    Princy.
  7. Yeroon
    Yeroon avatar
    87 posts
    Member since:
    Oct 2012

    Posted 19 Oct 2010 Link to this post

    Hi Princy,

    Thank you very much. This was indeed the problem. I had turned it off since I didn't need it up till I added the textboxes. Silly me :)


    ?yeroon
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017