Thursday, September 09, 2010

ASP.NET Coding Standards

I found a great post, which focuses on ASP.NET Coding Standards. These standards should be considered while working with ASP.NET application.

You can read the article here.

Wednesday, June 23, 2010

Validate Blank Subject line for Outlook 2007

Currently, in the professional world, Outlook is most popularly used as the email client. The subject line is a very important part of any email. A good subject line can attract the attention of the email reader. How many times has it happened to you that you have sent an email through Outlook without a subject line? After clicking the Send button, you realize that you have made a mistake. Are you forgetting to include the subject line in emails? Outlook does not validate for an empty subject line.


Solution

We'll follow these steps to validate an empty subject line for Outlook 2007.

1. Go to Tools -> Macro -> Visual Basic Editor. Or, directly press Alt + F11.


2. The Visual Basic editor window gets opened.
3. On the left pane, you will see Microsoft Outlook Objects or Project1, expand this. Now, you will see the ThisOutLookSession entry.


4. Double click on ThisOutLookSession. It will open up a code pane.
5. Copy and paste the following code in the code pane:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
   Dim strSubject As String
   strSubject = Item.Subject
   If Len(Trim(strSubject)) = 0 Then
       Prompt$ = "Subject is Empty. Are you sure you want to send the Mail?"
      If MsgBox(Prompt$, vbYesNo + vbQuestion + _
            vbMsgBoxSetForeground, _
            "Check for Subject") = vbNo Then
        Cancel = True
      End If
  End If
End Sub


Now, just save the project.

There is one more setting you need to do, and that is to enable the macro. Go to Tools->Macro->Security. A dialog window gets opened. Select Warning for all macro options.


That's it.. You are good to go now.

Close your Outlook and open it again. You will be notified about a security concern. See the image below:


Click on Enable Macros.

Now, try to send an mail with an empty subject line. Enjoy....

Enjoy,
Virendra Dugar

Sunday, June 20, 2010

Access ViewState between Pages

Before I start this article, let me ask you something. Is it possible to access the ViewState variable of one page on another page? I don't know what your answer is. Well, frankly speaking, my answer was also "NO" before writing this article as it is said that ViewState is page specific.

ViewState is a very misunderstood animal. It is said that ViewState is Page specific; that means, it is available only on the same page on which it was created. Once you redirect to another page, the previous page's viewstate is no longer accessible. But that is not true.


Demo

Yes, we can access the viewstate variables across pages. This is only possible if Cross Page Posting or Server.transfer is used to redirect the user to other page. If Response.redirect is used, then ViewState cannot be accessed across pages.

Before you continue reading this article, please read these articles on Cross Page Posting and Server.transfer.

Ok, so all set now... I will demonstrate this using the demo created by me. You can download the demo from the link at the top of this article.

I have created two *.aspx pages named:
  1. ViewStateContainer.aspx: This page sets the ViewState variable and transfers the user to another page using Server.transfer.
  2. AccessViewState.aspx: This page accesses the ViewState variable of ViewStateContainer.aspx page.
This is the code of ViewStateContainer.aspx page:
public partial class ViewStateContainer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ViewState["Page1"] = "Page1 ViewState";
        Server.Transfer("AccessViewState.aspx");
    }

    public StateBag ReturnViewState()
    {
        return ViewState;
    }
}

As you can see, I have set a ViewState variable in Page Load and transfer the user to AccessViewState.aspx page using the Server.transfer() method.

This page also contains a method ReturnViewState() which actually returns the ViewState of this page to the calling function. The return type of the method is StateBag class.

StateBag class: This class is the primary storage mechanism for all HTML and Web server controls. It stores attribute/value pairs as strings associated with the control. It tracks changes to these attributes only after the OnInit method is executed for a page request, and saves the changes to the page's or control's viewstate.

