Solved

Programmatically Set NavigateURL in a GridView

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

626 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