PDA

View Full Version : [CLOSED] How To Update Value of Dynamically Created Controls (With Dynamic ID's).



Tonic
Apr 23, 2013, 5:58 AM
In my scenario one TreeNode Click (Click1) event is creating some dynamic controls like TextField, ComboBox, NumberField etc. with values in it (Fetching From Database).
And Second Direct Event(UpdateEvent) is Updating Data in it (by passing Some SQL queries.)
Problem is that i Could not update it, as i could not retain ID of the Controls on Update Event. ID becomes null when it comes in Update Region.
please Help it.....!


aspx file


<ext:Panel ID="pnlOuterPropControls" runat="server" Width="620" Height="530">
<Items>
<ext:Panel ID="PnlPropControls" runat="server" Width="500" Height="530" PaddingSpec="20px 0px 0px 30px" AutoDataBind="true"
Border="false">
<Items>
</Items>
<Buttons>
<ext:Button ID="BtnUpdateProp" runat="server" Text="Update !" Width="100" OnDirectClick="BtnUpdateProp_Click"
Hidden="true">
</ext:Button>

</Buttons>
</ext:Panel>
</Items>
</ext:Panel>



code behind


public void Click1(object sender, DirectEventArgs e)
{
BtnUpdateProp.Show();

PnlPropControls.RemoveAll();
string TfNewParntHierName = e.ExtraParams["CmptMstrpk1"].ToString();
Session["sessionTfNewParntHierName"] = TfNewParntHierName;
string shrtdesc = fn.Selectsinglevalues("select accountShortDesc from tc_AccountCodes where pk_AccountCode = '" + TfNewParntHierName + "' ");

Session["shrtdesc"] = shrtdesc;
DataSet dsPg = fn.Selectdata("select * from dbo.tc_configuration where configPropCatgSub = '" + shrtdesc + "' order by configpropsrno asc");
for (int i = 0; i < dsPg.Tables[0].Rows.Count; i++)
{
string Controlpk = dsPg.Tables[0].Rows[i]["pk_configuration"].ToString();
string ControlType = dsPg.Tables[0].Rows[i]["configPropType"].ToString();
string ControlDataType = dsPg.Tables[0].Rows[i]["configPropDataType"].ToString();
string ControlItem = dsPg.Tables[0].Rows[i]["configPropItem"].ToString();

if (ControlType == "Numeric")
{
NumberField nFNumericData = new NumberField();
nFNumericData.ID = "nFNumericData_" + i.ToString();

nFNumericData.MaxValue = 100;
nFNumericData.MinValue = 0;
nFNumericData.Text = fn.Selectsinglevalues("select configPropValue from tc_configuration where pk_configuration = '" + Controlpk + "' ");
nFNumericData.FieldLabel = fn.Selectsinglevalues("select configPropItem from tc_configuration where pk_configuration = '" + Controlpk + "' ");
PnlPropControls.Items.Add(nFNumericData);
nFNumericData.Render();

}





}
}

public void BtnUpdateProp_Click(object sender, DirectEventArgs e)
{
DataSet ds = fn.Selectdata("select * from dbo.tc_configuration where configPropCatgSub = '" + Session["shrtdesc"] + "' order by configpropsrno asc");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string Controlpk = ds.Tables[0].Rows[i]["pk_configuration"].ToString();
string ControlType = ds.Tables[0].Rows[i]["configPropType"].ToString();
string ControlDataType = ds.Tables[0].Rows[i]["configPropDataType"].ToString();
string ControlItem = ds.Tables[0].Rows[i]["configPropItem"].ToString();
TextField tfDescription = (TextField)PnlPropControls.FindControl("tfDescription");
NumberField nFNumericData = (NumberField)PnlPropControls.FindControl("nFNumericData");


TextBox tbnnn = (TextBox)PnlPropControls.FindControl("tbnnn");

if (ControlType == "Numeric") // Checking condition "Values Stored in DB"
{
fn.updatedata(" update tc_configuration set configPropValue = '" + nFNumericData.Value + "' where pk_configuration = '" + Controlpk + "' ");
}





}




}

Daniil
Apr 23, 2013, 6:40 AM
Hi @Tonic,

If you create/render a control during one request (a DirectEvent), it doesn't persists automatically during another request (a DirectEvent). It is why you are getting nulls.

To update fields I would try the following.

X.GetCmp<TextField>("TextField1").Text = "new text";

Tonic
Apr 23, 2013, 7:38 AM
thanks for reply,

but sir problem is still the same, i am unable to use it..
can u please tell me where to place the code line you wrote.

And one more thing, ID of Controls are generating Automatilcally for about 100's of entries for different Controls (Depending upon DB)


