This section covers some of
the common programming tasks involved in building and modifying
Enterprise Portal controls and explains the technology behind them with
code samples.
AJAX
ASP.NET AJAX allows the
developer to create ASP.NET Web pages that can update data on the Web
page without completely reloading the page. ASP.NET AJAX provides
client-side and server-side components that use the XMLHttpRequest
object along with JavaScript and DHTML to enable portions of the page
to update asynchronously, again without reloading the entire page. With
ASP.NET AJAX, you can develop Enterprise Portal Web pages, just as you
would any regular ASP.NET page, and you can declaratively mark the
components that need to be rendered asynchronously.
Using the UpdatePanel
server control, you can enable sections of the Web page to be partially
rerendered without an entire page postback. The Dynamics User Control
Web part contains the UpdatePanel internally, and ScriptLibrary
is included in the master page, so any control that you use in the Web
User Control instantly leverages AJAX without you needing to write any
explicit markup or code.
For example, if you add a text box and button,
writing code for the button’s click event on the server, without AJAX,
when you click the button, the entire page refreshes. But when you load
the same control through the Dynamics User Control Web part, it
leverages AJAX and updates the text box without refreshing the entire
page.
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Button ID="Button1"
runat="server" onclick="Button1_Click" Text="Button" />
|
And in the code-behind, update the text box with the current time after 5 seconds.
protected void Button1_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(5000);
TextBox1.Text = System.DateTime.Now.ToShortTimeString();
}
|
If you want to override the AJAX behavior and force a full postback, you can use the PostBackTrigger control in your Web User Control, as shown here.
<%@ Register assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicK
eyToken=31bf3856ad364e35" Namespace="System.Web.UI" TagPrefix="asp" %>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
|
Session
All the Web parts in a Web page share the same
session in Dynamics AX. Once the page is served, the session is torn
down. To optimize performance, you can control the time frame for
tearing down the session through a web.config setting. For example, add
the Microsoft.Dynamics sectionGroup to web.config as in the following code.
<sectionGroup name="Microsoft.Dynamics">
<section name="Session" type="System.Configuration.SingleTagSectionHandler, System,
Version=1.0.5000.0, Culture=neutral,PublicKeyToken=b77a5c561934e089" />
</sectionGroup>
|
Add the <Microsoft.Dynamics> element under </system.web> and then add the following text to the file.
<Microsoft.Dynamics>
<Session Timeout="15" />
</Microsoft.Dynamics>
|
Many of the methods you use in the Enterprise Portal framework to add code to User Controls require access to the Session object. You need to pass the Session object when using proxy classes. You can access the Session object through the Web part hosting the User Control, as shown here.
AxBaseWebPart webpart = AxBaseWebPart.GetWebpart(this);
return webpart == null ? null : webpart.Session;
|
Context
Context is a data
structure used to share data related to the current environment and user
actions taking place with different parts of a Web application. Context
lets you know what’s happening in one control so you can react to it
via another control or Web part, or pass that information to a new page.
Generally, information about the current record the user is working on
forms the context. For example, when the user selects a row in a grid
view, other controls might need to get information about the newly
selected row to react.
AxContext is an abstract class that encapsulates the concept of the context. AxTableContext and AxViewContext derive from and implement AxContext. AxTableContext is for table-based context, and AxViewContext is for data set view context. A view can contain more than one table, so it contains an AxTableContext object for each table in the view in the TableContextList collection. The RootTableContext property returns the TableContext of the root table in that data set view. AxViewDataKey uniquely identifies the AxViewContext, and it contains the TableDataKeys collection. AxTableDataKey uniquely identifies AxTableContext. An event is raised whenever the context changes. If the context is changed within the Web User Control, the CurrentContextChanged event is raised. If the context changes from other Web parts that are connected to it, the ExternalContextChanged event is raised.
You can write code in these events on the AxBaseWebPart from your Web User Control and use the CurrentContextProviderView or ExternalContextProviderView and ExternalRecord
properties to get the record associated with the context. You can fire
all these events programmatically from your application logic by calling
FireCurrentContextChanged or FireExternalContextChanged so that all other connected controls could react to the change you made through your code.
Following is sample code to fire the CurrentContextChanged event.
void CurrentContextProviderView_ListChanged(object sender, System.ComponentModel.
ListChangedEventArgs e)
{
/* The current row (which is the current context) has changed -
update the consumer webparts.Fire the current context change event to refresh
(re-execute the query) the consumer webparts
*/
AxBaseWebPart webpart = this.WebPart;
webpart.FireCurrentContextChanged();
}
|
Sample code for getting the record from the connected Web part follows. First subscribe to the ExternalContextChanged event in the consumer Web User Control, as here.
protected void Page_Load(object sender, EventArgs e)
{
//Add Event handler for the ExternalContextChange event. Whenever selecting
//the grid of the provider Web part changes, this event gets fired.
(AxBaseWebPart.GetWebpart(this)).ExternalContextChanged += new
EventHandler<Microsoft.Dynamics.Framework.Portal.UI.AxExternalContextChangedEventArgs>
(AxContextConsumer_ExternalContextChanged);
}
|
Then get the record passed through the external context, as shown here.
void AxContextConsumer_ExternalContextChanged(object sender, Microsoft.Dynamics.
Framework.Portal.UI.AxExternalContextChangedEventArgs e)
{
//Get the AxTableContext from the ExternalContext passed through web part
connection and construct the record object
//and get to the value of the fields
IAxaptaRecordAdapter currentRecord = (AxBaseWebPart.GetWebpart(this)).
ExternalRecord;
{
if (currentRecord != null)
{
lblCustomer.Text = (string)currentRecord.GetField("Name");
}
}
}
|