Description:-
in this
Example we explain that how to create a link in ItemTemplate of Gridview means
create link in each record and when click on this link the page is redirect to
this page Automatically.
This type
of Facility is Availabe in GMAIL. We allow know that GMAIL provide a facility of link
in every mail in INBOX and you can click on it and open the MAIL very easily.
In this example i will let you know that how can you pass
value from GridView Row data value like id to another page .Here i used
Linkbutton for this inside GridView same like as we show in Gmail
Email, When you get redirected to the new page you can get the id of this
record in the query string for further data procesing.
<asp:TemplateField HeaderText="subject" >
<ItemTemplate>
<a href="data.aspx?id=<%#
Eval("id")%>" style="text-decoration:none"> <asp:Label ID="Label2" runat="server" Text= '<%# Eval("subject") %>'></asp:Label></a>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#
Eval("subject") %>'></asp:TextBox>
</EditItemTemplate>
Look
above Code we Create Link in Subject Field Same Like Gmail and when user click
on this Link the page is Redirect data.aspx and open this page same as the Mail is
open Gmail.so this is a Functionality we providing in this Example same like
Gmail.
So
that’s type of Facility we can also provide to our user and for that we have to
create a LINK in a ITEMTEMPLATE of a GRIDVIEW.
how to Create or Generate thumbnails from images Create thumbnails of Image in asp.net
How to call code behind page method from json or jquery in asp.net call aspx code behind method in json/jquery
Extract unzip Files into zip folder Extract or convert unzip files to zip folder in asp.net
inbox.aspx:-
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="inbox.aspx.cs" Inherits="inbox" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript">
function mouseIn(row) {
document.getElementById("panels").style.display
= 'block';
row.style.backgroundColor = '#D3DFF8';
$("#panels").slideToggle("slow");
}
//Function
to change color of grid row on mouseout
function mouseOut(row) {
row.style.backgroundColor = '#FFFFFF';
}
</script>
<h2 align="center">welcome <% Response.Write(Request.QueryString["unm"]); %></h2>
<br /> <br />
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True"
oncheckedchanged="CheckBox2_CheckedChanged" Text="Select All"
BackColor="#0066FF" ForeColor="White" />
<asp:Button ID="Button1" runat="server" Text="Delete"
onclick="Button1_Click" BackColor="#0066FF" Font-Bold="True"
ForeColor="White" />
<asp:GridView ID="GridView1" runat="server" Width="100%" CellPadding="4"
ForeColor="#333333" GridLines="None" AutoGenerateColumns="False"
DataKeyNames="id" onselectedindexchanged="GridView1_SelectedIndexChanged"
AllowPaging="True" onpageindexchanging="GridView1_PageIndexChanging" onrowdatabound="GridView1_RowDataBound"
>
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:TemplateField InsertVisible="false" ShowHeader="false">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="id" />
<asp:TemplateField HeaderText="Name" >
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text= '<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="subject" >
<ItemTemplate>
<a href="data.aspx?id=<%# Eval("id")%>" style="text-decoration:none"> <asp:Label ID="Label2" runat="server" Text= '<%# Eval("subject") %>'></asp:Label></a>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("subject") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date" >
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text= '<%# Eval("date") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("date") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#66FF33" Font-Bold="True" ForeColor="White"
BorderStyle="Solid" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<div id="panels" style="display:none; position:absolute">
<asp:Image ID="i1" Width="50" Height="50" runat="server" AlternateText="photo" CssClass="im" />
<asp:Button ID="Button2" CssClass="b1" runat="server" Text="Remove" BackColor="#0066FF" Font-Bold="True" ForeColor="White" />
<asp:Button ID="Button3" CssClass="b2" runat="server" BackColor="#0066FF" Text="LogOut" ForeColor="White" Font-Bold="True" BorderColor="Blue" />
</div>
</asp:Content>
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.Data;
using System.Collections;
public partial class inbox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["unm"] == null)
{
Response.Redirect("~/login.aspx");
}
else
{
grid();
Menu inm = (Menu)this.Master.FindControl("Menu1");
string cnt = GridView1.Rows.Count.ToString();
foreach (MenuItem mi in inm.Items)
{
if (mi.Text == "Inbox")
{
mi.Text = mi.Text + "(" + cnt +
")";
}
}
//int
c = GridView1.Rows.Count;
//this.MasterPageFile
= "~/MasterPage.master?count=" + c;
}
}
}
protected void grid()
{
string unm1 = Request.QueryString["unm"];
DataSet ds = new DataSet();
string con = @"Data
Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";
string q = "select
id,name,subject,date from " + Session["unm"].ToString();
string cnt = "select
count(*)from " + Session["unm"].ToString()
+ "s";
SqlConnection conn = new SqlConnection(con);
SqlCommand cmd = new SqlCommand(q, conn);
SqlCommand cmd1 = new SqlCommand(cnt, conn);
SqlDataAdapter sa = new SqlDataAdapter();
conn.Open();
cmd.ExecuteNonQuery();
sa.SelectCommand = cmd;
sa.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
string co = cmd1.ExecuteScalar().ToString();
Menu inm = (Menu)this.Master.FindControl("Menu1");
foreach (MenuItem mi in inm.Items)
{
if (mi.Text == "Sent")
{
mi.Text = mi.Text + "(" + co + ")";
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int i = 0;
foreach (GridViewRow grdRow in GridView1.Rows)
{
CheckBox c =
(CheckBox)(GridView1.Rows[grdRow.RowIndex].Cells[0].FindControl("CheckBox1"));
string a = GridView1.Rows[grdRow.RowIndex].Cells[1].Text;
if (c.Checked)
{
string con = @"Data
Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";
string q = "delete from " + Session["unm"].ToString() + "
where id = " + a.ToString();
SqlConnection conn = new SqlConnection(con);
SqlCommand cmd = new SqlCommand(q, conn);
conn.Open();
i = cmd.ExecuteNonQuery();
}
}
grid();
if (i > 0)
ClientScript.RegisterClientScriptBlock(this.GetType(), "key", "<script>
alert('delete successfully');</script>");
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox2.Checked)
{
foreach (GridViewRow grdRow in GridView1.Rows)
{
CheckBox c =
(CheckBox)(GridView1.Rows[grdRow.RowIndex].Cells[0].FindControl("CheckBox1"));
c.Checked = true;
}
}
else
{
foreach (GridViewRow grdRow in GridView1.Rows)
{
CheckBox c =
(CheckBox)(GridView1.Rows[grdRow.RowIndex].Cells[0].FindControl("CheckBox1"));
c.Checked = false;
}
}
}
protected void GridView1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
grid();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Binding
client side functions to GridView row
e.Row.Attributes.Add("onmouseover", "mouseIn(this);");
e.Row.Attributes.Add("onmouseout", "mouseOut(this);");
}
}
catch (Exception ex)
{
throw;
}
}
}
thanks for this nice tutorial.
ReplyDeletemy question is when we navigate to another page we have the details in a detailsview control.
that detailsview data to be stored in another page when we click the button in another page.please help me.