i am using this code line to get values of number field and use it in update query

NumberField nFNumericData = (NumberField)PnlPropControls.FindControl("nFNumericData");


this is my update query

if (ControlType == "Numeric") // Checking condition "Values Stored in DB"
{
fn.updatedata(" update tc_configuration set configPropValue = '" + nFNumericData.Value + "' where pk_configuration = '" + Controlpk + "' ");
}

Daniil
Apr 23, 2013, 8:00 AM
Please take a look at the example.

Example

<%@ Page Language="C#" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">
protected void RenderTextField(object sender, DirectEventArgs e)
{
TextField tf = new TextField();
tf.ID = "TextField1";
tf.Text = "initial text";

tf.AddTo(this.Form);
}

protected void UpdateTextField(object sender, DirectEventArgs e)
{
X.GetCmp<TextField>("TextField1").Text = "new text";
}
</script>

<!DOCTYPE html>
<html>
<head runat="server">
<title>Ext.NET v2 Example</title>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />

<ext:Button runat="server" Text="Render TextField" OnDirectClick="RenderTextField" />

<ext:Button runat="server" Text="Update TextField" OnDirectClick="UpdateTextField" />
</form>
</body>
</html>

Tonic
Apr 23, 2013, 10:25 AM
thanks for your Consideration...!

but it is not working..in my code .

What Exactly i need is nFNumericData.Text which is needed in update query, but unfortunately it is comming null every time.


and also as you have hardcoded ID of textfield, but in my case it changes depending on DB , like: textfield1,combobox2,textfield3,numberfield4,textf ield5 in a go

please refer the code i gave....today


update tc_configuration set configPropValue = '" + nFNumericData.Text+ "' where pk_configuration = '" + Controlpk + "' ");

Daniil
Apr 23, 2013, 12:41 PM
Maybe, I misunderstand something.

Do you need to submit the field's values from client (browser) to server (code behind), then push those values to a database, correct?

Tonic
Apr 23, 2013, 1:09 PM
exactly sir...

Daniil
Apr 23, 2013, 3:46 PM
OK. To have access to the update text you should know a control's ID.

Example

<%@ Page Language="C#" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">
protected void RenderTextField(object sender, DirectEventArgs e)
{
TextField tf = new TextField();
tf.ID = "TextField1";
tf.Text = "initial text";

tf.AddTo(this.Form);
}

protected void Get(object sender, DirectEventArgs e)
{
X.Msg.Alert("Get", X.GetCmp<TextField>("TextField1").Text).Show();
}
</script>

<!DOCTYPE html>
<html>
<head runat="server">
<title>Ext.NET v2 Example</title>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />

<ext:Button runat="server" Text="Render TextField" OnDirectClick="RenderTextField" />

<ext:Button runat="server" Text="Get TextField's Text" OnDirectClick="Get" />
</form>
</body>
</html>

Tonic
Apr 24, 2013, 6:38 AM
thanks a lot... the code you suggested worked.!
the Problem is Resolved now..... :-)
i was using

PnlPropControls.Items.Add(dfMaxDate);

instead of

dfMaxDate.AddTo(this.PnlPropControls);


You may now Close the thread

Daniil
Apr 24, 2013, 7:28 AM
You can put a control to an Items collection instead of calling the AddTo method, but you also have to call its Render method.

Example

<%@ Page Language="C#" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">
protected void RenderTextField(object sender, DirectEventArgs e)
{
TextField tf = new TextField();
tf.ID = "TextField1";
tf.Text = "initial text";

this.Container1.Items.Add(tf);
tf.Render();
}

protected void Get(object sender, DirectEventArgs e)
{
X.Msg.Alert("Get", X.GetCmp<TextField>("TextField1").Text).Show();
}
</script>

<!DOCTYPE html>
<html>
<head runat="server">
<title>Ext.NET v2 Example</title>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />

<ext:Container
ID="Container1"
runat="server"
Width="200"
Height="100" />

<ext:Button runat="server" Text="Render TextField" OnDirectClick="RenderTextField" />

<ext:Button runat="server" Text="Get TextField's Text" OnDirectClick="Get" />
</form>
</body>
</html>

jamesand
Jul 20, 2013, 9:59 AM
Hi Daniil and Tonic, I'm trying to do something similar in my project and I found the same problem as Tonic.
Searching for a solution I run into this thread and the example code you wrote here doesn't work for me in VB...
When I press "Get TextField's Text" it crashes: System.NullReferenceException.

Here it is my code:



<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">
Public Sub RenderTextField(ByVal sender As Object, ByVal e As Ext.Net.DirectEventArgs)

