Read, With the Name of Your Lord Who Created

Archive for the ‘Programming’ Category

ASP.NET Validation Controls: How to Disable It

Posted by triaslama on December 2, 2008

Validating an input is a common need, sometimes we want a field must be filled, a field must be in a specific range, or an input must follow a special pattern. ASP.NET has a bunch of validation controls that make our job a little bit easy. Despite all of the ease of use offered by ASP.NET validation controls, sometimes -just maybe- we need to disable it.

One important thing we must if we use ASP.NET validation controls is: what actions will trigger validators? One of the answer is button click! (The other maybe Postback action). With that in mind, we can take a conclusion that every click event of asp.net button (<asp:Button />) by default will always trigger validation controls to take the appropriate action.

Let’s say I want to disable RequiredFieldValidator against a TextBox control for one action, and enable it for another action, how can I accomplish this? Consider I have textbox control and two button controls (previous button and next button accordingly). I want if previous button clicked it goes to previous page and doesn’t fire the required field validator. Well, the solution is really simple.

Read the rest of this entry »

Advertisements

Posted in ASP.NET | Tagged: , , , , , | 12 Comments »

Error “‘Sys’ is undefined” in Asp.Net Ajax

Posted by triaslama on November 29, 2008

When I try ASP.NET Ajax in my development server I get this Javascript error: “‘Sys’ is undefined“. This error occur because I place <asp:ScriptManager runat=”server” /> element in my page.

Wondering why, I search in internet and hope that I will find one of the solution. After several trial and error finally I can find the solution and it work just fine for me.

To solve the error we need to modify web.config file, add the following under <system.web />

<httpHandlers>
<add  verb=”GET,HEAD” path=”ScriptResource.axd” type=”System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ />
</httpHandlers>

Nothing more I can say, but if you get the same error when you work with ASP.NET Ajax try the above solution, and I hope this help.

Posted in ASP.NET, Javascript | Tagged: , , , , , , , | 14 Comments »

How To: Include File in ASP.NET Pages

Posted by triaslama on November 19, 2008

Sometimes programming its just the matter of style, I am not generalize but often I found that we can achieve one goal through many different ways. Lets inspect one of those! By right now I have try three different way on how we put a file in ASP.NET pages. I don’t know which one is the best, but all of the three has the same meaning: we can refer to a file and bring it where ever we like in our ASP.NET pages.

Yes, this is maybe the prefered way because if we include a file and we place it in all of pages -let’s say copyrights footer for example- Later if we want to change the copyrights with the new one, or modify it to something we want, we simply modify it in one place (in our referenced file) rather than go through all of our pages and change it one by one.

How we do this? I have tried three solutions to do that.

1. Using <!– #include file=”[filename].aspx” –>
The first (and maybe the old one) we just write a simple script (as show above) and specify the file we want to include. Place the above script where ever we want content of [filename].aspx appear in pages that referenced it. For security reason always try to include only *.aspx files.
Read the rest of this entry »

Posted in ASP.NET, Programming | Tagged: , , , , , , | 9 Comments »

App.config And Web.config, The Two Configuration Files Story

Posted by triaslama on November 12, 2008

In .NET programming world we govern setting for our program through configuration file (file with .config extension). We use web.config for web application (ASP.NET) and app.config for the rest.

Although both of config files share much similarities but both has difference too. Yes, I realize that many of you maybe already know what is the difference, but I wrote this because just a moment ago I realize one of the difference.

In ASP.NET we can just drop our web.config file in root of our virtual directory and this file will be referenced automatically, as simple as that. When we use app.config file -for example in Windows Forms application- the app.config file need to renamed with the same name as our executable file (or *.dll file for library). In visual studio this thing happen automatically, so when I try it manually (without any of IDE) the problem arise.

I write a very simple Winforms application (lets say I give this Winforms app. as test.cs), I write app.config file too then I place this app.config in the same directory with my simple Winforms program. Inside my Winforms program(test.cs) I retrieve a value from app.config file. Then I compile my source code manually (with csc command, so it will produce test.exe) and I run it. Unfortunately I get NullReferenceException! “Oh no, what was already happen. I think I’m not do something strange” maybe something like that I muttered.

