Wednesday, August 20, 2008

Show Ellipsis Text in GridView

protected void Page_Load(object sender, EventArgs e)
{
// ... ...
gv1.Style["TABLE-LAYOUT"] = "fixed";
}

protected void gv1_RowCreated(object sender, GridViewRowEventArgs e)
{
foreach (TableCell cell in e.Row.Cells)
{
cell.Wrap = false;
cell.Style["overflow"] = "hidden";
cell.Style["text-overflow"] = "ellipsis";
}
}

// Showing full text tooltip in each cell
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach (TableCell cell in e.Row.Cells)
{
cell.ToolTip = cell.Text;
}
}

The above code just shows what style settings need to be set for the ellipsis effect. It is not recommended to write product code like this. Keep in mind that
gv1.Style["TABLE-LAYOUT"] = "fixed";
actually sets "TABLE-LAYOUT" style property of HTML Table. Therefore, this should be done in a CSS file: not only to separate UI part from logic part, but to make the data travel between server and client much light-weighted. (To demonstrate the last part, look at the source of the rendered HTML to see the table cell styles are duplicated across all the tags.)

Monday, August 18, 2008

Register Custom Control in Web Page

When we want to use a User Control (which is defined in *.ascx) in a web page, we can register the control like this:
<%@ Register Src="~/MyObjectDisplayer.ascx" TagName="MOD" TagPrefix="CustomControl" %>

If we use a Custom Control (which is defined in *.cs), however, the register tag should be a little different:
<%@ Register Namespace="MyControls" TagPrefix="CustomControl" %>

Whenever we want to place this control in the webpage, we write:
<CustomControl:PromptButton id="pb1" runat="server"/>

It assumes that our control classes is PromptButton and it is in MyControls namespace. And note that it has been tested in ASP.NET Web Site project, but not in ASP.NET Web Application project.

Tuesday, August 5, 2008

The Statelessness of Web Page

In ASP.NET, it is common to have some data members of our Page class (one who derived from System.Web.UI.Page). It is important to keep in mind that those members are re-defined or re-declared every time a new instance of our Page class is created. This happens each time there is a round-trip between the server and the browser (i.e. but may not limited to: Page_Load event). Therefore, we need to be aware of the state of those data members.

One more thing: even if an UpdatePanel is used in a page, Page_Load event still gets fired every time.