Wednesday, February 23, 2011

Disable back using javascript

After login we come to j\home page & after click on back button it should not move to login page
so we use following script to restrict it
when we traverse through webpages history is maintained. Dont forget to call setTimeout method
It works on Firefox , IE & Chrome  also
 <script type = "text/javascript" >
function disableback()
setTimeout("disableback()", 0);

Call above method on Body unload method on login page so when u move from login to home page after click on home's back button , we will be on same page that is on home page
<body onunload="disableback()">
 now your back button problem will be solved......

Tuesday, February 22, 2011

Focus particular textbox in page bydefault (like google) & on enter key press hit button from page(like google search button)

This are very easy things to do,
when we open any page & we want particular textbox contains cursor & after pressing enter button particular button gets clicked then do the following things
in page contains a form tag , It contains defaultbutton & defaultfocus attributes set it as per your requirement
defaultbutton:= give your buttons name which u want to clicked
defaultfocus:= give control name for which you want focus after page load
<form id="form1" runat="server" defaultbutton="Button1" defaultfocus="TextBox1">
 Here  TextBox1 contains cursor & after press enter Button1's click event gets called

Encrypt & Decrypt Connection string in web.config

first import System.web.configuration namespace then add folowing code for encryption
for example on some button i have written following code for encryption
        string path=@"\Projectname";
        Configuration config = WebConfigurationManager.OpenWebConfiguration(path);
        ConfigurationSection appsetting = config.GetSection("connectionStrings");

ProtectSection Method in  SectionInformation encrypts the connection string

Now lets see for how to decrypt, SectionInformation contains method UnprotectSection() for decryption
code as below
        string path = @"\Projectname";
        Configuration config = WebConfigurationManager.OpenWebConfiguration(path);
        ConfigurationSection appsetting = config.GetSection("connectionStrings");

This Snippet will help to Encrpt & Decrypt  connection String

Wednesday, February 9, 2011

Freeze the columns in Gridview

STEP 1: Add the Following CSS class to the .aspx file
<style type="CSS/Text">
text-align: left;
cursor: default;

STEP 2: Set the CSSClass property of the cells in the GridView RowDatabound event.

protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
e.Row.Cells[1].CssClass = "freezecolumn";

Tuesday, February 1, 2011

add & into querystring

If you try to add "&" directly into string , it doesn't work
Ex: WebPage.aspx?Var1=DVal1&Var2=DVal2
if u want to create above querystring & u try to write statement as below
string url="WebPage.aspx?Var1=" + DVal1 + "&Var2=" + DVal2;
DVal1 & DVal2 are strings. It won't work ,If DVal1 & DVal2 contains space in contents (DVal1="my value") It will show you
to make proper querystring you have to use a Server.UrlEncode
It replaces space with %20 and & with %26
string url="WebPage.aspx?Var1=" + Server.UrlEncode(DVal1) + "&Var2=" + Server.UrlEncode(DVal2);

Saturday, January 29, 2011

Maintain Scroll position in div

This javascript is useful for maintaining scroll position in Panel or div tag...

Following code in Script Tag....
window.onload = function(){
var strCook = document.cookie;
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

< div id="divTest" width:150px;height:200px;overflow:auto" onscroll="SetDivPosition()">
--Gridview (or more data)

Tuesday, January 11, 2011

Check boxes in List box

1.Drop CheckBoxList control on your page.
2.Surround it with
3.Set Overflow style of
to "auto" and size as you like.

< div style="BORDER: thin solid; OVERFLOW: auto; WIDTH: 100px; HEIGHT: 100px">
< asp:checkboxlist id="CheckBoxList1" runat="server">