Search This Blog

Thursday, January 13, 2011

How to create Dynamic Where Clause in Linq

C# Method:

 public IQueryable GetProductList(string whereClause)
    {
        var query = context.ItemMaster_Stage
                              .Where(whereClause);
  
        return (IQueryable)query;
    }

Method calling:

IQueryable obj = new Products().GetProductList("it.ITEM_NAME == 'WEDDING BANDS'");

Here you can call this method with 'Where' clause as per your requirement.

Wednesday, January 12, 2011

Linq To Sql return from function as IQueryable

Following is the example illustrating how we can return IQueryable<T> from a method which has linq to SQL statements and bind returned data to a asp.net repeater control:

public IQueryable GetParentCategory()
    {
        return (context.CategoryMaster
           .Where(c => c.PARENT_CATEGORY_ID == 0)
           .Select(c => new { c.PARENT_CATEGORY_ID, c.CATEGORY_NAME, c.CATEGORY_ID })).AsQueryable();
    }

In above example we need only three fields (Projection) from data source.

We can bind repeater with following code:

rptParentCategory.DataSource = new Products().GetParentCategory();
rptParentCategory.DataBind();

Here 'Product' is a class in 'App_code' containing method 'GetParentCategory'.

Wednesday, December 16, 2009

JS Function to 'SELECT ALL' / 'CLEAR ALL' for CheckBoxList Control

JS FUNCTION:


function CheckBoxListSelect(cbControl, state)
    {  
       var chkBoxList = document.getElementById(cbControl);
        var chkBoxCount= chkBoxList.getElementsByTagName("input");
        for(var i=0;i<chkBoxCount.length;i++)
        {
            chkBoxCount[i].checked = state;
        }
        return false;
    }



Friday, December 11, 2009

ASP.Net Repeater: checkbox validation using javascript

Javascript function for validating checkbox:

The repeater control has two chechbox.
Following function will warn user if he selects second checkbox without selecting first checkbox.


function ValidateCB(cb2)
{

//Get the object for Second Check box (Control ID is passed in method : this.id)
var cbSecondID = document.getElementById(cb2);

//Contruct Control ID for First CheckBox, by replacing string (control id of second checkbox)
var cbFirstID = cbSecondID.id.replace("cb2","cb1");

//Get the object for first checkbox
var cbFirst = document.getElementById(cbFirstID);

//displays alert when first cb is not selected and we are attampting to select second one
if (cbSecondID.checked == true && cbFirst.checked == false)
{
alert("Please select first checkbox");
}

}