Modifying a server control’s parent control collection

At work, I was tasked with creating a specialized version of the standard DetailsView server control. My job was to make sure the control would have all the properties and functionality that DetailsView have, and add some of our own. However, the challenge was that when our server control should render itself, it should wrap itself inside a Panel server control. In other words this:

<My:CustomDetailsView</My:CustomDetailsView>

Should render itself as:

<asp:Panel><My:CustomDetailsView></My:CustomDetailsView></asp:Panel>

Read the rest of this entry »

Comments off

Skinning dynamic controls in a custom server control

I was working on a server control which created several instances of the System.Web.UI.WebControls.Image class. I needed to set the SkinID to a value obtained when my control was data binding, but when I rendered the Image instances to the HtmlTextWriter, the Image controls were not skinned. After alot of searching and experimenting, I finally figured it out: The control has to be added to the Controls collection of the server control, to participate in the Theme and Skins process.

I hope this will help someone else, as I couldn’t find any specific info on this on the web.

Comments off

Scrolling the client to a specific control

Recently, in ASP.NET, I wanted to make a ‘smarter’ version of the standard “MaintainScrollPositionOnPostback”, so that I could tell the client which control to center the screen on. So I made a function that emits some simple JavaScript code that does just that. Take a look:

protected void ScrollClientToControl(WebControl wc)
  1.         {
  2.             scrollScript.Text =
  3.             @”&lt;script language='javascript' type='text/javascript'&gt;
  4.                var element = document.getElementById('" + wc.ClientID + @”');
  5.                var scrolly = element.offsetTop;
  6.                var elementHeight = element.clientHeight;
  7.                while (element.offsetParent != null)
  8.                {
  9.                    element = element.offsetParent;
  10.                    scrolly += element.offsetTop;
  11.                }
  12.                window.scroll(0, scrolly – ((document.documentElement.clientHeight – elementHeight) / 2));
  13.            &lt;/script&gt;";
  14.             scrollScript.Visible = true;
  15.         }

In the code above, ‘scrollScript’ is a literal control I put at the bottom of the page markup. This works pretty well. It enables me to scroll the client to any WebControl with a minimal amount of code.

Comments off

Bitmap fonts part 1 – Introduction

What is this?
I’ve started a multipart introduction (or tutorial) to bitmap fonts. This part will introduce you to bitmap fonts and explain in simple terms the idea behind bitmap fonts.

Read the rest of this entry »

Comments off