-->

Saturday, February 6, 2016

Refreshing / reloading of pages and delayed redirect using Meta tags in ASP.Net c#

In this article, I will explain you, how to reload page after few seconds. This types of logics appear where web page refresh just after few seconds like yahoo cricket. By using Meta tag, we can implement in web technologies. I will give you an example of it. This article cover such things like :
Watch full code video

  1. Page Reload/ Refresh after 10 seconds.
  2. By using code we can do reload/ refresh page.
  3. By using AppendHeader( ) method we can solve this query.
  4. Redirect after 5 seconds to new page.
  5. Redirect page after session out.
Refreshing / reloading of pages and delayed redirect using Meta tags in ASP.Net c#

By using meta tag's attribute, we can refresh webpage, in which we have two attributes i.e http-equiv and content. Let's, take an example:

<head runat="server">
    <title>Meta Tags Example</title>
     <meta http-equiv="Refresh" content="10" />
</head>

We can do this thing by using c# code. By using HtmlMeta class's property, we can also refresh page just after 10 seconds.

C#

protected void Page_Load(object sender, EventArgs e)
{
    HtmlMeta meta = new HtmlMeta();
    meta.HttpEquiv = "Refresh";
    meta.Content = "10";
    this.Page.Header.Controls.Add(meta);
}

By using AppendHeader method we can do the same things.

C#

protected void Page_Load(object sender, EventArgs e)
{
    Response.AppendHeader("Refresh", "10");
}


If you want to do redirect page after few seconds then you should use meta tag at compile as well as runtime. In compile time, you can use meta tag with similar attribute which is defined in above code. The only one difference in both code i.e URL. URL pass in content as a attribute with some interval. Like:

Compile time : 
<head runat="server">
       <meta http-equiv="Refresh" content="10;url=Default.aspx" />
</head>

You can do the same code using c#

protected void Page_Load(object sender, EventArgs e)
{
    HtmlMeta meta = new HtmlMeta();
    meta.HttpEquiv = "Refresh";
    meta.Content = "10;url=Default.aspx";
    this.Page.Controls.Add(meta);
}

II-Method

protected void Page_Load(object sender, EventArgs e)
{
    Response.AppendHeader("Refresh", "10;url=Default.aspx");
}

If you want to out your page after session out then you can replace seconds with Session.Timeout property.

Friday, February 5, 2016

Blocked fake download button websites by Google

Thanks to google for blocking sites which  contain deceptive material and fake download button. Many sites owners focus on ads and fake clicks. So they put download button just after important information, suppose your article is related to project download and you put download advertisement in place of actual download link. When visitor comes on your website and they click on advertisement. The whole deceptive material banned by google's safe browsing tech. By using this ,you can protect yourself or computer from malware and unwanted software.

Blocked fake download button websites by Google
Source : http://arstechnica.com/
  

Google already banned those site which contain wrong information and unwanted Softwares, you can say which breaks "Social Engg attacks".  

Thursday, February 4, 2016

How to use JavaScript in Master Page ContentPlace Holder in ASP.NET

In this article, I will explain you, How to use JavaScript code in ContentPlaceHolder of Master page. If your web page (Web Form=.aspx page)  is inherit from master page then you must to use ClientID of the control to get the value. Like

document.getElementById("<%=Label1.ClientID%>");

By using above mentioned code we want to retrieve inner html text of the label. If you are using java script in simple web form which is not inherit from master page then you can use simple id of the control as parameter. Like

document.getElementById("Label1");

How to use JavaScript in Master page's Content PlaceHolder

Step-1 :  Add a Master page also add web form.
Step-2 :  Do Inherit web form from master page.
Step-3 :  Write the below mentioned code in the content place holder of the web form.

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <input id="Button1" type="button" value="button" onclick = "check()"/>
    <script type = "text/javascript">
        function check()
        {
             var label = document.getElementById("<%=Label1.ClientID%>");
             var textbox = document.getElementById("<%=TextBox1.ClientID%>");
             alert("Label Value " + label.innerHTML + "TextBox Value  " + textbox.value);
           
        }
    </script>
</asp:Content>

Note: Please mentioned javascript code at last position of the page.


Tuesday, February 2, 2016

How to use ExecuteScalar method in ASP.NET c#

This method returns integer type value, actually, it returns top tuple no of record from the table. Here I will provide you an example of ExecuteScalar ( )  method. By using this example you can retrieve top tuple no from the record. 

Source Code

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server"></asp:Label>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Get Record" />
    
    </div>
    </form>
</body>
</html>

CodeBehind Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionStringtr"].ToString();
        con.Open();

        SqlCommand cmd=new SqlCommand();
        cmd.CommandText="select * from [user_table]";
        cmd.Connection=con;
        int a = Convert.ToInt32(cmd.ExecuteScalar());
        Label1.Text = a.ToString();
    }
}

Code Generates the following output:

How to use ExecuteScalar method in ASP.NET c#

Saturday, January 30, 2016

ASP.NET GridView on JQuery Modal Popup

Introduction
In this article, I will show you how to display GridView in modal Popup, you can say how to display a division in model popup. Here we have two javascript file, both working as reference in this example also we have a style sheet to display division in proper format. By using the dialog method we can display model popup on screen. Lets check the example of model Popup which consists of GridView.

