Get Id from GridView using Button - asp.net

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
}

Related

Onclick Image button is not working Asp.net Grid View

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

How to show div with the help of HyperLinked Field in a grid

<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
}
}

fileupload not working within gridview inside update panel

i have file upload control in gridview and that gridview is inside update panel
when i try to update gridview everything works but image path from fileupload don't save
please help me...
page.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" CellSpacing="4"
DataKeyNames="pid" ForeColor="Black" ShowHeaderWhenEmpty="True"
GridLines="Horizontal" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="Operation">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" ForeColor="#94b52c"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" ForeColor="#94b52c"
OnClientClick="return confirm('Are You Sure Want To Delete ?');"></asp:LinkButton>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Product ID" InsertVisible="False" SortExpression="pid">
<EditItemTemplate>
<asp:Label ID="lblpid" runat="server" Text='<%# Eval("pid") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblproductid" runat="server" Text='<%# Bind("pid") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Detail" SortExpression="pdetail">
<EditItemTemplate>
<asp:TextBox ID="txtproductdetail" runat="server" Text='<%# Bind("pdetail") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblproductdetail" runat="server" Text='<%# Bind("pdetail") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Image" SortExpression="pimage">
<EditItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="imgproductimage" runat="server" ImageUrl='<%# Bind("pimage") %>' Height="50px" Width="50px"/>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#F0F0F0" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
here is .cs file
public void bindgrid()
{
string qry = "select pid,pdetail,pimage from productdetail p,categorydetail c where p.cid=c.cid";
GridView1.DataSource = abal.Qry_Fire(qry);
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bindgrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label l = (Label)GridView1.Rows[e.RowIndex].FindControl("lblpid");
TextBox txtproductdetail = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtproductdetail");
FileUpload f = (FileUpload)GridView1.Rows[e.RowIndex].FindControl("FileUpload1");
string path = "~/user/product_image/" + f.FileName.ToString();
int msg = abal.Qry_All("update productdetail set pdetail='" + txtproductdetail.Text + "',pimage='" + path
+ "' WHERE pid='" + Convert.ToInt32(l.Text) + "'");
if(msg==1)
f.SaveAs(Server.MapPath(path));
GridView1.EditIndex = -1;
bindgrid();
}
I had same kind of problem and fixed it by using below solution.
Add the following code in your code behind.
Page.Form.Attributes.Add("enctype", "multipart/form-data");
protected void gvLineItems_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState.Equals(DataControlRowState.Edit))
{
Button btnUpload = e.Row.FindControl("btnUpload") as Button;
ScriptManager.GetCurrent(this).RegisterPostBackControl(btnUpload);
}
}
}
I am able to retrive the fileUpload control value after adding the above code.
protected void UpdateRow(object sender, GridViewUpdateEventArgs e)
{
FileUpload uploadedFile = (FileUpload)dgDocuments.Rows[editIndex].FindControl("UploadFile");
if (uploadedFile.HasFile)
{
uploadedFile.SaveAs(FileUploadURL + "\\Temp\\" + uploadedFile.FileName);
}
}
just add PostBackTrigger after </ContentTemplate> for the FileUploader as below:
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="FileUpload1" />
</Triggers>
</asp:UpdatePanel>
Update if its inside a gridview then you can try the below code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
FileUpload flUpload = e.Row.FindControl("FileUpload1") as FileUpload;
ScriptManager.GetCurrent(this).RegisterPostBackControl(flUpload);
}
Update2 add the event OnRowDataBound in the gridview:
<asp:gridview
id="GridView1"
onrowdatabound="GridView1_RowDataBound"
rest add your code for gridview
add this: Page.Form.Attributes.Add("enctype", "multipart/form-data"); to page load event:
protected void Page_Load(object sender, EventArgs e)
{
Page.Form.Attributes.Add("enctype", "multipart/form-data");
..............................
}
and Try This.
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="GridView1" />
</Triggers>
</asp:UpdatePanel>
Update panel dosen't work properly with some controls similarly with Fileupload..So try to do it with ajax call which is a better option.
Just Add "Page.Form.Attributes.Add("enctype", "multipart/form-data");" as posted by Jisha Muthuswamy. FileUpload control inside the gridview wrapped by UpdatePanel can never be accessed and thus cannot be set as a PostBackTrigger ControlID. Thanks