Dim tf As New TextField()
tf.ItemID = "TextField0"
tf.Text = "initial text"

tf.AddTo(divDetallesNuevoGrupoAbonos)

End Sub

Public Sub Gett(ByVal sender As Object, ByVal e As Ext.Net.DirectEventArgs)

Ext.Net.X.Msg.Alert("Get", DirectCast(Ext.Net.X.GetCmp("TextField0"), Ext.Net.TextField).Text).Show()

End Sub
</script>

<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
<title>Ext.NET v2 Example</title>
</head>
<body>
<form id="Form1" runat="server">
<ext:ResourceManager ID="ResourceManager1" runat="server" />

<ext:Button ID="Button1" runat="server" Text="Render TextField" OnDirectClick="RenderTextField" />

<ext:Button ID="Button2" runat="server" Text="Get TextField's Text" OnDirectClick="Gett" />

<ext:Panel
ID="divDetallesNuevoGrupoAbonos"
runat="server"
Title="Grupo de abonos"
AutoScroll="true"
Height="300">
</ext:Panel>

</form>
</body>
</html>



Am I missing something??

Vladimir
Jul 20, 2013, 10:09 AM
GetCmp works with ID of control, not ItemID

jamesand
Jul 20, 2013, 10:34 AM
It still gives me the same error. Code modified:


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">
Public Sub RenderTextField(ByVal sender As Object, ByVal e As Ext.Net.DirectEventArgs)

Dim tf As New TextField()
tf.ID = "TextField0"
tf.Text = "initial text"

tf.AddTo(divDetallesNuevoGrupoAbonos)

End Sub

Public Sub Gett(ByVal sender As Object, ByVal e As Ext.Net.DirectEventArgs)

Ext.Net.X.Msg.Alert("Gett", DirectCast(Ext.Net.X.GetCmp("TextField0"), Ext.Net.TextField).Text).Show()

End Sub
</script>

<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
<title>Ext.NET v2 Example</title>
</head>
<body>
<form id="Form1" runat="server">
<ext:ResourceManager ID="ResourceManager1" runat="server" />

<ext:Button ID="Button1" runat="server" Text="Render TextField" OnDirectClick="RenderTextField" />

<ext:Button ID="Button2" runat="server" Text="Get TextField's Text" OnDirectClick="Gett" />

<ext:Panel
ID="divDetallesNuevoGrupoAbonos"
runat="server"
Title="Grupo de abonos"
AutoScroll="true"
Height="300">
</ext:Panel>

</form>
</body>
</html>

Vladimir
Jul 20, 2013, 4:01 PM
You have to use generic version of GetCmp method


X.GetCmp<TextField>("TextField1").Text

jamesand
Jul 20, 2013, 11:44 PM
If I write X.GetCmp it gives me a compilation error:

'GetCmp' is not a member of 'Ext.Net.BuilderFactory'

Vladimir
Jul 21, 2013, 9:42 AM
Well, Try full name (it seems that in VB, X is translated to extension method X())


Ext.Net.X.GetCmp<TextField>("TextField1").Text

jamesand
Jul 21, 2013, 10:46 AM
I already tried it:


DirectCast(Ext.Net.X.GetCmp("TextField0"), Ext.Net.TextField).Text

And it crashes System.NullReferenceException (the control doesn't exist)

Vladimir
Jul 21, 2013, 11:26 AM
As I said before, you need to use generic version of GetCmp

jamesand
Jul 21, 2013, 2:10 PM
Vladimir I'm sorry but I think I don't understand you.

If I use the generic version of GetCmd:


DirectCast(X.GetCmp("TextField0"), Ext.Net.TextField).Text

it gives me a compilation error ('GetCmp' is not a member of 'Ext.Net.BuilderFactory')


And If I use the full name:


DirectCast(Ext.Net.X.GetCmp("TextField0"), Ext.Net.TextField).Text

it crashes System.NullReferenceException (the control doesn't exist).

So, what do you mean now to use the generic version of GetCmd?

jamesand
Jul 21, 2013, 9:24 PM
Ok It works with:


Ext.Net.X.Msg.Alert("Gett", Ext.Net.X.GetCmp(Of Ext.Net.TextField)("TextField0").Text).Show()

I didn't know that that is different to


Ext.Net.X.Msg.Alert("Gett", DirectCast(Ext.Net.X.GetCmp("TextField0"), Ext.Net.TextField).Text).Show()

Thanks for your help

Daniil
Jul 22, 2013, 8:09 AM
The difference is DirectCast and Of Ext.Net.TextField. The second case is a generic version of the GetCmp method, which Vladimir was talking about. A generic version of GetCmp and a non-generic one are the different methods.