[CLOSED] Can not access dynamically created asp.net classic controls' values

  1. #1

    [CLOSED] Can not access dynamically created asp.net classic controls' values

    Hi, in my project as a part of requirements i am creating a little bit complex table. To do that i am using asp.net Table control and creating table dynamically with code below.

    I am creating table and adding rows it but when click on ext.net button with direct event, I cannot access the table's rows.

    My .aspx page uses a masterpage with resourcemanager(rsm). Rsm's configuration is shown below.


    Could you check the code snippets below and what it is wrong with my code to access table's content ?

    Version: Ext.net 2.5.2

    
    <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <link rel="shortcut icon" type="image/png" href="/favicon.ico"/>
    
        <title></title>
        <style>
            .horizontal-menu .x-menu-item-link {
                line-height: 20px;
            }
    
            .horizontal-menu .x-menu-item-arrow {
                display: none;
            }
        </style>
        <asp:ContentPlaceHolder ID="head" runat="server">
        </asp:ContentPlaceHolder>
    </head>
    <body>
        <form id="form1" runat="server">
            <ext:ResourceManager ID="rsm" runat="server" AjaxTimeout="120000" 
                ShowWarningOnAjaxFailure="false" DisableViewState="false">
            </ext:ResourceManager>
            <ext:Viewport runat="server" ID="vwPortMaster" Layout="BorderLayout">
                <Items>                
                    <ext:Panel runat="server" Region="Center" Header="false" Layout="FitLayout" Border="false" AutoScroll="true">
                        <Content>
                            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
                            </asp:ContentPlaceHolder>
                        </Content>
                    </ext:Panel>                
                </Items>
            </ext:Viewport>
        </form>
    </body>
    </html>
    
    <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="qTest.aspx.cs" Inherits="qTest" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    
        <style type="text/css">
            table {
                border-collapse: collapse;
            }
    
            table, th, td {
                border: 1px solid black;
                font-family: Arial, Helvetica, sans-serif;
                font-weight: normal;
            }
        </style>
    
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    
        <ext:Panel runat="server" ID="p" Title="LIST" AutoScroll="true">
            <TopBar>
                <ext:Toolbar runat="server" ID="tlb">
                    <Items>
                        <ext:Button ID="btnSave" runat="server" Text="SAVE" ViewStateMode="Enabled">
                            <DirectEvents>
                                <Click OnEvent="btnSave_Click">
                                    <Confirmation ConfirmRequest="true" Message="Save ?"></Confirmation>
                                    <EventMask ShowMask="true" Msg="Saving..."></EventMask>
                                </Click>
                            </DirectEvents>
                        </ext:Button>
                    </Items>
                </ext:Toolbar>
            </TopBar>
            <Items>
                <ext:Container ID="Container1" runat="server" AutoScroll="true">
                    <Content>
                        <asp:Table runat="server" ID="t" Width="100%">
                        </asp:Table>
                    </Content>
                </ext:Container>
            </Items>
        </ext:Panel>
    </asp:Content>
    
    

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Drawing;
    
    using Ext.Net;
    
    public partial class qtest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.Search();
            }
        }
    
        private void Search()
        {
            this.CreateFirstRow();
            this.Container1.Render();
        }
    
        private Random rnd = new Random();
    
        private void CreateFirstRow()
        {
            TableHeaderRow tr = new TableHeaderRow();
    
            TableHeaderCell hc = new TableHeaderCell();
            hc.Text = @"ITEMS";
            hc.ColumnSpan = 3;
            hc.Width = new Unit(250, UnitType.Pixel);
    
            tr.Cells.Add(hc);
    
            //TdfManager tm = new TdfManager();
            //DataTable dtCust = tm.GET_TDF_CustList(1);
            //DataTable dt = tm.GET_TDF_By_Id(1);
    
            DataTable dtCust = new DataTable();
            dtCust.Columns.Add("cust");
            dtCust.Columns.Add("custId");
    
            DataRow dr1 = dtCust.NewRow();
            dr1[0] = "Cust1";
            dr1[1] = "1";
            dtCust.Rows.Add(dr1);
    
            DataRow dr2 = dtCust.NewRow();
            dr2[0] = "Cust2";
            dr2[1] = "2";
            dtCust.Rows.Add(dr2);
    
    
            DataRow dr3 = dtCust.NewRow();
            dr3[0] = "Cust3";
            dr3[1] = "3";
            dtCust.Rows.Add(dr3);
    
    
            foreach (DataRow dr in dtCust.Rows)
            {
                string cust = dr[0].ToString();
                string custId = dr[1].ToString();
                TableHeaderCell hc2 = new TableHeaderCell();
                hc2.Text = cust;
                hc2.ID = custId;
                hc2.ColumnSpan = 6;
                Color randomColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
                hc2.BackColor = randomColor;
                hc2.Width = new Unit(150, UnitType.Pixel);
                tr.Cells.Add(hc2);
            }
    
            t.Rows.Add(tr);
        }
    
    
        protected void btnSave_Click(object o, DirectEventArgs e)
        {
            int x = t.Rows.Count;
    
            if(x == 0)
            {
                Ext.Net.MessageBox msgBox = new MessageBox();
                msgBox.Configure(new MessageBoxConfig()
                {
                    Title = "ERROR",
                    Message = "No rows found",
                    Icon = MessageBox.Icon.ERROR,
                    Buttons = MessageBox.Button.OK
    
                });
                msgBox.Show();
            }
    
        }
    }
    Last edited by fabricio.murta; Jan 03, 2017 at 9:57 PM. Reason: no user feedback for 7+ days
  2. #2
    Hello @gumush75!

    Can you provide the namespaces you are importing to your code behind? My VS is not resolving TdfManager nor DataTable. I may just try and search the common namespace for them but there's a chance I find the wrong namespace and that would lead to a not so helpful feedback for your issue.
    Fabrício Murta
    Developer & Support Expert
  3. #3
    Hi fabricio, code edited and TdfManager issue resolved. Could you check again ? Urgent help needed.
  4. #4
    Hello! Still can't run your code here!..

    Can you review the code so it runs? You should either provide the necessary using lines and also code that should be included, or remove the code that is not relevant to reproduce the issue. Maybe you can reproduce your issue with a simple ASP.NET textbox inside Ext.NET container?

    To the current version you didn't provide namespaces for DataRow, DataTable, Color and also I believe 'rnd' is an instance of a class to generate random numbers but, guessing things will only lead us to a code that potentially differs from you, that's why it's so important to provide runnable test cases.

    This post suggests you should be able to have both Ext.NET and ASP.NET controls in the same page and access them from code behind as well: Post #2 on HTML controls not accessible from code behind when EXT.net controls exist in the aspx page.

    You should be able to access the table rows the same way you would with a normal ASP.NET application. As the table and rows are not an Ext.NET component, probably it is not synced on Ajax calls when you call the DirectMethod or DirectEvent. In such cases, you have to send them as parameters to the direct method or event.

    This example shows how to pass arguments to a direct method: Events - DirectMethod - Overview

    Parameters to direct events are passed with ExtraParam or ExtraParameter blocks.

    I hope the directions above helps if you are unable to provide a working test case by now.
    Fabrício Murta
    Developer & Support Expert
  5. #5
    You are right, build errors are fixed. Could you check code again ?

    At ext.net button click event, how can i pass asp.net table control as a parameter ?
  6. #6
    Hello @gumush75!

    You can't pass controls as parameters from client to server side, only thing that can be passed is controls' data.

    I have given a look on how the DataTable works and it seems it does (in a lower level manner) the same thing you would do with Ext.NET's grid panels so, unless you need to keep interoperability with legacy applications, you should be using those grids, which we can support.

    I have given a little search on google and I am still not very sure how you would submit back asp tables' when you dynamically create its rows. I found this stackoverflow question that will probably help you with the issue (and understanding the whole scenario): Creating table rows in code behind.

    This stackoverflow question above enforces the affirmation that Ext.NET does not get into the merit of inhibiting data from code behind, but that's a ASP.NET pages life cycle limitation that should be dealt with by the developer.

    Most of this work it exactly what Ext.NET does with its own controls, and that's why I suggested you trying to use Ext.NET data handling and components instead. Not cause Ext.NET breaks them, just that Ext.NET does not implement the whole work to move data back to code behind as asp:Table is not an Ext.NET component at all.

    Now, replying in parts your inquiry, to pass parameters to the directEvent (which triggers when you click the btnSave button) you should add to the click block (lines 26-29 in your webform file) the <ExtraParams> block with its parameters list.

    You will have to draw a JavaScript function to draw the data out of the asp table and url encode it. An outline of what needs to be done is this:

    1. To the Click block
    <ExtraParams>
        <ext:Parameter Name="tableData" Value="function() { return getAspTableData(); }" Mode="Raw" />
    </ExtraParams>
    The function() { return getAspTableData(); } will be interpreted every time you click the button. If you don't wrap the JavaScript method in a function body, it will be interpreted during page load and the value will never update between submissions or ajax calls.

    2. Outline the JavaScript to get the data from the asp table:
    <script type="text/javascript">
        var getAspTableData = function () {
            // get the tabe 't' data
            // serialize/stringify it and url encode/escape
            // return the result
        }
    </script>
    I hope this helps! If you can get the js code to grab the table data and still can't submit it, let us know, we'll help you take it thru code behind. But again, I believe a GridPanel is what you need.
    Fabrício Murta
    Developer & Support Expert
  7. #7
    Thanks for your reply. I will be trying it.

    Could you please explain how can i built a gridpanel as in attachment.

    In attachment single column and its custom summary and calculation rows added, in real application it will be dynamically adding.

    I handled dynamically adding columns and building gridpanel and its store dynamically. As to footer - its custom text, summary and calculation- rows which is in red at attachment how can i add these rows ?

    Click image for larger version. 

