sylverfyre
sylverfyre

Reputation: 3129

Response.Redirect not working as expected - page refreshes itself, no redirect occurs

So, I'm trying to have a field in a gridview which is a link that opens a details page, based on the row clicked. The details page works when I go to it manually, but I can't seem to access it via calling a function which does a Response.Redirect(URL) - instead, I get the odd behavior that when the linkbutton is clicked, the page does a postback and stays on itself.

What am I doing wrong? Should I be using Server.Transfer() instead? I'd rather use redirect, because having the url update seems to me like an interface advantage in the use case I'm looking at.

This is part of a sharepoint webpart, the other page is on the same sharepoint server, and is a different collection of webparts.

<asp:Gridview ID="grdWU" runat="server" [+bunch of settings]>
<Columns>
   <asp:TemplateField HeaderText="Workunit #" HeaderStyle-HorizontalAlign="Left"> 
      <ItemTemplate> 
         <asp:Linkbutton ID="Workunit" runat="server" **OnClientClick="OpenDetails"** Text='<%# Bind("Workunit") %>'></asp:Linkbutton>
      </ItemTemplate> 
   </asp:TemplateField>
</Columns>
</asp:Gridview>

In the code behind class:

    protected void OpenDetails(object sender, EventArgs e)
    {
        GridViewRow clickedRow = ((LinkButton) sender).NamingContainer as GridViewRow;
        Button btnWorkunit = (Button)clickedRow.FindControl("Workunit");
          //Response.Redirect(workunitdetailsurl + "?Workunit=" + activeworkunit.ID, true);
        Response.Redirect("http://www.google.com");
    }

Out of desperation/diagnosis, I tried to redirect to google instead of my crafted URL... still nothing. The page only refreshes itself. Am I doing something wrong?

Post-answer Edit: I guess I wasn't properly understanding the difference between onclick and onclientclick.

Upvotes: 1

Views: 3535

Answers (2)

user1753042
user1753042

Reputation: 49

Response.Redirect throws ThreadAbortException... try to handle the exception and also you can try html controls like input button or hyperlinks rather than link button in item template..

Upvotes: 0

Nathan Anderson
Nathan Anderson

Reputation: 6878

Have you been able to confirm your event is actually firing? Try using the OnClick event of the LinkButton instead of OnClientClick.

Also, try using the second overload for Response.Redirect which accepts a boolean indicating to end the execution of the current page:

Response.Redirect("http://www.google.com", true); //Indicates that the execution of the current page should terminate. 

Upvotes: 1

Related Questions