Solved

Programmatically Set NavigateURL in a GridView

Posted on 2007-03-21
4
2,039 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

896 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now