Thursday, January 31, 2008

Tip: How to enable post back from a custom web control?

In many scenarios I came across the need to use the __doPostBack function to do a post back for my web control or asp page. In order to do so, I have to inherit the control or page from the IPostBackEventHandler interface. However, this is not sufficient because the __doPostBack function will not be generated by the .NET FW. I have to add the "GetPostBackEventReference" function inside my control or page. The sample code below shows a simple implementation:


public classs MyControl: WebControl, IPostBackEventHandler
{
........
protected override void OnInit(EventArgs e)
{
PostBackOptions options = new PostBackOptions(this);
options.ClientSubmit = true;
Page.ClientScript.GetPostBackEventReference(options);
}
}


This enables the implementation of the following user functions:
1. Pass back key press from user computer back to server.
2. Any scenarios that require __doPostBack to be called manually in code.

Tuesday, January 29, 2008

Tip: Embed an Autocomplete Ajax Control into a Web Control

In order to embed an Autocomplete Ajax control in a custom web control, there is a rule that one needs to follow:


1. Place the Ajax control instance after the base.OnPreRender() function. The reason
is that Ajax control needs to get an instance of the Target Control when it's
created.

Tip: Closing an IE Modal Dialog

It's a tedious and time consuming task to find out actually how to close an IE modal dialog generated by calling the ShowModalDialog Javascript function. To make things short, i present only the codes:


//To show the modal dialog.
function DisplayMenu()
{
var url = "page.aspx";
var winName = "ProblemReportActions";
var height = 280;
var width = 500;
var returnedValue = '';
var params = '';
var returnedValue = ShowModalWindow( url, winName, width, height,params);
if(returnValue != null)
{
postBack(returnValue); //Javascript Post Back for the main window
}
}

//In the C# aspx page add the onclick attributes to the button to close the dialog.
//the first button actualy close the dialog and returns something to the main window.
//the second button basically closes the dialog like a cancel button.
this.bt_ok.Attributes.Add("onclick", "closeDialog('" + this.txtOption.UniqueID + "');return false;");
this.bt_cancel.Attributes.Add("onclick", "window.close();return false;");