Solved

Programmatically Set NavigateURL in a GridView

Posted on 2007-03-21
4
2,042 Views
Last Modified: 2011-09-20
I have a grid view in a UserControl, say uc1.ascx. One of the columns is a TemplateField and contains a hyperlink. Something like:

                        <asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="RequestId">
                            <ItemTemplate>
                                <asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Bind("RequestId") %>'></asp:HyperLink>
                            </ItemTemplate>
                        </asp:TemplateField>

Now, I place this UserControl in a page whose path is, say, http://myhost/dir1/default.aspx.

In the codebehind file of the user control, I need to get the URL of the aspx page that contains the usercontrol, make some modification to it, and assign that to the NavigateURL of the Hyperlink in the template field.

The NavigateURL will be something like:
http://myhost/dir1/update.aspx?id=<%# Bind("Id") %>'>

any suggestions?
0
Comment
Question by:IUAATech
  • 2
  • 2
4 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 18764231
One way is to use RowDatabound event.
Another way is to use something like this:

http://myhost/<%=this.Page.Request.FilePath%>?id=<%# Bind("Id") %>'>
I'm not sure for the FilePath property but if it is not check the Request's properties

Regards
Martin
0
 

Author Comment

by:IUAATech
ID: 18764292
can you show me how to do this using RowDataBound event?

and FIlePath doesn't quite work. I need everything in the URL except the file name since default.aspx needs to be replaced with update.aspx. Any way to do string manipulation?
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 250 total points
ID: 18764409
Write in the html code like this

<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Bind("RequestId") %>' NavigateURL='http://myhost/dir1'></asp:HyperLink>

in your RowDatabound event do this:


void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {
       
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      //You must replace objec here with the type of the object that you are binding to the grid view
      Object item = e.Row.DataItem;

      HyperLink hl = (HyperLink)e.Row.FindControl("HyperLink1");
       hl.NavigateUrl += System.IO.Path.GetFileName(this.Page.Request.Url.AbsoluteUri) +
                    "?id=" + Bind("Id");
      //also you must get the Id from the item and not as string
    }

Martin
0
 

Author Comment

by:IUAATech
ID: 18765706
got it. thanks.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question