Asp.Net: Gridview OnPageIndexChanging not firing server method

I am not able to change the page index of the gridview. The server method for OnPageIndexChanging is not at all getting fired. I do not know what am I doing wrong here.
Here's my gridview
<asp:GridView ID="VideoCommentsGrid" runat="server"
OnRowDataBound="VideoCommentsGrid_RowDataBound"
OnPageIndexChanging="VideoCommentsGrid_PageIndexChanging" allowpaging="true"
CssClass="tables"
EmptyDataText="<div class='notice show bottom'>No Comments found.</div>"
AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="center" HeaderStyle-Width="70" ItemStyle-HorizontalAlign="center">
<HeaderTemplate>
Approve
<br />
<input id="ChkAllApprovedItems" type="checkbox" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkApproval" Checked='<%#Eval("IsApproved").ToString()=="1"?true:false %>' runat="server" />
<asp:Label ID="lblCommentID" runat="server" Text='<%#Eval("CommentId") %>' CssClass="hide"/>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-HorizontalAlign="center" HeaderStyle-Width="70" ItemStyle-HorizontalAlign="center">
<HeaderTemplate>
Reject
<br />
<input id="ChkAllRejectedItems" type="checkbox" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkReject" Checked='<%#Eval("IsRejected").ToString()=="1"?true:false %>' runat="server" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="User Name" HeaderStyle-Width="70" >
<ItemTemplate>
<asp:Label ID="lblUserName" runat="server" Text='<%#Eval("FirstName")%>'> </asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="150" />
<ItemStyle HorizontalAlign="Left" CssClass="wordWrap" Width="150" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Comments" HeaderStyle-Width="70" >
<ItemTemplate>
<asp:Label ID="lblComment" runat="server" Text='<%#Eval("VideoComment") %>' />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="150" />
<ItemStyle HorizontalAlign="Left" CssClass="wordWrap" Width="150" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Comment Time* ">
<ItemTemplate>
<asp:Label ID="lblCommentDate" runat="server" Text='<%#(Eval("CommentCreatedDate"))%>'> </asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="80" />
<ItemStyle HorizontalAlign="Center" Width="80" />
</asp:TemplateField>
</Columns>
<HeaderStyle Height="30" />
<PagerStyle HorizontalAlign="Center" CssClass="footer" />
<AlternatingRowStyle CssClass="odd" />
</asp:GridView>
My server side code is as follows,
protected void Page_Load(object sender, EventArgs e)
{
LoadCommentsGridView(VideoCommentsGrid.PageIndex);
}
protected void VideoCommentsGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (!e.Row.RowType.Equals(DataControlRowType.DataRow)) return;
}
protected void VideoCommentsGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
VideoCommentsGrid.PageIndex = e.NewPageIndex;
LoadCommentsGridView(VideoCommentsGrid.PageIndex);
hidCheckedValue.Value = string.Empty;
}
protected void LoadCommentsGridView(int PageIndex)
{
SetPageIndex(PageIndex);
LoadDefaultGrid();
}
private void LoadDefaultGrid()
{
VideoCommentsGrid.PageSize = CurrentSchoolDetails.PageViewCount;
IList<Comment> allComments = CommentRepository.GetAllCommentsByVideoID(VideoID);
BindDataControls.BindGridView(VideoCommentsGrid, allComments);
}
Please help me out,
Thanks.
I think this is occurring because you are binding the data to the GridView on every page load, where as you only need to bind the data once initially and then again when the page index changes.
Try changing the code in the Page_Load method like so:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
LoadCommentsGridView(VideoCommentsGrid.PageIndex);
}
}
on the rowdatabound method try to comment this piece of code and see what happen.
//if (!e.Row.RowType.Equals(DataControlRowType.DataRow)) return;

Gridview not displaying in Panel associated with a ModalPopupExtender

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();
}
}

Resources