Menu ▼

Home / blogs /

<< < 1 2 3 4 5 6 7 8 9 10 > >>

By default asp.net updatepanel will not maintain the div/panel scroll position after partial post back. To main the scroll position we need to add our own javascript.

Note: You need to add the below javascript code after the scriptmanager tag.

<script type="text/javascript">
    var xPos, yPos;
    var prm = Sys.WebForms.PageRequestManager.getInstance();

    function BeginRequestHandler(sender, args) {
        if ($get('divid') != null) {
            xPos = $get('divid').scrollLeft;
            yPos = $get('divid').scrollTop;
        }
    }

    function EndRequestHandler(sender, args) {
        if ($get('divid') != null) {
            $get('divid').scrollLeft = xPos;
            $get('divid').scrollTop = yPos;
        }
    }

    prm.add_beginRequest(BeginRequestHandler);
    prm.add_endRequest(EndRequestHandler);
</script>
Read More...
Submited By : mrkraju On 08 Feb 2017
By default jquery ui draggable will not work with buttons. To make it work with button you need to set the cancel property value to false
<script language="javascript">
    $(document).ready(function () {
        $("#btndrag").draggable({
            cancel: false
        });
    });
</script>
Example:

Source code:
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

    <script language="javascript">
        $(document).ready(function () {
            $("#btndrag").draggable({
                cancel: false
            });
        });
    </script>
</head>
<body>
    <input type="button" id="btndrag" value="Drag !" />

</body>
</html>
Read More...
Submited By : mrkraju On 15 Jan 2017

Below query will return the total number of tables in sql server

SELECT count(name) as tablecount FROM sys.sysobjects WHERE xtype = 'U'


Below query will return the total number of views in sql server

SELECT count(name) as viewscount FROM sys.sysobjects WHERE xtype = 'V'


Below query will return the total number of stored procedures in sql server

SELECT count(name) as spcount FROM sys.sysobjects WHERE xtype = 'P'


Below query will return the total number of functions in sql server

SELECT count(name) as fncount FROM sys.sysobjects WHERE xtype = 'FN'



If you want to get the names of all tables, views, stored procedures or functions then use the below queries

SELECT name FROM sys.sysobjects WHERE xtype = 'U'

SELECT name FROM sys.sysobjects WHERE xtype = 'V'

SELECT name FROM sys.sysobjects WHERE xtype = 'P'

SELECT name FROM sys.sysobjects WHERE xtype = 'FN'

Read More...
Submited By : mrkraju On 07 Jan 2017

Below JavaScript function getdatetime() will return the date string in mm/dd/yyyy hhmmss ampm format. You need to pass date as parameter that need to be returned in the format mm/dd/yyyy hhmmss ampm. If you want current date in the format then you can simple call the function as getdatetime(new Date());

Example

JavaScript Function

function getdatetime(dt) {
    var res = "";
    res += formatdigits(dt.getMonth() + 1);
    res += "/";
    res += formatdigits(dt.getDate());
    res += "/";
    res += formatdigits(dt.getFullYear());
    res += " ";
    res += formatdigits(dt.getHours() > 12 ? dt.getHours() - 12 : dt.getHours());
    res += ":";
    res += formatdigits(dt.getMinutes());
    res += ":";
    res += formatdigits(dt.getSeconds());
    res += " " + dt.getHours() > 11 ? " PM" : " AM";
    return res;
}
function formatdigits(val) {
    val = val.toString();
    return val.length == 1 ? "0" + val : val;
}

Calling function:

var dtformated = getdatetime(new Date());
$("#divdatetime").html(dtformated);

Source code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script>
        function getdatetime(dt) {
            var res = "";
            res += formatdigits(dt.getMonth() + 1);
            res += "/";
            res += formatdigits(dt.getDate());
            res += "/";
            res += formatdigits(dt.getFullYear());
            res += " ";
            res += formatdigits(dt.getHours() > 12 ? dt.getHours() - 12 : dt.getHours());
            res += ":";
            res += formatdigits(dt.getMinutes());
            res += ":";
            res += formatdigits(dt.getSeconds());
            res += " " + dt.getHours() > 11 ? " PM" : " AM";
            return res;
        }
        function formatdigits(val) {
            val = val.toString();
            return val.length == 1 ? "0" + val : val;
        }
        $(document).ready(function () {
            var dtformated = getdatetime(new Date());
            $("#divdatetime").html(dtformated);
        });
    </script>
