Developing any non-trivial ASP.NET User Control involves defining properties, events, binding methods ...
Even the simplest Calculator User Control takes some time to be developed :(
Fist, let's add the code to fill the operationsList list with the possible operations.
Binding to a List of Enum Values
Recall that the TOperations enumeration was defined as:type TOperation = (Add, Subtract, Multiply, Divide) ;Here's a custom BindOperationList procedure you call in the User Control's Init event handler to data bind the DropDownList with all possible enumeration values:
procedure TCalculator.BindOperationList;While there is much more code in the TCalculator's implementation, here's just the calcButton's Click event handler (part of it):
var
opArray : System.&Array;
begin
opArray := Enum.GetValues(typeof(TOperation)) ;
operationsList.DataSource := opArray;
operationsList.DataBind;
//no need for items to go into the view state as we are binding the list on each postback
operationsList.EnableViewState := false;
end;
procedure TCalculator.calcButton_Click(sender: System.Object; e: System.EventArgs) ;The Operation property gets the selected operation from the operationsList SelectedValue by converting a string value to an enum value
begin
//should add error handling / type checking here
case Operation of
Calculator.Add : resultLabel.Text := Convert.ToString(System.Int32.Parse(textBox1.Text) + System.Int32.Parse(textBox2.Text)) ;
Calculator.Subtract: ... ;
Calculator.Multiply: ... ;
Calculator.Divide: ... ;
end;
function TCalculator.GetOperation: TOperation;Note: the rest of the Calculator's implementation code is included in the accompanying source code.
var
sOp : string;
begin
sOp := operationsList.SelectedValue;
//cast it to TOperation
result := TOperation(Enum.Parse(typeof(TOperation),sOp)) ;
end;
Adding a User Control to a Page
Once you have defined the look and the programming logic for your user control, you decide to add it to a page by loading it dynamically.When to "LoadControl"?
You should almost always load User Controls in the Page's Init event handler.If you decide to dymaically add a User Control in the Page's Load event handler, the User Control might not work correctly. The reason to use the Init event is related to understanding ViewState and Postback Processing in ASP.NET applications
For example: the BackColor property of the Calculator User Control wraps the Panel's BackColor proerty. Since this property for the Panel component is stored in the ViewState, if you set the BackColor property after the LoadViewState, that is in the Page's Load event, the previous BackColor value will be lost.
Here's a procedure you need to call in the Page's Init event handler to succesfully load the Calculator user control.
procedure TUserControlTest.AddCalculator;After we "load" the Calculator, using the LoadControl method, we need to place the User Control inside some of the container type ASP.NET components (like a Panel).
var
calc : TCalculator;
begin
calc := TCalculator(Page.LoadControl('~/Controls/Calculator.ascx')) ;
Panel1.Controls.Add(calc) ;
calc.BackColor := Color.Green;
calc.Operation := TOperation.Add;
calc.ID := 'calc';
//add 2 event handlers
Include(calc.CalculationComplete, HandleCalcComplete2) ;
Include(calc.CalculationComplete, HandleCalcComplete) ;
end;
The code above also assigns two event handling procedures for the CalculationComplete event of the TCalculator class. Since .Net events are multicast, the Include and Exclude standard procedures are used to include and exclude event handlers at runtime.
Here's the implemntation of one of the event handling procedures, chaning the BackColor property depending on the selected operation:
procedure TUserControlTest.HandleCalcComplete2(Sender: TObject) ;
var
calc : TCalculator;
begin
calc := Panel1.FindControl('calc') as TCalculator;
case calc.Operation of
Calculator.Add: calc.BackColor := Color.Yellow ;
Calculator.Subtract: calc.BackColor := Color.Navy;
Calculator.Multiply: calc.BackColor := Color.Aqua;
Calculator.Divide: calc.BackColor := Color.Red;
end;
end;
Make sure you download the source code accompanying this chapter. Try placing a call to AddCalculator procedure in the Page Load event handler .. add some properties of your own ...
To the next chapter...
A Beginner's Guide to ASP.NET Programming for Delphi developers: Next Chapter >>>> Table Of Contents


