Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Programmatically Set NavigateURL in a GridView

Posted on 2007-03-21
4
Medium Priority
?
2,050 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

670 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