</head>
<body>
    <div id="divdatetime">
    </div>
</body>
</html>

Read More...
Submited By : mrkraju On 25 Nov 2016

Below JavaScript function getdate() will return the date string in mm/dd/yyyy format. You need to pass date as parameter that need to be returned in the format mm/dd/yyyy. If you want current date in the format then you can simple call the function as getdate(new Date());

Example:

Javascript Function:

function getdate(dt) {
    var res = "";
    if ((dt.getMonth() + 1).toString().length < 2)
        res += "0" + (dt.getMonth() + 1).toString();
    else
        res += (dt.getMonth() + 1).toString();
    res += "/";
    if (dt.getDate().toString().length < 2)
        res += "0" + dt.getDate().toString();
    else
        res += dt.getDate().toString();
    res += "/" + dt.getFullYear().toString();
    return res;
}

Calling function:

var dtformated = getdate(new Date());
$("#divdatetime").html(dtformated);

Source code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script>
        function getdate(dt) {
            var res = "";
            if ((dt.getMonth() + 1).toString().length < 2)
                res += "0" + (dt.getMonth() + 1).toString();
            else
                res += (dt.getMonth() + 1).toString();
            res += "/";
            if (dt.getDate().toString().length < 2)
                res += "0" + dt.getDate().toString();
            else
                res += dt.getDate().toString();
            res += "/" + dt.getFullYear().toString();
            return res;
        }
        $(document).ready(function () {
            var dtformated = getdate(new Date());
            $("#divdatetime").html(dtformated);
        });
    </script>
</head>
<body>
    <div id="divdatetime">
    </div>
</body>
</html>

Read More...
Submited By : mrkraju On 17 Nov 2016

Using jQuery animate you can animate the controls. In this example we will see how to animate the control width using jQuery animate and toggle.

Example:
Click on menu icon to animate


jQuery code to slide/animate menu bar

    <script language="javascript">
        $(document).ready(function () {
            $(".menuicon").on("click", function () {
                $(".menu").animate({ width: 'toggle' }, "slow");
            });
        });
    </script>

Source code:

<html>
<head>
    <style>
        .menu {
            float: left;
            display: none;
            padding: 0px 5px;
        }

            .menu ul {
                list-style-type: none;
                margin: 0;
                padding: 0;
            }

                .menu ul li {
                    margin: 0;
                    padding: 0;
                }

                    .menu ul li a {
                        text-decoration: none;
                        color: #292323;
                        background: #ccc;
                        display: block;
                        padding: 5px 30px 5px 5px;
                        border-bottom: solid 1px #fff;
                        height: 20px;
                        white-space: nowrap;
                    }

        .menuicon {
            border: solid 1px #000;
            border-radius: 50%;
            padding: 5px 8px;
            float: left;
            cursor: pointer;
        }

        .menuiconbar {
            width: 25px;
            height: 5px;
            background-color: black;
            margin: 3px 0;
        }
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script language="javascript">
        $(document).ready(function () {
            $(".menuicon").on("click", function () {
                $(".menu").animate({ width: 'toggle' }, "slow");
            });
        });
    </script>
</head>
<body>
    <div>
        <div class="menu">
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">Blogs</a></li>
                <li><a href="#">Tutorials</a></li>
                <li><a href="#">Forums</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </div>
        <div class="menuicon">
            <div class="menuiconbar"></div>
            <div class="menuiconbar"></div>
            <div class="menuiconbar"></div>
        </div>
    </div>
    <br clear="all" />
</body>
</html>

Read More...
Submited By : mrkraju On 06 Oct 2016

This example shows you how to blink div background using jQuery toggleClass and javascript setInterval.

jQuery toggleClass() example!



