Wednesday, May 20, 2009

Validation control and static asterix display

This is one of the most common problems that every programmer faces with the validation controls available in ASP.net. I am taking the example of RequiredFileldControl validation control to explain the problem and a simple solution to it.

In web forms we often create text boxes to access input from the user. Some of these text boxes require the values to be entered before the form is submitted. To Indicate it as a mandatory field the normal practice has been to show a red asterix besides the text box.

The RequiredFileldControlcontrol has two properties ErrorMessage and Text.

ErrorMessage: This property is used to display the error message in the ValidationSummary control when the form is submitted and user has not given any text in the text box

Text: We can use this property to show the asterix that we talked about in the above paragraphs.

So where is the problem?

The problem is that if you put an asterix character in the Text property, it will only show only after you submit the form and not before that. But this is not what you wanted. You wanted the asterix to be always displayed to indicate it as a mandatory field.

Soulution #1 (That does not work as expected)

One of the solution is to type an asterix character besides the text box and change the color to red and keep the "Text" property blank.

The problem is that if you keep the Text property blank, when you submit the form, the text inside the property "ErrorText" is displayed. This means you will see an asterix symbol (that you cretad manually) and the Error message. This doesnt look good!. So you cannot leave the "Text" property blank because you do not want the error text message to appear at two places 1) validation summary control and 2) besides the text box.

Solution #2

The solution is to keep the "Text" property blank and somehow make the RequiredFieldControl not to display the error message if it is blank. How can we do this?. The solution simple. Follow these steps.

1) Drag a RequiredFieldControl to the form.
2) Set the Text property to any text. For example give a value "Dummy" (without quotes)
3) Switch to source view where you can see the HTML code.
4) Search for the value you entered for the text field. Example here it is text "Dummy".
5) Select this text and replace it with  
6) Done!

Please remember you still have to type the static asterix with red color besides the text box.

hope this helps! ;)

Friday, October 26, 2007

Connecting to a server with dynamic IP address

Here is a link to a nice discussion on a problem faced by a user who wants to connect his client software to a server that doesnt have a static IP address

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_22919918.html

There is a reference to some nice and FREE online utilities (DynDNS) that are worth noting.

Wednesday, October 24, 2007

Event does not fire unless control is created in memory

I stumbled across a very interesting problem in ASP.net. Say you have one Repeater and you have put a LinkButton for a cell in every row. The LinkButton has a eventHandler for its onClick method. The interesting thing is that, the click event will not be fired unless the linkbutton inside the repeater control is recreated.

It means that unless you repopulate the repeater which will create a linkButton in every row, the click event will not be evaluated. This is happening because, unless the server controls (here the linkbutton) is created in the memory, the click event cannot be tied to the control.

To test this, just populate a repeater control with linkbutton in the load event only when the page is loaded for the first time as in the following statment

if (!isPostback)
{

//Fill the repeater
}


//Write event handler to handle click event of the linkbutton

The above will cause the repeater control to appear on the page. After this, click the link button. You will see that the Click Event handler is not fired because the data has not been bound to the repeater and therefore no linkbutton exist.

Sunday, October 21, 2007

Value type and reference type differences

I have been reading a lot about the differences between value type and reference type elsewhere in the internet. The concept is simple if tried to understand the memory allocation for these two in the memory. A value type is allocated in a stack (first in last out fashion). A reference type object is allocated in a heap and a reference object to this space in the heap is allocated in the stack.

Then interesting thing is that once this reference to the heap goes out of scope how does the stuff in heap get cleaned. This is where the garbage collector comes into action. I read a nice article on albahari's (http://www.albahari.com/value%20vs%20reference%20types.html) web site that tells us how to get these space de-alllocated automatically from the heap.

If an object can be added to a container that is aware about its job to clean or destroy all its children then we do not need an exclusive call to the dispose method of the object itself.

For example if we create a text box, we need to first add it to the container control. Now when the form itself gets destroyed, it will automatically remove the textbox details from the heap!.

This means that if we create a text box like in the following statment

TextBox myText= new TextBox();
myForm.Controls.Add(myText);
myText = null;

In the above code the text box does not get destroy even though we have assigned null value to myText reference object. The GB will detect that the textbox in the heap is being referenced by the Form! (myForm.Controls.Add())

Tuesday, September 4, 2007

why are interfaces used?

Interfaces in OOPs are very powerful when it is used wisely. One of the best uses of an interface is to decouple the classes or reduces the dependency between two classes. The following example illustrates a scenario where interfaces can be used to decouple two classes, management and employee type classes.

Let us take an example of a company which has a managment entity and other workers like clerks and engineers. Assume that the management wants to find out the manager of these two worker entities from the OOPs point of view.

Bad example.

A naive programmer would implement something like this

class management
{

public string manager(clerk clerkObject)
{

return clerkobject.manager();
}

public string manager(engineer engineerObject)
{

return engineerObject.manager();
}


}


class clerk
{

public string manager()
{
return "Pradeep";
}

}


class engineer
{

public string manager()
{
return "Pramod";
}
}

The above code will return the name of the manager heading the clerk or engineer class of employees. Even though the code returns the correct result, the design is not good from OOPs point of view. A better way would be the following


class management
{

public string manager(IManager managerObj)
{

return managerObj.manager();
}

}


interface IManager
{
string manager();
}


class clerk:IManager
{

public string manager()
{
return "Pradeep";
}

}


class engineer:IManager
{

public string manager()
{
return "Pramod";
}
}

In the above example we have decoupled the managemened class from the Clerk and engineer class, at the same time getting the result that we want.

Sunday, September 2, 2007

Thin line in HTML using CSS

I really like the power of CSS. We often find some visual elements on the HTML pages, that may look simpler, but we cannot create them ourselves with the basic HTML elements.

One of these visual object is a very thin line which is not possible through the simple HR tag the HTML. We need to use the power of CSS in order to draw a thin line. Following is how we can do this.

Just define the following style sheet element between the head element in the HTML page

<style type="text/css">

hr{height:1px}

</style>

After you have defined this, everytime you use a


tag, a thin line will be displayed instead of a the default think line.

CSS is really powerful if used properly.

Accessing Parent page objects from User Control

Sometimes a user control needs to access some public methods or variables or any other objects for that matter in its parent page. The user control provides the Page object which gives access to its container parent. The problem with this page object is that, it is the general page object. This means that this page object doesnt contain the necessary type details which will enable you to access the object in the required parent container.

In order to achieve this, the general page object has to be type casted to its parent type after which the accessible objects within the parent will be visible to the intellisense.

For eg. if the parent page has the following code..


namespace testwebapp
{
public partial class parentpage : System.Web.UI.Page
{
public string setLabelvalue
{
set
{
Label1.Text = value;
}
get
{
return Label1.Text;
}
}
}
}


In the above code, I am seeting the value of the label to string 'Pradeep'. In order to call this public property from the user control contained in this page, use the following code.



namespace testwebapp
{
public partial class ChildControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
((parentpage)Page).setLabelvalue = "pradeep";
}
}
}


In the above code the following statment type casts the Page object to the 'parentpage' type.

((parentpage)Page).setLabelvalue = "pradeep";