PDA

View Full Version : [CLOSED] Can not access dynamically created asp.net classic controls' values



gumush75
Nov 16, 2016, 7:32 AM
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();
}

}
}

fabricio.murta
Nov 17, 2016, 1:09 AM
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.

gumush75
Nov 17, 2016, 3:00 AM
Hi fabricio, code edited and TdfManager issue resolved. Could you check again ? Urgent help needed.

fabricio.murta
Nov 17, 2016, 7:32 PM
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 (http://forums.ext.net/showthread.php?59271&p=271491#post271491).

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 (http://examples2.ext.net/#/Events/DirectMethods/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.

gumush75
Nov 17, 2016, 10:14 PM
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 ?

fabricio.murta
Nov 18, 2016, 8:02 PM
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 (http://stackoverflow.com/questions/14985812/creating-table-rows-in-codebehind).

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.

gumush75
Nov 18, 2016, 9:05 PM
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 ?

24785

fabricio.murta
Nov 21, 2016, 3:37 PM
Hello @gumush75!

Have you reviewed the various grid panel examples we have in our examples explorer for Ext.NET v2 (http://examples2.ext.net/)? I am pretty confident this examples highlight how to do what you just asked: GridPanel - Miscellaneous - Grid TotalRow (http://examples2.ext.net/#/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!

fabricio.murta
Dec 02, 2016, 2:31 PM
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!