Gridview (RowClick) Selected Checkbox accidentally redirects to another page

Donald Symmons 2,861 Reputation points
2022-11-29T11:49:33.207+00:00

I am having some irregularities in my gridview. I have a gridview with columns. The first column contains checkbox, that when selected can give room for record to be deleted. While there are other columns that display records from database table. In the same gridview, I have “OnRowDataBound” and “OnSelectedIndexChanged” functions.

The issue is that when I click to select a checkbox, it redirects me to another page. Could it be due to the “OnSelectedIndexChanged”?

Please how can I get rid of this problem?

Here is my Gridview and Code

<asp:UpdatePanel ID="panel" runat="server">  
                       <ContentTemplate>  
                           <div class="container-fluid p-3 mb5 bg-white rounded" id="card" style="margin: 0 auto; padding: 10px; border: 1.3px solid #e4e7e8;">  
                               <div class="grid-corner" style="width: 100%; background-color: white; font-size: 9pt; margin: 0 auto; padding: 5px; overflow: scroll;">  
                                   <asp:GridView ID="GridView1" runat="server" GridLines="None" DataKeyNames="reference" AllowPaging="true" HeaderStyle-ForeColor="#05214d" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="11pt" Font-Size="10pt"  
                                       AutoGenerateColumns="false" OnSelectedIndexChanged="OnSelectedIndexChanged" OnRowDataBound="OnRowDataBound" OnPageIndexChanging="OnPageIndexChanging" class="table" Width="100%">  
                                       <EmptyDataTemplate>  
                                           <div style="text-align: center; font-weight: bolder;">  
                                               <hr />  
                                               <hr />  
                                               <asp:Label ID="labelTemp" runat="server" Font-Size="12pt" Font-Bold="true" Text="No Record"></asp:Label>  
                                               <hr />  
                                               <hr />  
                                           </div>  
                                       </EmptyDataTemplate>  
                                       <Columns>  
                                           <asp:TemplateField>  
                                               <HeaderTemplate>  
                                                   <asp:CheckBox ID="chkAll" runat="server" onclick="checkAll(this);" />  
                                               </HeaderTemplate>  
                                               <ItemTemplate>  
                                                   <asp:CheckBox ID="checker" runat="server" onclick="All_Check(this);" />  
                                               </ItemTemplate>  
                                           </asp:TemplateField>  
                                           <asp:BoundField DataField="reference" HeaderText="Reference" />  
                                           <asp:BoundField DataField="email" HeaderText="Created By" />  
                                           <asp:BoundField DataField="Name" HeaderText="Client" />  
                                       </Columns>  
                                   </asp:GridView>  
                                   <div style="float: right; font-size: 10pt; margin-right: 1%;">  
                                       Showing&nbsp;Page&nbsp;  
       <asp:Label ID="lblPageIndex" runat="server" Text="Label" />  
                                       &nbsp;of&nbsp;  
       <asp:Label ID="lblTotalPage" runat="server" />  
                                       (<asp:Label ID="lblTotal" runat="server" />  
                                       Records)&nbsp;&nbsp;  
       <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="Change_Page" OnClientClick='<%# !Convert.ToBoolean(Eval("Enabled")) ? "return false;" : "" %>'></asp:LinkButton>  
               </ItemTemplate>  
           </asp:Repeater>  
       </div>  
                                   </div>  
                                   <br />  
                                   <br />  
                               </div>  
                           </div>  
                       </ContentTemplate>  
                   </asp:UpdatePanel>  

CODE BEHIND

private int PageSize = 5;  
   protected void Page_Load(object sender, EventArgs e)  
   {  
       if (!this.IsPostBack)  
       {  
           this.GetCustomersPageWise(1);  
       }  
   }  
   
   private void GetCustomersPageWise(int pageIndex)  
   {  
       using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security = True"))  
       {  
           using (SqlCommand cmd = new SqlCommand("Datareg", con))  
           {  
               cmd.CommandType = CommandType.StoredProcedure;  
               cmd.Parameters.AddWithValue("@CreatedBy", createby.Text.Trim());  
               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);  
                       GridView1.DataSource = dt;  
                       GridView1.DataBind();  
                       con.Close();  
                       int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);  
                       this.PopulatePager(recordCount, pageIndex);  
                   }  
               }  
           }  
       }  
   }  
   
   protected void Change_Page(object sender, EventArgs e)  
   {  
       int pageIndex = int.Parse((sender as LinkButton).CommandArgument);  
       this.GetCustomersPageWise(pageIndex);  
   }  
   
   protected void Page_Changed(object sender, EventArgs e)  
   {  
       int pageIndex = int.Parse((sender as LinkButton).CommandArgument);  
       this.CustomersPage(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 OnPageIndexChanging(object sender, GridViewPageEventArgs e)  
   {  
       GridView1.PageIndex = e.NewPageIndex;  
       this.GetCustomersPageWise(1);  
   }  
   protected void OnRowDataBound(object sender, GridViewRowEventArgs e)  
   {  
       if (e.Row.RowType == DataControlRowType.DataRow)  
       {  
           e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);  
           e.Row.Style.Add("cursor", "pointer");  
           e.Row.ToolTip = "View Details.";  
       }  
   }  
   protected void OnSelectedIndexChanged(object sender, EventArgs e)  
   {  
       foreach (GridViewRow row in GridView1.Rows)  
       {  
           if (row.RowIndex == GridView1.SelectedIndex)  
           {  
               int index = GridView1.SelectedIndex;  
               Session["RowIndex"] = GridView1.DataKeys[index].Value.ToString();  
               Response.Redirect("details.aspx");  
           }  
       }  
   }  
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,412 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,632 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jose Zero 576 Reputation points
    2022-11-29T21:31:43.463+00:00

    As already said, your code is working exactly as written.
    Since you have OnSelectedIndexChanged, it gets fired when you click.
    Just remove OnSelectedIndexChanged, it should work


  2. Donald Symmons 2,861 Reputation points
    2022-11-30T09:56:46.923+00:00

    I have been able to make it work.
    In the the OnRowDataBound, I applied onclick event to each cell of the rows except the CheckBox column, and it is working

    Here is how

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)  
    {  
        if (e.Row.RowType == DataControlRowType.DataRow)  
        {  
            for (int i = 1; i < e.Row.Cells.Count; i++)  
            {  
                e.Row.Cells[i].Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);  
                e.Row.Cells[i].Style.Add("cursor", "pointer");  
                e.Row.Cells[i].ToolTip = "View Details.";  
            }  
        }  
    }  
    
    0 comments No comments