Source Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="showgridviewinmodalpopup.aspx.cs" Inherits="showgridviewinmodalpopup" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
      <link href="http://code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <script>
      $(function () {
          $("#Button1").click(function () {
              $("#popupdiv").dialog({
                  title: "Show Modal POPUP",
                  width: 350,
                  height: 300,
                  modal: true,
                  buttons: {
                      Close: function () {
                          $(this).dialog('close');
                      }
                  }



              });



          })


      });



  </script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="popupdiv" style="display:none">
    <asp:GridView runat="server" ID="g1" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="Id" HeaderText="ID" />
            <asp:BoundField DataField="username" HeaderText="UserName" />
            <asp:BoundField DataField="Password" HeaderText="Password" />
            <asp:BoundField DataField="email" HeaderText="Email" />
        </Columns>



    </asp:GridView>
    </div>
        <input id="Button1" type="button" value="button" />
           </form>
</body>
</html>

Code-Behind File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class showgridviewinmodalpopup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionStringtr"].ToString();
            con.Open();

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from [user_table]";
            cmd.Connection = con;
            SqlDataReader rd = cmd.ExecuteReader();
            g1.DataSource = rd;
            g1.DataBind();
         
        }
    }
 
}

Code Generate the following output:


Thursday, January 28, 2016

JQuery ASP.NET GridView details on Modal Popup

In this article, I will show you how to show details of the grid view's row on modal popup. Before doing this article, I will show details of row in second page. You can say that  all information of particular data shown on modal popup. Lets take an example:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GRIDVIEWDETAILS.aspx.cs" Inherits="GRIDVIEWDETAILS" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="http://code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script>
        function popup(idn,un,ps,em)
        {
            $("#userid").text(idn);
            $("#usern").text(un);
            $("#pwd").text(ps);
            $("#eml").text(em);
            $("#popupdiv").dialog({
                title: "Show modal popup window",
                width: 350,
                height: 250,
                modal: true,
                buttons: {
                    Close: function () {
                        $(this).dialog('close');
                    }
                }


            })
         
        }



    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="popupdiv" title="JQuery Modal" style="display:none">
        id:<label id="userid"></label><br />
        userName :<label id="usern"></label><br />
        Password : <label id="pwd"></label><br />
        Email : <label id="eml"></label>
        </div>


        <asp:GridView AutoGenerateColumns="false" ID="g1" runat="server">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="ID" />
                <asp:BoundField DataField="username" HeaderText="UserName" />
                <asp:BoundField DataField="Password" HeaderText="Password" />
                <asp:BoundField DataField="email" HeaderText="Email" />
              <asp:TemplateField>
                  <ItemTemplate>

                      <a href="#" onclick='popup("<%# Eval("Id") %>","<%# Eval("username") %>","<%# Eval("Password") %>","<%# Eval("email") %>")'>Check</a>

                  </ItemTemplate>

              </asp:TemplateField>

            </Columns>



        </asp:GridView>
   
 
    </form>
</body>
</html>

According to mentioned code, we have a grid view with 4 columns and one hyperlink column. Hyperlink column contains four parameters. When we click on it then all these are pass into JavaScript function. In this function, generate modal popdialog by using dialog method.

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;



public partial class GRIDVIEWDETAILS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionStringtr"].ToString();
            con.Open();

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Select * from [user_table]";
            cmd.Connection = con;

            SqlDataReader rd = cmd.ExecuteReader();
            g1.DataSource = rd;
            g1.DataBind();
        }
    }

Code generate the following output:



Friday, January 22, 2016

Merge two Data Column and bind GridView in ASP.NET

In this article, I will show you how to merge two data column into single one. First of all, bind DataTable with four columns i.e SNO, FirstName, LastName and City. Bind the DataTable source with GridView. Now, You can change the binding view of Gridview using OnRowDataBound event.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default12.aspx.cs" Inherits="Default12" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView runat="server" ID="g1" AutoGenerateColumns="false" OnRowDataBound="g1_RowDataBound">

        <Columns>

            <asp:BoundField DataField="SNO" HeaderText="Serial Number" />
            <asp:BoundField DataField="" HeaderText="Name(First+Last)" />
            <asp:BoundField DataField="City" HeaderText ="City" />

        </Columns>


    </asp:GridView>
    </div>
    </form>
</body>
</html>



Code Behind Code

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default12 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[4] { new DataColumn("SNO"), new DataColumn("FirstName"), new DataColumn("LastName"), new DataColumn("City") });
            dt.Rows.Add(1, "jacob ", "lefore", "New York");
            dt.Rows.Add(2, "Bill ", "Smith", "US1");
            dt.Rows.Add(3, "Smith ", "Bill", "US2");
            dt.Rows.Add(4, "Ammey ", "Bella", "US3");
            g1.DataSource = dt;
            g1.DataBind();
        }

    }
    protected void g1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Text = string.Format("{0}-{1}", DataBinder.Eval(e.Row.DataItem, "FirstName"), DataBinder.Eval(e.Row.DataItem, "LastName"));
        }
    }
}
© Copyright 2013 Computer Programming | All Right Reserved