0

I have a button inside gridview and i am trying to redirect it to a page. The following is my code for the template view.

<asp:TemplateField ShowHeader="False">
              <ItemTemplate>
                 <asp:Button ID="btnView" runat="server" CausesValidation="false" OnClick="btnView_Click" Text="View" Font-Size="Small"></asp:Button>
              </ItemTemplate>
           </asp:TemplateField>

I tried the following solution for the OnClick.

 protected void btnView_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    GridViewRow row = (GridViewRow)btn.NamingContainer;

    Response.Redirect("Customer.aspx" );
}

My code isn't working fro some reason and the button doesn't redirect. I looked at other solutions as well but I they are not working for me. Can someone please let me know what I am missing here. Thank you.

1 Answers1

0

Code working at my end. Try checking page load event in server side. If there are, then perhaps by adding IsPostBack will help. Refer to this post.

Aspx Page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="tempform.aspx.cs" Inherits="PivotTest.tempform" %>

<!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>temp test Page</title>
</head>
<body>
    <form id="random" runat="server">
        <asp:GridView ID="demo" runat="server"
            AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField HeaderText="temp" DataField="Col1" />
                <asp:TemplateField HeaderText="Run">
                    <ItemTemplate>
                        <asp:Button ID="btnView" runat="server" CausesValidation="false" OnClick="btnView_Click" Text="View" Font-Size="Small"></asp:Button>
                    </ItemTemplate>
                    <HeaderStyle Width="88px" />
                </asp:TemplateField>

            </Columns>
        </asp:GridView>
    </form>
</body>
</html>

Aspx.cs Page:

using Samples.SampleData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PivotTest
{
    public partial class tempform : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                demo.DataSource = new List<Temp>() {
                    new Temp() { Col1="1"},
                    new Temp() { Col1="1"},
                    new Temp() { Col1="1"},
                    new Temp() { Col1="1"},
                    };

                demo.DataBind();
            }
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            GridViewRow row = (GridViewRow)btn.NamingContainer;

            Response.Redirect("Customer.aspx");
        }
    }
}
Ajay
  • 643
  • 2
  • 10
  • 27
  • I think the problem with `URL` that he is providing in `Response.Redirect`, question code is fine. –  Jul 11 '17 at 10:31