Name:	gpCustom.jpg 
Views:	93 
Size:	20.0 KB 
ID:	24785
    Last edited by gumush75; Nov 21, 2016 at 2:53 PM.
  8. #8
    Hello @gumush75!

    Have you reviewed the various grid panel examples we have in our examples explorer for Ext.NET v2? I am pretty confident this examples highlight how to do what you just asked: GridPanel - Miscellaneous - Grid TotalRow.

    Browsing the examples is probably a very good practice to have an overview of what can be done with Ext.NET and although you can browse thru examples not useful now, you'll probably remember one or another when you have other pages to make using Ext.NET.

    Hope this helps!
    Fabrício Murta
    Developer & Support Expert
  9. #9
    Hello again @gumush75!

    Been a few days since we replied to this thread and still no feedback from you. Did you progress on your issue? Maybe you still needs help with it? We're looking forward for your feedback!
    Fabrício Murta
    Developer & Support Expert

Similar Threads

  1. Help with controls created dynamically.
    By Master15 in forum 3.x Help
    Replies: 3
    Last Post: Feb 20, 2016, 3:56 AM
  2. saving dynamically-created controls' values
    By Skizzot223 in forum 1.x Help
    Replies: 1
    Last Post: Apr 16, 2012, 12:54 PM
  3. [CLOSED] Unable to access dynamically created controls
    By rnachman in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Apr 07, 2011, 5:49 AM
  4. Replies: 2
    Last Post: Jan 03, 2011, 5:34 PM
  5. Dynamically created controls cookbooks
    By arodier in forum 1.x Help
    Replies: 15
    Last Post: May 07, 2010, 7:12 PM

Tags for this Thread

Posting Permissions