I have label controls and textbox controls in the details page to which clicked GridView row data will be displayed, but when I click on a GridView row, it redirects the GridView row data to details page but in the details page it does not display the record of the GridView row data of the previous page, on the details page.
Here is my HTML and code
<asp:ScriptManager runat="server" />
<asp:UpdatePanel runat="server" ID="paneltemp">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" GridLines="None" AllowPaging="true" HeaderStyle-ForeColor="#eeeeee" HeaderStyle-Font-Bold="true" HeaderStyle-BackColor="#224f6d"
AutoGenerateColumns="false" OnSelectedIndexChanged="OnSelectedIndexChanged" OnPageIndexChanging="OnPageIndexChanging" OnRowDataBound="OnRowDataBound" class="table" Width="100%"
HeaderStyle-HorizontalAlign="left" RowStyle-HorizontalAlign="Left">
<Columns>
<asp:BoundField DataField="ContactName" HeaderText="Contact Name" />
<asp:BoundField DataField="City" HeaderText="City" ReadOnly="true" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
<hr />
<div style="float: left; font-size: 10pt; margin-right: 1%; font-weight: 600;">
Page
<asp:Label ID="lblPageIndex" runat="server" Text="Label" />
of
<asp:Label ID="lblTotalPage" runat="server" />
(<asp:Label ID="lblTotal" runat="server" />
Election(s))
<div class="dvPager">
<asp:Repeater ID="rptPager" runat="server">
<ItemTemplate>
<asp:LinkButton ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%# Eval("Value") %>'
CssClass='<%# Convert.ToBoolean(Eval("Enabled")) ? "page_enabled" : "page_disabled" %>'
OnClick="Page_Changed" OnClientClick='<%# !Convert.ToBoolean(Eval("Enabled")) ? "return false;" : "" %>'></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
</div>
</div>
<br />
<br />
</ContentTemplate>
</asp:UpdatePanel>
C#
private int PageSize = 4;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.ElectionPage(1);
}
}
private void ElectionPage(int pageIndex)
{
using (SqlConnection con = new SqlConnection("Data Source = .;DataBase=Northwind;UID=sa;PWD=pass@123;"))
{
using (SqlCommand cmd = new SqlCommand("Customers_GetCustomersPageWise", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
cmd.Parameters.AddWithValue("@PageSize", PageSize);
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
con.Open();
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
con.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);
this.PopulatePager(recordCount, pageIndex);
}
}
}
}
}
private void PopulatePager(int recordCount, int currentPage)
{
double dblPageCount = (double)((decimal)recordCount / (decimal)PageSize);
int pageCount = (int)Math.Ceiling(dblPageCount);
List<ListItem> pages = new List<ListItem>();
if (pageCount > 0)
{
if (currentPage != 1)
{
pages.Add(new ListItem("Prev", (currentPage - 1).ToString()));
}
if (pageCount < 4)
{
for (int i = 1; i <= pageCount; i++)
{
pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
}
}
else if (currentPage < 4)
{
for (int i = 1; i <= 4; i++)
{
pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
}
pages.Add(new ListItem("...", (currentPage).ToString(), false));
}
else if (currentPage > pageCount - 4)
{
pages.Add(new ListItem("...", (currentPage).ToString(), false));
for (int i = currentPage - 1; i <= pageCount; i++)
{
pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
}
}
else
{
pages.Add(new ListItem("...", (currentPage).ToString(), false));
for (int i = currentPage - 2; i <= currentPage + 2; i++)
{
pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
}
pages.Add(new ListItem("...", (currentPage).ToString(), false));
}
if (currentPage != pageCount)
{
pages.Add(new ListItem("Next", (currentPage + 1).ToString()));
}
}
rptPager.DataSource = pages;
rptPager.DataBind();
lblPageIndex.Text = currentPage.ToString();
lblTotalPage.Text = ((recordCount / PageSize) + ((recordCount % PageSize) > 0 ? 1 : 0)).ToString();
lblTotal.Text = recordCount.ToString();
}
protected void Page_Changed(object sender, EventArgs e)
{
int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
this.ElectionPage(pageIndex);
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.ElectionPage(1);
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//e.Row.Cells[2].Text = Convert.ToDateTime(e.Row.Cells[2].Text.Replace("T", " ")).ToString("d, MMM yyyy hh:mm tt");
//e.Row.Cells[1].Text = Convert.ToDateTime(e.Row.Cells[1].Text.Replace("T", " ")).ToString("d, MMM yyyy hh:mm tt");
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
e.Row.Style.Add("cursor", "pointer");
e.Row.ToolTip = "Click to select this row.";
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowIndex == GridView1.SelectedIndex)
{
Response.Redirect("summary.aspx?ElectionName=" + HttpUtility.UrlEncode(row.Cells[0].Text.Trim()));
}
}
}
DetailsPage HTML
<asp:Label ID="ContactName" runat="server" Text=""></asp:Label>
<asp:Label ID="City" runat="server" Text=""></asp:Label>
<asp:Label ID="County" runat="server" Text=""></asp:Label>
DetailsPage (C#)
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Request.QueryString["ElectionName"]);
}