Pages

Saturday, January 29, 2011

Maintain Scroll position in div Asp.Net

This javascript is useful for maintaining scroll position in Panel or div tag...
Write following javascript function inside js file and add script reference to this file on page Or write following javascript on the same page inside script tag, it is called as inline javascript.

Following code in Script Tag....
window.onload = function(){
var strCook = document.cookie;
if(strCook.indexOf("!~")!=0){
var intS = strCook.indexOf("!~");
var intE = strCook.indexOf("~!");
var strPos = strCook.substring(intS+2,intE);
document.getElementById("divTest").scrollTop = strPos;
}
}
function SetDivPosition(){
var intY = document.getElementById("divTest").scrollTop;
document.title = intY;
document.cookie = "yPos=!~" + intY + "~!";
}


Following code in body

You need to call SetDivPosition() javascript function on div's onscroll event
< div id="divTest" width:150px;height:200px;overflow:auto" onscroll="SetDivPosition()">
--Gridview (or more data)
</div>
This way you can maintain the scroll position.

Happy Coding!!

Tuesday, January 11, 2011

checkbox in listbox asp.net

Sometimes we require a list of checkbox. How can we achieve this in asp.net
1. Add div on your asp.net page. Set some height and width to div so it will be fixed in height and width and will look appropriately on the web page.
Set overflow:auto so if your list is increasing it will give you scrollbar.
< div style="BORDER: thin solid; OVERFLOW: auto; WIDTH: 100px; HEIGHT: 100px">
< /div>
2. Insert CheckBoxList control inside div.
< div style="BORDER: thin solid; OVERFLOW: auto; WIDTH: 100px; HEIGHT: 100px">
< asp:checkboxlist id="CheckBoxList1" runat="server">
</div> 
This way you can get a checkboxlist effect.

Happy Coding!!

Thursday, September 2, 2010

Suppress C# Compiler Warnings

When you compile any c# program it may produce warning & Errors. If your code contains any errors, it means there is some serious issue. If your code contains warning means it is tolerable.

Compiler has option which treat warning as errors. In that case your build process fail if your code contains warning.

Some sample code for Warning
class MyProgram
{
      int variable1;
}

It generates warning :Code111: The field variable1 is never used .

Code111 is Error number. you can supress this warning using Pragma warning compiler directive.

class MyProgram
{
#pragma warning disable 111 //Here u can give comma separated list of warning numbers
int variable1;
}

If you want to re-enable warning you can use restore keyword
#pragma warning restore 111 //if used it again it shows the warning

Best practice is your code always have 0 errors and 0 warnings.

Happy Coding!!