Yes, I am not do something wrong except because I retrieve value from app.config! Suddenly I realize that my app.config cannot referenced by my executable file (test.exe) automatically (like what will happen in web.config case). Because I feel it’s strange (It work when I use Visual Studio but doesn’t work when I manually compile it) I inspect my Visual Studio files and I open bin directory. Finally I found this: the config file (app.config) renamed automatically by Visual Studio with the same name with my *.exe file, but with *.config extension.

Oh, so this is the matter. the *.exe file (or *.dll file) referenced config file automatically as long as the config file has the same name. So I rename my app.config become test.config, run again my test.exe and yes now it works.

Posted in Programming | Tagged: , , , , , | 1 Comment »

Four Ways Javascript Binding Event Listeners

Posted by triaslama on July 22, 2008

There is several ways in how to bind an event in Javascript. As far as I know four ways exist on behalf of binding a Javascript event. Here brief description of each way

1. Through inline HTML Code
The Javascript event declared inline in HTML code, for example:

<input type=”button” value=”Alert Message” onclick=”showAlert()” />

Then showAlert() is a Javascript function that merely show an alert message:

function showAlert()
{
      window.alert("Hello, World!!!");
}

This inline event binding works for all browsers.

2. Traditional Binding
I think this way is more elegance than the first one, but before we can do this we must get the desired element. We can use two useful methods of document object to get the element we want, there is getElementById() and getElementsByTagName(). Both method receive one string parameter which show the ID of an element (for getElementById()) or elements name (for getElementsByTagName()). Please aware that the result of getElementsByTagName() is always array of element.
Suppose that we have the following page:

Read the rest of this entry »

Posted in Javascript, Programming | Tagged: , , , , , | 18 Comments »

Javascript Event Phases: Capturing and Bubbling

Posted by triaslama on July 3, 2008

Events is something useful in programming world, because with events program can react based on user action. Javascript is no exception and I think one of Javascript strength is Events. There is many interesting things in Javascript events one of them are the Javascript event phases: Capturing and Bubbling.

Two Phases of Javascript Event: Capturing and Bubbling
Capturing phase is an event moving down the DOM tree to the element that instantiated event. Bubbling phase occuring after Capturing phase. Bubbling phase begins from the source of the event (e.g. button click) traverses up DOM tree to the root element.

Let’s see the following document structure:

<body>
<form>
<input type="button" />
</form>
</body>

When we click the button, the event captured by the document first, then <body> element, <form> element, and finally the <input type=”button” /> element (capturing phase). After that the event moves back, started from button element (button click event handler fired), <form>’s click, <body>’s click, and document click event handler fired (bubbling phase).

For the complete example try the following (evtphases.htm):
Read the rest of this entry »

Posted in Javascript, Programming | Tagged: , , , , , | 2 Comments »

The Differences of ASP.NET Forms and Windows Forms in a Nutshell

Posted by triaslama on June 24, 2008

I think its quite interesting if we compare two things that has equalities and differences. Now we will talk about Forms (WebForms and WinForms).

As we already know WebForms uses browser as presentation (ASP.NET Forms is no exception) and Windows Forms has its own presentation. So we may agree that WebForms has the strength through its availability and WinForms through its rich user interface.

In this post I will talk about the differences, what I get from both forms (ASP.NET Forms and Windows Forms) at a glance. I hope this post not tedious for those who already familiar with ASP.NET and Windows Forms.

Base Class
An ASP.NET Form inherits Page class (contained in System.Web.UI namespace). Look at the following example:
Presentation page (Welcome.aspx):

    <%@ Page Language=”C#” debug=”true” CodeFile=”Welcome.aspx.cs” Inherits=”Welcome.HelloWorldClass” %>

    <html>
    <head>
    <title>Welcome To ASP.NET Form</title>
    <script lang=”C#” runat=”server”>
    </script>
    </head>
    <body>
    <form runat=”server”>
    <center><asp:Label id=”label” Font-Size=”25px” style=”font-weight:bold;” runat=”server” /></center>
    </form>
    </body>
    </html>

 

This is the code behind (Welcome.aspx.cs):

Read the rest of this entry »

Posted in .NET, ASP.NET, C#, Programming | Tagged: , , , , | 7 Comments »

Martial Arts and Computer Programming, The Equalities and Differences

Posted by triaslama on June 20, 2008

Computer Programming

