In asp.net gridview i am using a image click but it is not working ?
<asp:GridView ID="GVPending" DataKeyNames="Userid" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="#">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Name"></asp:BoundField>
<asp:BoundField DataField="Branch" HeaderText="Branch"></asp:BoundField>
<asp:BoundField DataField="Address" HeaderText="Address"></asp:BoundField>
<asp:BoundField DataField="EmailAddress" HeaderText="Email"></asp:BoundField>
<asp:BoundField DataField="Phonenumber" HeaderText="Phone Number"></asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="btnEdit" runat="server" CommandName="Approve" ImageUrl="~/approve.jpg" OnClick="btnEdit_Click" ToolTip="Approve" style="
width: 30px;
"/>
<asp:ImageButton ID="btnDelete" runat="server" CommandName="Decline" ImageUrl="~/delete.jpg" OnClick="btnDelete_Click" ToolTip="Decline" style="
width: 30px;
" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code :
protected void btnEdit_Click(object sender, ImageClickEventArgs e)
{
ImageButton lbtn = (ImageButton)sender;
GridViewRow row = (GridViewRow)lbtn.NamingContainer;
if (row != null)
{
string userid = Convert.ToString(GVPending.DataKeys[row.RowIndex].Value);
}
}
In this onclick is not firing.please go through the design and code that i submitted.If anyone help it will great to me
Related
I have a grid view which has two autogenerated Edit and delete buttons in first cell itself. The code is as below
<asp:GridView ID="grdStudent" runat="server" DataKeyNames="id" AutoGenerateColumns="false"
AutoGenerateDeleteButton="true" AutoGenerateEditButton="true"
OnRowDeleting="grdStudent_RowDeleting" OnRowDataBound="grdStudent_RowDataBound" OnRowEditing="EditGrid" OnRowCancelingEdit="CancelEdit" OnRowUpdating="UpdateGrid"
ForeColor="#333333" CellPadding="4" GridLines="Both" Width="90%"
class="table table-striped table-bordered table-hover" AllowPaging="True"
EnableSortingAndPagingCallbacks="false">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#999999" BorderStyle="Solid" HorizontalAlign="Center"
VerticalAlign="Middle" />
<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"
BorderStyle="Solid" HorizontalAlign="Center" VerticalAlign="Middle" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" CssClass="table table-striped table-bordered table-hover" />
<Columns>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%#Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("first_name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtFirstName" Text='<%#Eval("first_name") %>' Width="100px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%#Eval("last_name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtLastName" Text='<%#Eval("last_name") %>' Width="100px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contact No">
<ItemTemplate>
<asp:Label ID="lblContactNo" runat="server" Text='<%#Eval("phone_no") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtContactNo" Text='<%#Eval("phone_no") %>' Width="100px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label ID="lblEmail" runat="server" Text='<%#Eval("email") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtEmail" Text='<%#Eval("email") %>' Width="100px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="lblAddress" runat="server" Text='<%#Eval("address") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtAddress" Text='<%#Eval("address") %>' Width="100px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<%-- <asp:BoundField DataField="active" HeaderText="IsActive" />--%>
<asp:TemplateField HeaderText="IsActive">
<ItemTemplate>
<asp:Label ID="lblIsActive" runat="server" Text='<%#Eval("active") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<select id="ddlIsActive" class="dropdown" runat="server">
<option value="1">True</option>
<option value="2">False</option>
</select>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Username">
<ItemTemplate>
<asp:Label ID="lblUsername" runat="server" Text='<%#Eval("username") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtUsername" Text='<%#Eval("username") %>' Width="100px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:Label ID="lblPassword" runat="server" Text='<%#Eval("password") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtPassword" Text='<%#Eval("password") %>' Width="100px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<%--<asp:ButtonField ButtonType="Link" --%>
</Columns>
</asp:GridView>
I need to access the delete button in Row Data bound event and add a confirmation attribute to it. I tried the below code but only Edit button is accessed at server side.
protected void grdStudent_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != grdStudent.EditIndex)
{
string text = ((LinkButton)e.Row.Cells[0].Controls[0]).Text;
//text always has the value as "Edit" never "Delete"
if (text == "delete")
{
(e.Row.Cells[0].Controls[0] as LinkButton).Attributes["onclick"] = "return confirm('Do you want to edit this row?');";
//del.OnClientClick = "return confirm('Are you certain you want to delete the record?');";
}
}
// (e.Row.Cells[0].Controls[0] as LinkButton).Attributes["onclick"] = "return confirm('Do you want to edit this row?');";
// //(e.Row.Cells[0].Controls[1] as LinkButton).Attributes["onclick"] = "return confirm('Do you want to delete this row?');";
//}
//if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex == grdStudent.EditIndex)
//{
// (e.Row.Cells[0].Controls[0] as LinkButton).Attributes["onclick"] = "return confirm('Do you want to edit this row?');";
//}
}
How can I access Delete button and add a confirmation message box.
You have to add the attributes as a key/value pair.
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lbEdit = e.Row.Cells[0].Controls[0] as LinkButton;
lbEdit.Attributes.Add("onclick", "return confirm('Do you want to edit this row?');");
LinkButton lbDelete = e.Row.Cells[0].Controls[2] as LinkButton;
lbDelete.Attributes.Add("onclick","return confirm('Do you want to delete this row?');");
}
#VDWWD's answer should solve the original problem.
These days, I personally like to use icon/font to look more professional.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewDemo.aspx.cs" Inherits="DemoWebForm.GridViewDemo" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
</head>
<body>
<form id="form1" runat="server">
<br />
<div class="container">
<asp:GridView runat="server" ID="grdStudent"
AutoGenerateColumns="False"
OnRowEditing="grdStudent_RowEditing"
OnRowDeleting="grdStudent_RowDeleting"
OnRowDataBound="grdStudent_RowDataBound"
CssClass="table">
<Columns>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton runat="server" ID="EditLinkButton" CommandName="Edit">
<i class="fa fa-pencil"></i>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton runat="server" ID="DeleteLinkButton" CommandName="Delete">
<i class="fa fa-trash-o"></i>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Code Behind
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
namespace DemoWebForm
{
public partial class GridViewDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
grdStudent.DataSource = Collection;
grdStudent.DataBind();
}
}
protected void grdStudent_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var d = e.Row.FindControl("DeleteLinkButton") as LinkButton;
d.Attributes.Add("onclick", "return confirm('Do you want to delete this row?');");
}
}
protected void grdStudent_RowDeleting(object sender, GridViewDeleteEventArgs e){}
protected void grdStudent_RowEditing(object sender, GridViewEditEventArgs e){}
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
}
public static IList<Item> Collection = new List<Item>
{
new Item {Id = 1, Name = "One"},
new Item {Id = 2, Name = "Two"},
new Item {Id = 3, Name = "Three"}
};
}
}
This will work better for confirmation message box at Client side:
if (e.Row.RowType == DataControlRowType.DataRow)
{
// reference the Delete LinkButton
LinkButton lb = (LinkButton)e.Row.Cells[0].Controls[2];
if (lb.Text == "Delete")
{
lb.OnClientClick = "return confirm('Are you sure you want to delete this row?');";
}
}
I have a Grid Where I have Id like this:
<div class="row ">
<asp:GridView ID="gvIndex" CssClass="table table-bordered table-responsive" runat="server" AllowPaging="True" PageSize="10" AutoGenerateColumns="False" AutoGenerateEditButton="False" OnRowCommand="gvIndex_RowCommand">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Número" HtmlEncode="false"> //This is my Id Field
<FooterStyle Width="200px" />
</asp:BoundField>
<asp:BoundField DataField="Nombre" HeaderText="Nombre" HtmlEncode="false">
<FooterStyle Width="400px" />
</asp:BoundField>
<asp:BoundField DataField="Direccion" HeaderText="Dirección" HtmlEncode="false">
<FooterStyle Width="400px" />
</asp:BoundField>
<asp:BoundField DataField="Puesto" HeaderText="Puesto" HtmlEncode="false">
<FooterStyle Width="400px" />
</asp:BoundField>
<asp:BoundField DataField="Fecha" HeaderText="Fecha" HtmlEncode="false">
<FooterStyle Width="175px" />
</asp:BoundField>
<asp:BoundField DataField="CorreoElectronico" HeaderText="Correro Electrónico" HtmlEncode="false">
<FooterStyle Width="350px" />
</asp:BoundField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Edición">
<ItemTemplate>
<asp:LinkButton ID="EditarIngreso" OnClick="EditarIngreso_Click" CommandArgument="<% Eval('Id') %>" CommandName="SelectRow" CssClass="btn btn-primary" runat="server">Editar</asp:LinkButton> //There I want to get value of Id
<%--<asp:HyperLink ID="EditarIngreso" BorderStyle="None" OnClick="EditarIngreso_Click" Text="Editar" runat="server">HyperLink</asp:HyperLink>--%>
<%--<a id="EditarIngreso" runat="server" style="text-decoration: underline; border: none">Editar</a>--%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
As you can see, I want to get value of Id in this line:
<asp:LinkButton ID="EditarIngreso" OnClick="EditarIngreso_Click" CommandArgument="<% Eval('Id') %>" CommandName="SelectRow" CssClass="btn btn-primary" runat="server">Editar</asp:LinkButton>
But I don't know what is wrong with this, I try to get it with this method:
protected void gvIndex_RowCommand(object sender, GridViewCommandEventArgs e)
{
MyID.Value = e.CommandArgument.ToString();
}
But I just receiving "<%Eval('Id') %>" instead my Id into MyID.Value
Any Help is very appreciate. Regards
You use it like this:
<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("Id") %>' runat="server" Text="Button" CommandName="SelectRow" />
And it looks you are using a regular button OnClick also, you need to remove that and use the CommandName in gvIndex_RowCommand
protected void gvIndex_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "SelectRow")
{
MyID.Value = e.CommandArgument.ToString();
}
}
First missing #
<asp:LinkButton ID="EditarIngreso" OnClick="EditarIngreso_Click" CommandArgument="<%# Eval('Id') %>" CommandName="SelectRow" CssClass="btn btn-primary" runat="server">Editar</asp:LinkButton>
In Gridview Tag
add DataKeyNames= "Id"
<asp:Gridview id="gvIndex" DataKeyNames= "Id" />
Then OnClick of LinkButton
protected void EditarIngreso_Click(object sender,EventArgs e)
{
LinkButton btn = sender as LinkButton ;
GridViewRow row = btn.NamingContainer as GridViewRow;
string pk = gvIndex.DataKeys[row.RowIndex].Values[0].ToString();
// here PK is your ID
// Convert it to Int32
}
<asp:GridView ID="grdAdslCompanyAdvisers" Width="100%" HeaderStyle-BackColor="ActiveCaption"
HeaderStyle-ForeColor="Black" AlternatingRowStyle-CssClass="FormTableContainer"
AutoGenerateColumns="False" ForeColor="Black" runat="server" DataKeyNames="Days">
<Columns>
<asp:HyperLinkField HeaderText="Name" DataTextField="Name" DataNavigateUrlFields="ID" DataNavigateUrlFormatString="#MyDiv " />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Email" HeaderText="Email" />
</Columns>
</asp:GridView>
<div id ="MyDiv"></div>
I need to show a div section(ClientDetail wil show in this div) as per the selected Client Name.I know DataNavigateUrlFormatString will use for redirecting to destination page. But I need show Div section on same opage. And If there is other option , then plz Kindly suggest.
Update your GridView code as below , add TemplateColumn, add OnRowCommand="grdAdslCompanyAdvisers_RowCommand"
<asp:UpdatePanel ID="upd" runat="server">
<ContentTemplate>
<asp:GridView ID="grdAdslCompanyAdvisers" Width="100%" HeaderStyle-BackColor="ActiveCaption"
HeaderStyle-ForeColor="Black" AlternatingRowStyle-CssClass="FormTableContainer"
AutoGenerateColumns="False" ForeColor="Black" runat="server" DataKeyNames="ID" OnRowCommand="grdAdslCompanyAdvisers_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btn" runat="server" Text='<%#Eval("Name")%>' CommandArgument='<%#Eval("ID") %>'' CommandName="View" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Email" HeaderText="Email" />
</Columns>
</asp:GridView>
<div id ="MyDiv">
<asp:Label ID="lbl" runat="server" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
In code behind
protected void grdAdslCompanyAdvisers_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "View") {
//Get Command Argument
e.CommandArgument.ToString(); // get ID and display details by adding some Label/Gridview details in MyDiv
}
}
I have asp:GridView. I can edit all columns without problems, except this one:
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="hyperlinkEdocs" runat="server" Target="HyperLink"
HeaderText="Dopolnitve (eDocs)"
NavigateUrl='<%# String.Format("http://{0}", Eval("CUSTOMER").ToString()) %>'
Text='<%# Eval("CUSTOMER") %>'></asp:HyperLink>
</ItemTemplate>
I am editing columns by click on 'Edit' link (this triggers that columns content is displayed in a text boxes).
How can I make this column editable, so I will be able to edit and update hyperlink in column?
<asp:GridView runat="server" ID="gvrecords" AutoGenerateColumns="false"
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White" DataKeyNames="UserId">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" Text='<%# Bind("Name") %>' NavigateUrl='<%# Bind("Name", "~/Images/{0}") %>' runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserName" HeaderText="UserName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="Email" HeaderText="Email" />
</Columns>
</asp:GridView
I'm not 100% sure what you are trying to do because I can't see your code behind but why don't you use an edit item template?
<asp:TemplateField HeaderStyle-Width="100px" HeaderText = "ABC">
<ItemTemplate>
<asp:Label ID="label" runat="server"
Text='<%# Eval("id")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="textbox1" Width="100px" Height="50px" runat="server"
Text='<%# Eval("id")%>'></asp:TextBox>
</EditItemTemplate>
<HeaderStyle Width="80px" HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
Edit link click open textbox
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
grd_view();
}
I have two imagebuttons which are associated with a modalpopupextender. When I click imagebutton1, it opens up the panel and within that panel there is a grid which loads with the information. However, when I click Imagebutton2 it opens up the panel but does not display the gridview even though that gridview tag is under that panel.
I am pretty new with web, please help. Thank You. Please find the code below:
<asp:Panel ID="PNL" ScrollBars="Auto" runat="server" Style="border: 2px solid Black; display: none; width: auto;
background-color: White; padding: 20px; text-align: center;" Height="444">
<asp:Label ID="LabelCount" runat="server" Text="Label" Font-Size="Large"
style="text-align: center"></asp:Label>
<asp:Button ID="ButtonOK" Text="OK" runat="server" />
<%-- Area for keeping POPUP grid ot window or picture DO NOT PUT UNDER TABLE Taj 3-Jan-2010--%>
<asp:GridView ID="gvUsersWHYME" runat="server" BackColor="White" BorderColor="#010101"
BorderStyle="None" CaptionAlign="Top" CellPadding="1" CellSpacing="2" GridLines="None"
Style="margin-left: 0px" DataSourceID="SqlDataSource0" AutoGenerateColumns="False"
EnableTheming="False" AllowSorting="True"
OnRowDataBound="gvUsers_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Roles" Visible="False">
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server"
onclick="javascript:SelectAllCheckboxesSpecific(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkGenerate" runat="server"
onclick="javascript:HighlightRow(this);" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProjectNum" HeaderText="Release#" SortExpression="ProjectNum"
ReadOnly="true" />
<asp:BoundField DataField="IPMRefNum" HeaderText="IPM Ref #"
SortExpression="IPMRefNum" />
<asp:BoundField DataField="TestprojectNo" HeaderText="Release Description" SortExpression="TestprojectNo"
ItemStyle-Width="444">
<ItemStyle Width="444px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="ProjectName" HeaderText="ProjectName" SortExpression="ProjectName"
Visible="False" />
<asp:BoundField DataField="ProposedPhaseName" HeaderText="Proposed Phase Name" ItemStyle-Wrap="False"
SortExpression="ProposedPhaseName">
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="DependencyList" HeaderText="Dependency List"
SortExpression="DependencyList" />
<asp:BoundField DataField="QA_Planned_EndDate" HeaderText="Release Handover Date"
SortExpression="QA_Planned_EndDate" DataFormatString="{0:d}"
ItemStyle-Wrap="False">
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Applications" HeaderText="Impacted Application(s)"
SortExpression="Applications" />
<asp:BoundField DataField="AppCount" HeaderText="Count"
SortExpression="AppCount" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#330099" />
<RowStyle BackColor="White" ForeColor="#330099" />
<HeaderStyle BackColor="#ACCDF6" Font-Bold="True" ForeColor="#FFFFCC" />
</asp:GridView>
<asp:Label ID="lblCountImpacted" runat="server" Text="Total Impacted App."
Font-Size="Large"></asp:Label>
</asp:Panel>
<br />
<br />
<br />
<br />
<br />
<asp:Panel ID="Panel1" ScrollBars="Auto" runat="server" Style="border: 2px solid Black; display: none; width: auto;
background-color: White; padding: 20px; text-align: center;" Height="444">
<asp:Button ID="Button1" Text="OK" runat="server" />
<asp:GridView ID="GridView4" runat="server" BackColor="White" BorderColor="#010101"
BorderStyle="None" CaptionAlign="Top" CellPadding="1" CellSpacing="2" GridLines="None"
Style="margin-left: 0px" DataSourceID="SqlDataSource6" AutoGenerateColumns="False"
EnableTheming="False" AllowSorting="True"
OnRowDataBound="GridView4_RowDataBound">
<Columns>
<asp:BoundField DataField="ProjectNum" HeaderText="Release#" SortExpression="ProjectNum"
ReadOnly="true" />
<asp:BoundField DataField="IPMRefNum" HeaderText="IPM Ref #"
SortExpression="IPMRefNum" />
<asp:BoundField DataField="TestprojectNo" HeaderText="Release Description" SortExpression="TestprojectNo"
ItemStyle-Width="444">
<ItemStyle Width="444px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="ProjectName" HeaderText="ProjectName" SortExpression="ProjectName"
Visible="False" />
<asp:BoundField DataField="ProposedPhaseName" HeaderText="Proposed Phase Name" ItemStyle-Wrap="False"
SortExpression="ProposedPhaseName">
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="DependencyList" HeaderText="Dependency List"
SortExpression="DependencyList" />
<asp:BoundField DataField="QA_Planned_EndDate" HeaderText="Release Handover Date"
SortExpression="QA_Planned_EndDate" DataFormatString="{0:d}"
ItemStyle-Wrap="False">
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#330099" />
<RowStyle BackColor="White" ForeColor="#330099" />
<HeaderStyle BackColor="#ACCDF6" Font-Bold="True" ForeColor="#FFFFCC" />
</asp:GridView>
</asp:Panel>
I have created a sample code.Similar to your problem.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="MasterDetail.aspx.cs"
Inherits="MasterDetail" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxtoolkit" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form" runat="server">
<asp:ScriptManager ID="scriptManager" runat="server" />
<div>
<asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblTitle" runat="server" Text="Customers" BackColor="lightblue" Width="95%" />
<asp:GridView ID="gvCustomers" runat="server" DataKeyNames="Id" AutoGenerateColumns="false"
AllowPaging="true" AllowSorting="true" PageSize="10" Width="95%">
<AlternatingRowStyle BackColor="aliceBlue" />
<HeaderStyle HorizontalAlign="Left" />
<Columns>
<asp:TemplateField ControlStyle-Width="50px" HeaderStyle-Width="60px">
<ItemTemplate>
<asp:Button ID="btnViewDetails" runat="server" Text="Details" OnClick="BtnViewDetails_Click" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="ID" SortExpression="Id" ReadOnly="true" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="contactname" ReadOnly="true" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="btnShowPopup" runat="server" Style="display: none" />
<ajaxtoolkit:ModalPopupExtender ID="mdlPopup" runat="server" TargetControlID="btnShowPopup"
PopupControlID="pnlPopup" CancelControlID="btnClose" BackgroundCssClass="modalBackground" />
<asp:Panel ID="pnlPopup" runat="server" Width="500px" Style="display: none">
<asp:UpdatePanel ID="updPnlCustomerDetail" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblCustomerDetail" runat="server" Text="Customer Detail" BackColor="lightblue"
Width="95%" />
<asp:DetailsView ID="dvCustomerDetail" runat="server" DefaultMode="Edit" Width="95%"
BackColor="white" />
</ContentTemplate>
</asp:UpdatePanel>
<div align="right" style="width: 95%">
<asp:Button ID="btnSave" runat="server" Text="Save"
OnClientClick="alert('Sorry, but I didnt implement save '); return false;"
Width="50px" />
<asp:Button ID="btnClose" runat="server" Text="Close" Width="50px" />
</div>
</asp:Panel>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class MasterDetail : System.Web.UI.Page
{
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
ds = Data();
if (!this.IsPostBack)
{
if (Session["parent"] == null)
{
gvCustomers.DataSource = ds.Tables["Parent"];
gvCustomers.DataBind();
}
else
{
gvCustomers.DataSource = Session["Parent"] as DataTable;
gvCustomers.DataBind();
}
}
}
private DataSet Data()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add(new object[] { 1, "aaaa" });
dt.Rows.Add(new object[] { 2, "bbbb" });
dt.Rows.Add(new object[] { 3, "cccc" });
dt.TableName = "Parent";
DataTable dtc = new DataTable();
dtc.Columns.Add("Id", typeof(int));
dtc.Columns.Add("Qul", typeof(string));
dtc.Rows.Add(new object[] { 1, "aaaa" });
dtc.Rows.Add(new object[] { 2, "bbbb" });
dtc.Rows.Add(new object[] { 3, "bbbb" });
dtc.TableName = "Child";
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.Tables.Add(dtc);
Session["Parent"] = dt;
return ds;
}
protected void BtnViewDetails_Click(object sender, EventArgs e)
{
// get the gridviewrow from the sender so we can get the datakey we need
Button btnDetails = sender as Button;
GridViewRow row = (GridViewRow)btnDetails.NamingContainer;
// extract the id from the row whose details button originated the postback.
// grab the customerid and feed it to the customer details datasource
// finally, rebind the detailview
DataView dv = new DataView(ds.Tables["Child"]);
dv.RowFilter = "Id=" + Convert.ToString(this.gvCustomers.DataKeys[row.RowIndex].Value);
dvCustomerDetail.DataSource = dv;
dvCustomerDetail.DataBind();
// update the contents in the detail panel
this.updPnlCustomerDetail.Update();
// show the modal popup
this.mdlPopup.Show();
}
}