Wednesday 11 January 2012

Call Server Side Function From Javascript


In design :
In the head section:

 <script type="text/javascript" language="javascript">
    function callme(chkCall)
    {
       var chkCall=document.getElementById(chkCall);
       if(chkCall.checked==true)
        {
             chkCall.checked=true;
             PageMethods.GetContactName();
        }
        else
        {
              chkCall.checked=false;
        }
    }
    </script>

In the body section:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
<asp:CheckBox ID="chkCall" runat="server" />

In the CS part :

protected void Page_Load(object sender, EventArgs e)
    {
        chkCall.Attributes.Add("onclick", "javascript:callme('" + chkCall.ClientID + "')");
    }

    [System.Web.Services.WebMethod]
    public static void GetContactName()
    {
        Response.Write("your name");
    }

Monday 2 January 2012

Resize the TextBox dynamically according to text


<script type="text/javascript">
function setHeight(txtNote) {
txtNote.style.height = txtNote.scrollHeight + "px";
}
</script>

In the body :
<asp:TextBox ID="txtNote" runat= "server" TextMode="MultiLine"  onkeyup="setHeight(this);" onkeydown="setHeight(this);" />

Add to Bookmark using JS


<script language="javascript" type="text/javascript">
function addBookmark() {
bookmarkurl = document.URL;
bookmarktitle = document.title;
if (document.all)  //Check the condition for IE
window.external.AddFavorite(bookmarkurl, bookmarktitle)
else if (window.sidebar) // Check the condition for Mozilla
{
window.sidebar.addPanel(bookmarktitle, bookmarkurl, "");
}
}
</script>

In the body part :
<a href="javascript:addBookmark();">Bookmark this page!</a>

Show Tooltip in DropDown list items

In design Page :

<asp:DropDownList ID="DropDownList1" runat="server" ondatabound="DropDownList1_DataBound">
</asp:DropDownList>

In CS page :

protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dTable = new DataTable("Dynamically_Generated");
        DataColumn name = new DataColumn("Name", typeof(string));
        dTable.Columns.Add(name);
        dTable.Rows.Add("sanjeet");
        dTable.Rows.Add("ajay");
        dTable.Rows.Add("sonu");
        dTable.Rows.Add("dipu");
        DropDownList1.DataSource = dTable;
        DropDownList1.DataTextField = "Name";
        DropDownList1.DataValueField = "Name";
        DropDownList1.DataBind();
    }
    protected void  DropDownList1_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        if (ddl != null)
        {
            foreach (ListItem li in ddl.Items)
            {
                li.Attributes["title"] = li.Text;
            }
        }
    }