Source code:
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <style type="text/css">
        .divmsg {
            background: green;
            color: #fff;
            display: inline-block;
            margin: 20px;
            padding: 20px;
            border-radius:5px;
        }

        .divred {
            background: red !important;
        }

        #btntoggle, #btnstoptoggle {
            margin: 10px 20px;
            padding:10px;
        }
    </style>
</head>
<body>
    <div class="divmsg">
        jQuery toggleClass() example!
    </div><br />
    <input type="button" id="btntoggle" value="Blink background using toogle" /><br />
    <input type="button" id="btnstoptoggle" value="Stop blinking" />
    <script src="http://code.jquery.com/jquery-1.7.2.min.js" language="javascript"></script>
    <script language="javascript">
        $(document).ready(function () {
            var blink = null;
            $("#btntoggle").on("click", function () {
                if (blink == null)
                    blink = setInterval(blinkMessage, 500);
            });
            $("#btnstoptoggle").on("click", function () {
                if (blink != null) {
                    clearInterval(blink);
                    blink = null;
                    $(".divmsg").removeClass("divred");
                }
            });
        });

        function blinkMessage() {
            $(".divmsg").toggleClass("divred");
        }
    </script>
</body>
</html>

Read More...
Submited By : mrkraju On 25 Sep 2016

To take backup of dll in GAC follow the below setps.

  • Open Windows Run
  • Type C:\Windows\assembly\GAC_MSIL and click enter
  • All the GAC MSIL dll's will be opened
  • You can take backup of what ever dll you want

Read More...
Submited By : mrkraju On 14 Jul 2016

This example shows you how to bind data to DropDownList in GridView based on the Grid Row Value. In this example we will bind the States to DropDownList based on Country in Row.

In the GridView RowDataBound we will find the DropDownList and Country then fetch the state list for the Country and bind the state list to DropDownList.

RowDataBound event

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int CountryID = int.Parse(((HiddenField)e.Row.FindControl("hidCountryID")).Value);
            DropDownList ddl = (DropDownList)e.Row.FindControl("ddlState");
            ddl.DataSource = GetStates(CountryID);
            ddl.DataTextField = "State";
            ddl.DataValueField = "StateID";
            ddl.DataBind();
        }
    }

Example
Dropdown binding in GridView

CS Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Text;
using System.Collections;

namespace WebApp
{
    public partial class Sample : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillGrid();
            }
        }

        void FillGrid()
        {
            gv.DataSource = GetCountries();
            gv.DataBind();
        }

        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int CountryID = int.Parse(((HiddenField)e.Row.FindControl("hidCountryID")).Value);
                DropDownList ddl = (DropDownList)e.Row.FindControl("ddlState");
                ddl.DataSource = GetStates(CountryID);
                ddl.DataTextField = "State";
                ddl.DataValueField = "StateID";
                ddl.DataBind();
            }
        }

        DataTable GetCountries()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("CountryID", typeof(int));
            dt.Columns.Add("Country", typeof(string));
            dt.Rows.Add(1, "Australia");
            dt.Rows.Add(2, "New Zealand");
            dt.Rows.Add(3, "India");
            dt.Rows.Add(4, "USA");
            return dt;
        }

        DataTable dtStates = null;

        DataTable GetStates(int CountryID)
        {
            if (dtStates == null)
            {
                dtStates = new DataTable();
                dtStates.Columns.Add("CountryID", typeof(int));
                dtStates.Columns.Add("StateID", typeof(string));
                dtStates.Columns.Add("State", typeof(string));
                dtStates.Rows.Add(1, 1, "Albury");
                dtStates.Rows.Add(1, 2, "Dubbo");
                dtStates.Rows.Add(1, 3, "Sydney");
                dtStates.Rows.Add(2, 1, "Auckland");
                dtStates.Rows.Add(2, 2, "Hamilton");
                dtStates.Rows.Add(2, 3, "Wellington");
                dtStates.Rows.Add(3, 1, "Delhi");
                dtStates.Rows.Add(3, 2, "Hyderabad");
                dtStates.Rows.Add(3, 3, "Mumbai");
                dtStates.Rows.Add(4, 1, "Chicago");
                dtStates.Rows.Add(4, 2, "New York");
                dtStates.Rows.Add(4, 3, "Washington DC");
            }
            dtStates.DefaultView.RowFilter ="CountryID=" + CountryID;
            return dtStates.DefaultView.ToTable();
        }


    }

}

