PDA

View Full Version : [CLOSED] How to load formpanel textfields with data from sql database



ASAPCH
Nov 08, 2012, 10:32 PM
Hello Daniil, I would like ask you for help. I have formpanel with some textfield,datefield etc. and I need fill this formpanel with data from sql database by use of SqlDataReader object (or similar). I think that there is some method to databind & fill textfileds in FormPanel with record read from database. Thank you for help. ASAPCH


<ext:FormPanel
ID="formpanelBasic"
runat="server"
Title="Basic"
DefaultAnchor="100%"
BodyPadding="5">
<Items>
<ext:TextField ID="FirstName" runat="server" FieldLabel="First Name" Name="FirstName" />
<ext:TextField ID="LastName" runat="server" FieldLabel="Last Name" Name="LastName" />
<ext:DateField ID="EntryDate" runat="server" FieldLabel="Entry date" Format="yyyy-MM-dd" Name="EntryDate" />
</Items>
</ext:FormPanel>
.....
.....
SqlDataReader myDataReader = GetSQLRecordById(id);
if (myDataReader.Read())
{
formpanelBasic.SetValues(myDataReader);// this doesnt work
// following code I want to replace with one function call
this.FirstName.Text = myDataReader["FirstName"].ToString();
this.LastName.Text = myDataReader["LastName"].ToString();
this.EntryDate.Text = String.Format("{0:yyyy-MM-dd}", myDataReader["EntryDate"]); }
....

Daniil
Nov 09, 2012, 7:35 AM
Hi @ASAPCH,

Please use a Store to bind a data from a database. Then load a Store's record to a FormPanel. Here is an example.
http://examples2.ext.net/#/Form/Miscellaneous/Form_View/

ASAPCH
Nov 09, 2012, 1:21 PM
Hi @ASAPCH,

Please use a Store to bind a data from a database. Then load a Store's record to a FormPanel. Here is an example.
http://examples2.ext.net/#/Form/Miscellaneous/Form_View/

Hi Daniil, thanks for reply. Please do you know how to fill form from code behind? There are no methods getForm() & loadRecord () in ext:FormPanel object. Another question: is ext:TextField content bind with store/database column through atribute "Name" of TextField ?


<ext:TextField ID="FirstNameID" runat="server" FieldLabel="First Name" Name="FirstName" />

Thanks. ASAPCH

Daniil
Nov 09, 2012, 4:15 PM
Please do you know how to fill form from code behind? There are no methods getForm() & loadRecord () in ext:FormPanel object.


Please look at the example how you can use a SetValues method.

Example

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

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

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
var values = new
{
TextField1 = "1",
TextField2 = "2"
};

this.FormPanel1.SetValues(values);
}
}
</script>

<!DOCTYPE html>

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

<ext:FormPanel ID="FormPanel1" runat="server">
<Items>
<ext:TextField runat="server" Name="TextField1" />
<ext:TextField runat="server" Name="TextField2" />
</Items>
</ext:FormPanel>
</form>
</body>
</html>



Another question: is ext:TextField content bind with store/database column through atribute "Name" of TextField ?


<ext:TextField ID="FirstNameID" runat="server" FieldLabel="First Name" Name="FirstName" />


Yes, ModelField's Name is associated with field's Name.

ASAPCH
Nov 09, 2012, 5:51 PM
Please look at the example how you can use a SetValues method.

Example
[CODE]<%@ Page Language="C#" %>

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

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
var values = new
{
TextField1 = "1",
TextField2 = "2"
};

this.FormPanel1.SetValues(values);
}
}
</script>

Yes, ModelField's Name is associated with field's Name.

Ok Daniil, my case is following: I have infinite remote buffered grid with Store filled from SQL database, after user doubleclick on row detail form window(in ASCX, ASCX.cs) should be opened. I have tried:


public bool SetFormValues(int id) // DB read & fill form
{

var record = this.GridStore.GetById(id);
formpanelBasic.SetValues(record);
return true;
}

.. but this ended with crash. Another question is: method GetById(id) returns ModelProxy, but how can I get proper record data(columns from DB) from this?
Another choice is to do this not in code behind but in script:


var onItemDblClick = function (record, index, e ) {
#{TestDetail1}.formpanelBasic.getForm().loadRecord (record)
#{TestDetail1}.Show();
}
...
<Listeners>
<ItemDblClick Handler="onItemDblClick ( record, index, e );" />
</Listeners>
</ext:GridPanel>
...
<uc1:TestDetail ID="TestDetail1" runat="server" />

..but I have problem in reference to formpanelBasic ( is in detail window in separate ascx file )

Thanks. ASAPCH

Daniil
Nov 12, 2012, 8:39 AM
Ok Daniil, my case is following: I have infinite remote buffered grid with Store filled from SQL database, after user doubleclick on row detail form window(in ASCX, ASCX.cs) should be opened. I have tried:


public bool SetFormValues(int id) // DB read & fill form
{

var record = this.GridStore.GetById(id);
formpanelBasic.SetValues(record);
return true;
}

.. but this ended with crash.

Please use LoadRecord method.



Another question is: method GetById(id) returns ModelProxy, but how can I get proper record data(columns from DB) from this?


It is not submitted from client automatically. You can submit it manually. Or, at least, a record's id to retrieve the rest data from a database.



Another choice is to do this not in code behind but in script:

var onItemDblClick = function (record, index, e ) {
#{TestDetail1}.formpanelBasic.getForm().loadRecord (record)
#{TestDetail1}.Show();
}
...
<Listeners>
<ItemDblClick Handler="onItemDblClick ( record, index, e );" />
</Listeners>
</ext:GridPanel>
...
<uc1:TestDetail ID="TestDetail1" runat="server" />

..but I have problem in reference to formpanelBasic ( is in detail window in separate ascx file )


You can set IDMode="Static" for the FormPanel. Then its client reference will be:

App.FormPanelServerID

Obviously, it is not an option if you want to use multiple instance of a TestDetail control. In this case you should come up with another solution to manage client references.

By the way I would suggest you to look into MessageBus feature.
http://examples2.ext.net/#/search/messagebus