Martial Arts

(Both images taken from sosamartialarts.com and degreedirectory.org)

First and foremost, this is only my own opinion, you not necassary agree with me or maybe you have your own opinion. What I write here maybe not reflect the reality of both (martial arts and computer programming). Second I write this with “As Far As I Know” assumption.

In this post I just tempted to unveil what is reside in my mind. By now I want to talk about equalities and differences between martial arts and computer programming.

Lets start with the equalities:

Sometimes it is suck, but it is rock!
Learning martial arts can be so suck, the same thing happen with computer programming! But best of all both of two things is rock! So your sacrifice is worth!

It is good when you enjoy it!
What is better than do thing that we like? So its really good for us especially when we enjoy doing it!

Read the rest of this entry »

Posted in Martial Arts, Programming, Thoughts and Opinions | Tagged: , , , , , , | 4 Comments »

Cursor Positions, Selected Text, and A Simple HTML Editor

Posted by triaslama on June 6, 2008

It would be nice if we have a HTML text editor that doing a simple task of HTML editor for us. It works just like HTML view of WordPress admin page when we write a post. We select the text press specific button and then the selected text will be enclosed with appropriate format. I Test the code in Mozilla Firefox and Internet Explorer, so in both browsers this code should works fine. Here the screenshoots:

Screenshoots page.

We need to prepare the prerequisites.

As prerequisites, at least we need two things. First, we need to know how to define the cursor position inside a <textarea> element, second we need to know how to retrieve a selected text inside a <textarea> element. So, for the first step, I will talk about the both things shortly.

Every browser brings the different behaviour. The browser quirk will make our code a little bit longer because we need to specify different code for different browser. Defining cursor position (current position) is trivial in Mozilla Firefox, but we need a little trick in Internet Explorer. If we want to retrieve selected text in a <textarea> element, both (Forefox and IE) has their own way too.

Defining Cursor Position Inside a <textarea> Element
In Firefox we just need the following code to determine the current position of cursor in a <textarea> element:

var currentPosition = txtArea.selectionStart;

txtArea is a <textarea> element.
Internet Explorer (I use IE 6) doesn’t have selectionStart and selectionEnd property, so we need a little more effort to determine start and end position of the cursor (this is a tricky way).

var range = document.selection.createRange();
var drange = range.duplicate();
drange.moveToElementText(txtArea);
drange.setEndPoint("EndToEnd", range);

var currentPosition = drange.text.length - range.text.length;
var endPosition = drange.text.length - currentPosition;

Read the rest of this entry »

Posted in HTML / CSS, Javascript | Tagged: , , , , , , , | 17 Comments »

C# 3.0: Inferred Type Variables, Extension Methods, and Lambda Expressions

Posted by triaslama on May 27, 2008

C# 3.0 brings many of new features. Some of features developed from the existing one (such as lambda expressions that provides more concise syntax than anonymous methods). Other features can be considered as totally new (such as LINQ). In this post together we will learn about Inferred type variables, extension methods, and lambda expressions.

By reading this article I assume that you are familiar with C#, knowing the previous features of this language is a plus.

Inferred Type Variable
Inferred type variable presented through ‘var’ keyword. If you are a Javascript programmer you must be already familiar with ‘var’ keyword. But, ‘var’ in C# 3.0 has different meaning with ‘var’ keyword in Javascript. In C# 3.0 when we use ‘var’, we tell the compiler that the type of variable should be inferred from the assignment. In Javascript var means that a variable can hold any kind of type.
Consider the following Javascript file:

    var myVariable = 5;
    window.alert(myVariable);
    myVariable = “I change the type of myVariable, now myVariable is a string!”;
    window.alert(myVariable);

 

At the beginning I assign myVariable with 5 (an integer), but later, I assign myVariable with a string (so myVariable can hold any kind of value). But its not the case with ‘var’ in C# 3.0.

using System;

class InferredTypeVar
{
   static void Main()
   {
      var myVariable = 12.5;
      Console.WriteLine("myVariable: "+myVariable);
   }
}

 

Because I assign 12.5 to myVariable then type of myVariable will be double. If we try to fake the compiler and add the following code (hoping that it will the same as in Javascript):

Read the rest of this entry »

Posted in C# | Tagged: , , , | 5 Comments »