-->

02 February 2020

Asp.Net GridView HyperLink Fields

  Asp.Net CS By Example       02 February 2020


 GridView HyperLink Fields 

 We use HyperLinkField to create a link to another page; HyperLinkField is particularly useful when we need to build two page Master/Detail forms.

 HyperLinkField supports the following properties:

Sr.No Property Description
1) DataNavigateUrlFields Represents the field or fields from the data source to use with DataNavigateUrlFormatString.
2) DataNavigateUrlFormatString Represents a format string that can be used to create the hyperlink.
3) DataTextField Represents a field from the data source to use for the hyperlink label.
4) DataTextFormatString Represents a format string that can be used to format the hyperlink label.
5) NavigateUrl Represents a fixed link to another page
6) Target Represents the target of a link. Possible values include blank, parent, self, and top. we can also supply the name of a frame or iframe.
7) Text Represents fixed text to display as the label for the hyperlink.

 The DataNavigateUrlFields property accepts a comma-separated list of column names. we can use multiple placeholders in DataNavigateUrlFormatString.

  Code: GridView15_HyperLinkMaster.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView15_HyperLinkMaster.aspx.cs"
    Inherits="LearnAsp.Net.ControlDemo.GridView.GridView15_HyperLinkMaster" %>

<!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>Frame Master</title>
    <style type="text/css">
        html
        {
            background-color: silver;
        }
        .content
        {
            width: 500px;
            margin: auto;
            background-color: white;
        }
        .column
        {
            padding: 10px;
            float: left;
        }
        #FrameDetails
        {
            width: 100%;
            height: 400px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div class="content">
        <div class="column">
            <asp:GridView ID="grid" AutoGenerateColumns="false" runat="server">
                <Columns>
                    <asp:HyperLinkField HeaderText="Department" DataTextField="dname" DataNavigateUrlFields="deptno"
                        DataNavigateUrlFormatString="GridView15_HyperLinkDetails.aspx?DeptId={0}" Target="FrameDetails" />
                </Columns>
            </asp:GridView>
        </div>
        <div class="column">
            <iframe name="FrameDetails" id="FrameDetails"></iframe>
        </div>
        <br style="clear: both" />
    </div>
    </form>
</body>
</html>

  Code: GridView15_HyperLinkMaster.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.Configuration;

namespace LearnAsp.Net.ControlDemo.GridView
{
    public partial class GridView15_HyperLinkMaster : System.Web.UI.Page
    {
        static string strConn = ConfigurationManager.ConnectionStrings["LearnAsp"].ToString();
        private OracleConnection objConn = new OracleConnection(strConn);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) { GridBind(); }
        }

        protected void GridBind()
        {
            String query = " select * from dept ";

            OracleCommand objCmd;
            // Retrieve Details from the DB
            objCmd = new OracleCommand(query, objConn);
            //
            DataTable dt = new DataTable();
            objConn.Open();
            OracleDataAdapter oda = new OracleDataAdapter(objCmd);
            oda.Fill(dt);
            objConn.Close();

            if (dt.Rows.Count > 0)
            {
                grid.DataSource = dt;
                grid.DataBind();
            }
            else
            {
                grid.DataSource = dt.NewRow();
                grid.DataBind();
                int columncount = grid.Rows[0].Cells.Count;
                grid.Rows[0].Cells.Clear();
                grid.Rows[0].Cells.Add(new TableCell());
                grid.Rows[0].Cells[0].ColumnSpan = columncount;
                grid.Rows[0].Cells[0].Text = "No Records Found";
            }
        }
    }
}

  Code: GridView15_HyperLinkDetails.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView15_HyperLinkDetails.aspx.cs"
    Inherits="LearnAsp.Net.ControlDemo.GridView.GridView15_HyperLinkDetails" %>

<!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">
    <h4> Employee Info</h4>
    <div>
        <asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" 
        CellPadding="4"  ForeColor="#333333">
            <Columns>
                <asp:BoundField DataField="empno" HeaderText="Employee ID" />
                <asp:BoundField DataField="ename" HeaderText="Name" />                
                <asp:BoundField DataField="sal" HeaderText="Salary" DataFormatString="{0:c}" />
            </Columns>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <EditRowStyle BackColor="#999999" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>


  Code: GridView15_HyperLinkDetails.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.Configuration;

namespace LearnAsp.Net.ControlDemo.GridView
{
    public partial class GridView15_HyperLinkDetails : System.Web.UI.Page
    {
        static string strConn = ConfigurationManager.ConnectionStrings["LearnAsp"].ToString();
        private OracleConnection objConn = new OracleConnection(strConn);

        protected void Page_Load(object sender, EventArgs e)
        {
            string DeptID = Request.QueryString["DeptId"];
            if (DeptID != null)
            {
                GridBind(DeptID);
            }
        }

        protected void GridBind(String DeptID)
        {
            String query = " select * from emp where deptno=" + DeptID + "";

            OracleCommand objCmd;
            // Retrieve Details from the DB
            objCmd = new OracleCommand(query, objConn);
            //
            DataTable dt = new DataTable();
            objConn.Open();
            OracleDataAdapter oda = new OracleDataAdapter(objCmd);
            oda.Fill(dt);
            objConn.Close();

            if (dt.Rows.Count > 0)
            {
                grid.DataSource = dt;
                grid.DataBind();
            }
            else
            {
                dt.Rows.Add(dt.NewRow());
                // Bind the DataTable which contain a blank row to the GridView
                grid.DataSource = dt;
                grid.DataBind();
                // Get the total number of columns in the GridView
                //to know what the Column Span should be
                int columnsCount = grid.Columns.Count;
                grid.Rows[0].Cells.Clear();// clear all the cells in the row
                grid.Rows[0].Cells.Add(new TableCell()); //add a new blank cell
                //set the column span to the new added cell
                grid.Rows[0].Cells[0].ColumnSpan = columnsCount;
                //You can set the styles here
                grid.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
                grid.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Red;
                grid.Rows[0].Cells[0].Font.Bold = true;
                //set No Results found to the new added cell
                grid.Rows[0].Cells[0].Text = "NO RESULT FOUND!";

            }
        }
    }
}

  Output:
logoblog

Thanks for reading Asp.Net GridView HyperLink Fields

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.