Now let's take look at AccessViewState.aspx Page code:
public partial class AccessViewState : System.Web.UI.Page
{
    private StateBag PreviousPageViewState
    {
        get
        {
            StateBag returnValue = null;
            if (PreviousPage != null)
            {
                Object objPreviousPage = (Object)PreviousPage;
                MethodInfo objMethod = objPreviousPage.GetType().GetMethod
      ("ReturnViewState");
                return (StateBag)objMethod.Invoke(objPreviousPage, null);
            }
            return returnValue;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {
            if (PreviousPageViewState != null)
            {
                Label1.Text = PreviousPageViewState["Page1"].ToString();
            }
        }
    }
}

Whenever we use Server.transfer or Cross Page Posting, We can get the previous page object via PreviousPage property. Using Previous Page, we can find the controls of the previous page. For example, one can access Label control placed in ViewStateContainer Page in current Page.

Looking at the code, I have created a PreviousPageViewState property in this page, which returns the previous page's ViewState. It first checks whether PreviousPage is null or not, if it's not null, then it creates an object of the previous page. Now using Reflection, we can invoke the method of the previous class. Using MethodInfo class, I have invoked the ReturnViewState() method of ViewStateContainer Page.

In Page_Load event, I am able to access the ViewState variable of ViewStateContainer Page. You can access all the viewstate variables set in ViewStateContainer Page.

Enjoy,
Virendra Dugar

Saturday, May 22, 2010

Check/Uncheck All Checkboxes with JQuery

This particular article is about a pretty common functionality which I think most of the software engineers face in their projects. Even I came across the same situation many times. Previously I used to achieve the functionality via JavaScript, which really worked flawlessly. But as a software engineer, I think one should always adapt the new technology. So, this time I tried to achieve the same via JQuery as its booming now and one need to upgrade himself. So what functionality are we talking about? See below given image.




Okay. So you must have got the idea about the functionality. Yes, you guessed it right. It’s about selecting all the items of checkbox list, when select all is checked. You can find thousands of solution using Jquery when you google it. Then why you are here. Well, in fact I googled a lot but didn’t even come across to a single article which solves my purpose. Let me explain the requirement of the functionality here.

Wednesday, January 20, 2010

15 New Features of jQuery 1.4

jQuery 1.4 was recently released. There are many new features, enhancements and performance improvements included in 1.4!

You can download jQuery 1.4 right now, here:http://code.jquery.com/jquery-1.4.js

Check this article to know 15 New Features of JQuery 1.4

Enjoy..

Sunday, January 03, 2010

Set Page Position after asyncPostback

I will start this article, with one of the problem which I faced recently.

BackGround

I was working with a page. The page is divided into two parts.

1. Add/Update/Delete (I call it Area 1)
2. Listing of records (I call it Area 2)

I have used Repeater control with no paging (Though the records are more but my client hates paging. :) ). Above the repeater control, there were some textbox, dropdowns, checkbox, listbox etc. were placed used for add/update operations. There is a link button in the repeater control for every row, which when clicked opens the records in update mode. It fills all the control with data placed on Area 1 . Everything is placed in an UpdatePanel.

Problem

There were more than 10000 records listed in the repeater. When I click the very last record for editing, asynchronous postback happens and data is loaded for editing in Area 1 . But the problem was with the page position. I was not able to see the control's of Part1, as page was maintaining it's scroll position after async postback. This is what I don't want. As it's not at all user friendly. End-User will never come to know that record has been opened in edit mode. I set the "MaintainScrollPositionOnPostback" property to false but it was not working because of AJAX. Somehow, I want page position to be set to (0,0) so that I can see Area 1 controls.

Solution

The solution was to set Page position back to 0,0, at the end of ajax request. Here is what I did. See below code.

  
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, args) {
window.scroll(0,0);
}
</script>
Javascript provides a function scroll() which basically scrolls page position to the specified value. It takes two arguments Top and left. I passed it 0,0 and it worked for me.

Enjoy..

back to top