Wednesday, September 1, 2010

Clientside Validation for Controls inside GridView

Server Side validations for the Controls inside the Gridview will be bit heavy for the pages havingmore than two gridviews. Javascripts can be used to iterate through the controls inside the particular column of grid. We will have to create a Javascript Two dimensional array to hold both the Server ID and Client ID of the control from the GridView PreRender Event. Same can be achieved from GridView RowDataBound Event also.

The following PreRender Event registers a Two Dimensional Array with control's Server ID and Client ID.


protected void GrvPostPromotion_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ClientScriptManager oCsm = this.Page.ClientScript;
            TextBox oGrossSales = e.Row.FindControl("TxtGrossSalesPOP") as TextBox;
            TextBox oNetToGross = e.Row.FindControl("TxtNetToGrossPOP") as TextBox;
            TextBox oSGM = e.Row.FindControl("TxtPSGMPOP") as TextBox;

            oCsm.RegisterArrayDeclaration("TxtGrossSalesPOP", "'" + oGrossSales.ClientID + "'");
            oCsm.RegisterArrayDeclaration("TxtNetToGrossPOP", "'" + oNetToGross.ClientID + "'");
            oCsm.RegisterArrayDeclaration("TxtPSGMPOP", "'" + oSGM.ClientID + "'");                
        }
    }


Your Javascript Validation Function can do the Job like this

function ValidateDraftPostPromoGrid() {
    var rerunThis = false;
    var vFlag = 0;
    var oDisp = document.getElementById('divMessage'); // DIV, where you display the error message
    for (i = 0; i < TxtGrossSalesPOP.length; i++) {
        var oGrossSales = document.getElementById(TxtGrossSalesPOP[i])
        if (oGrossSales.value == "") {
            oGrossSales.className = "txtboxRedBorder";
            oDisp.innerHTML = "Post Promotion Details is Mandatory ";
            vFlag = 1;
            rerunThis = false;
            break;
        }
        else {
            if (isNaN(oGrossSales.value) == false) {
                oGrossSales.className = "txtboxNormal";
                oDisp.innerHTML = "";
                rerunThis = true;
            }
            else {
                oGrossSales.className = "txtboxRedBorder";
                oDisp.innerHTML = "Gross Sales to be entered in numbers ";
                vFlag = 1;
                rerunThis = false;
                break;
            }
        }
    }
return rerunThis;
}

Tuesday, August 31, 2010

Grouping/Appending GridView Header

GridView header to have a Parent Header(Grouped Header) is as simple as the following.







protected void
GrvPrePromotion_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridView HeaderGrid = (GridView)sender;
            GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
            TableCell HeaderCell = new TableCell();
            HeaderCell.Text = "Promotion Product Group";
            HeaderCell.ColumnSpan = 3;
            HeaderGridRow.Cells.Add(HeaderCell);
            HeaderGridRow.CssClass = "gridheadtop";

            HeaderCell = new TableCell();
            HeaderCell.Text = "Sales and SGM";
            HeaderCell.ColumnSpan = 4;
            HeaderGridRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = "Special Discount";
            HeaderCell.ColumnSpan = 3;
            HeaderGridRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = "Rebate";
            HeaderCell.ColumnSpan = 1;
            HeaderGridRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = "Free Goods / Prize";
            HeaderCell.ColumnSpan = 1;
            HeaderGridRow.Cells.Add(HeaderCell);

            GrvPrePromotion.Controls[0].Controls.AddAt(0, HeaderGridRow);
        }
    }

 How to Display/Calculate Total/Percentage in the Gridview footer.

Rounding a number to decimal places, Javascript.

As Javascript's Math.Round() cant be directly used to round to decimal places, we should find the workarounds.
Have a look at the following function which does the job.

function RoundNumber(ThisNumber, rlength) { // Arguments: number to round, number of decimal places
  var RoundedNumber = Math.round(ThisNumber*Math.pow(10,rlength))/Math.pow(10,rlength);
  return RoundedNumber ; // returns the resulting number. 
}

Friday, August 27, 2010

WebConfig settings for Sharepoint (WSS and MOSS Portals)

Get Microsoft ASP.NET 2.0 AJAX Extensions 1.0 running on the WFE (WHere the Instance of your WSS/MOSS is running. Open up the Web.config of your WebSite from C:/INETPUB/VIRTUAL DIRECTORIES/WSS/(PORT NUMBER OF YOUR SITE) and do some settings which are required for AJAX to run on your Portal. Web.Config wont have Ajax settings in it even if AJAX is installed on the Server.

FOR WSS

FOR MOSS

ASP.Net Application to run on IIS 7.0

Upgrading an ASP.NET application from IIS 6.0 or lower version to IIS 7.0 will have a lot of work. Microsoft provides an application that does the settings for your application to run on IIS 7.0. [Classic ASP.NET Integration Mode to IIS 7.0 Integrated Mode]

IIS 7.0 takes care of migrating the application by using the APPCMD.EXE command line tool to perform the migration. The migration error message contains the command that is executed in command line window (which you must run--right click the Programs\Accessories\Command Prompt icon, and choose "Run as administrator") in order to instantly migrate your application to Integrated mode.

The format of the migration command is :

%windir%\system32\inetsrv\APPCMD.EXE migrate config

where Application Path is the virtual path of the application containing the site name, such as "Default Web Site/app1"

Monday, August 2, 2010

Use Delegate to Delete an Item from a List

Iterating through the list items is quite a simple task when delegates are in place.

 
protected void GrvUploads_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString() == "DeleNow")
        {
            List oUploader = Session["UploadedFiles"] as List;
            if (oUploader != null)
            {
                oUploader.Remove(oUploader.Find(delegate(UploadEntity oEntity)
                {
                    return (oEntity.UploadID == Convert.ToInt64(e.CommandArgument));
                }
                ));
            }
            Session["UploadedFiles"] = oUploader;
            BindUploadGrid();
        }
    }

Tuesday, July 20, 2010

SET NOCOUNT ON, the performance booster

We normally use SET NOCOUNT ON in STORED PROCEDURES and TRIGGERS
What does actually SET NOCOUNT ON do?

When we normally execute a T-SQL statements (INSERT, UPDATE, SELECT, DELETE) in a query window, we see messages that says something like "(20 row(s) affected)" as the response to our query?

Database's internal architecture processes and displays the status of the execution process. The message display is done by the DONE_IN_PROC which is predefined in the architecture. Though this is default to SQL Server, this makes a slight difference in performance. Obviously, when a query has lots of T-SQL statements in it to perform, this costs you a lot.

The DONE_IN_PROC message is pretty useful when we are using the query window to execute the procedure. But it runs unwanted when the web applications are concerned. Each operation of T-SQL statement will return this internal message (eg, an INSERT, UPDATE, or SET in a WHILE loop) for each step within the Stored Procedure execution cycle.

So from the web developer perspective, its quite unwanted when he update the files in to production server. Lets have a boost in performance this way.