Solved

Programmatically Set NavigateURL in a GridView

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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 …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

740 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