ASPX Page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Sample.aspx.cs" Inherits="WebApp.Sample" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" 
            CellPadding="5" CellSpacing="5" ForeColor="#333333" GridLines="Both" 
            onrowdatabound="gv_RowDataBound"  >
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:TemplateField HeaderText="Country">
                    <ItemTemplate>
                        <asp:HiddenField ID="hidCountryID" runat="server" Value='<%#Eval("CountryID") %>' />
                        <asp:Label ID="lblCountry" runat="server" Text='<%#Eval("Country") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="State">
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlState" runat="server" style="width:100%">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Read More...
Submited By : mrkraju On 20 Jun 2016

This example shows you how to write C# extension method and how to convert DataSet to JSON using JavaScriptSerializer.

By default DataSet doesnot have GetJSON method to get JOSN string from DataSet. So in this example I have written an extension method to add GetJSON method to DataSet and it will return JSON String as output.

    public static class DataSetExt
    {
        public static string GetJSON(this DataSet ds)
        {

            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            ArrayList root = new ArrayList();
            List<Dictionary<string, object>> table;
            Dictionary<string, object> data;

            foreach (DataTable dt in ds.Tables)
            {
                table = new List<Dictionary<string, object>>();
                foreach (DataRow dr in dt.Rows)
                {
                    data = new Dictionary<string, object>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        data.Add(col.ColumnName, dr[col]);
                    }
                    table.Add(data);
                }
                root.Add(table);
            }
            
            return serializer.Serialize(root);
        }
    }

Steps to write GetJSON extension method.

  • Class should be declared as static
  • Method name should be declared as static
  • Method parameter should be starts with "this" keyword and the Object(DataSet) and variable name(ds).
  • Using System.Web.Script.Serialization.JavaScriptSerializer we converted the DataSet to JSON using Dictionary and ArrayList.

Example
C# extension method

CS Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Text;
using System.Collections;

namespace WebApp
{
    public partial class Sample : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnGetJSON_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add("Employee Name", typeof(string));
            dt.Columns.Add("Salary", typeof(decimal));
            dt.Columns.Add("Department", typeof(string));
            dt.Rows.Add("John", 5000.00, "Marketing");
            dt.Rows.Add("Smith", 4000.00, "IT");
            dt.Rows.Add("Williams", 6000.00, "Sales");
            dt.Rows.Add("Vijay", 6500.00, "IT");
            ds.Tables.Add(dt);
            dt = new DataTable();
            dt.Columns.Add("StudentID", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Marks", typeof(int));
            dt.Columns.Add("Grade", typeof(string));
            dt.Rows.Add(1, "Suresh", 950, "A+");
            dt.Rows.Add(1, "Rama", 900, "A+");
            dt.Rows.Add(1, "Kishore", 750, "B");
            ds.Tables.Add(dt);
            divOutput.InnerHtml = ds.GetJSON();


        }

    }


    public static class DataSetExt
    {
        public static string GetJSON(this DataSet ds)
        {

            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            ArrayList root = new ArrayList();
            List<Dictionary<string, object>> table;
            Dictionary<string, object> data;

            foreach (DataTable dt in ds.Tables)
            {
                table = new List<Dictionary<string, object>>();
                foreach (DataRow dr in dt.Rows)
                {
                    data = new Dictionary<string, object>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        data.Add(col.ColumnName, dr[col]);
                    }
                    table.Add(data);
                }
                root.Add(table);
            }
            
            return serializer.Serialize(root);
        }
    }
}

ASPX Page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Sample.aspx.cs" Inherits="WebApp.Sample" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Button ID="btnGetJSON" runat="server" Text="Get JSON" OnClick="btnGetJSON_Click" />
    <br /><br />
    <div id="divOutput" runat="server">
        
    </div>
    </form>
</body>
</html>

Read More...
Submited By : mrkraju On 17 Jun 2016
<< < 1 2 3 4 5 6 7 8 9 10 > >>




Search dotnetlearners.com