Solved

How to URL Encode value in <asp:HyperLinkField> field? Trying to format value in DataNavigateUrlFormatString

Posted on 2008-06-25
8
3,102 Views
Last Modified: 2013-11-26
Hey everyone,

I am using a <asp:HyperLinkField> control inside of a column set that is a part of a gridview control.

For each row in the result set, i wish to put some of the paramaters into a URL. The problem is some of them have ampersands in them, and I would like to url encode them.

How can i do this?


Here is the code:

<asp:GridView ID="GridView1" runat="server" CellPadding="1" Width="100%" AllowSorting="True" AutoGenerateColumns="false" DataSourceID="mydata">
                  <Columns>
                      <asp:BoundField DataField="practice_area_synonym" HeaderText="Synonym" ItemStyle-HorizontalAlign="Center" ItemStyle-BackColor=#E9F1D9/>
                      <asp:BoundField DataField="language" HeaderText="language" ItemStyle-HorizontalAlign="Center" ItemStyle-BackColor=#E9F1D9/>
                      <asp:TemplateField HeaderText="Remove" ItemStyle-HorizontalAlign="Center" ItemStyle-BackColor="#E9F1D9">
                          <ItemTemplate>
                            <a href='adminsynonym.aspx?aid=1&synonym=&action=remove'>Remove</a>
                          </ItemTemplate>
                      </asp:TemplateField>
                      <asp:HyperLinkField Text="Edit" HeaderText="Editing" ItemStyle-HorizontalAlign="Center" DataNavigateUrlFields="practice_area_id, practice_area_synonym" DataNavigateUrlFormatString="adminsynonym.aspx?aid={0}&synonym={1} %>&action=edit"/>
                  </Columns>
                 
                  <EditRowStyle Font-Bold="False" Font-Italic="False" />        
              </asp:GridView>


The field in question is:
<asp:HyperLinkField Text="Edit" HeaderText="Editing" ItemStyle-HorizontalAlign="Center" DataNavigateUrlFields="practice_area_id, practice_area_synonym" DataNavigateUrlFormatString="adminsynonym.aspx?aid={0}&synonym={1} %>&action=edit"/>

{1} - Should be URL encoded

The dataset is being bound to the gridview on the code behind page:

GridView1.DataSource = ds;                
GridView1.DataBind();

where ds is a loaded DataSet object with my results.

Thanks!



0
Comment
Question by:web_ohh
  • 4
  • 2
  • 2
8 Comments
 
LVL 10

Expert Comment

by:MrClyfar
ID: 21868147
Hi web_ohh.

You can use the HttpUtility.UrlEncode method to help you here:

http://msdn.microsoft.com/en-us/library/system.web.httputility.aspx

Hope this helps.
Jas.
0
 

Author Comment

by:web_ohh
ID: 21868195
How can I use this method?

Part of the problem here is that on the front end (.aspx) page, I dont have access to the values in the dataset. How can i get access to practice_area_synonym values, and still use UrlEncode?

Thanks,
0
 
LVL 10

Accepted Solution

by:
MrClyfar earned 125 total points
ID: 21868516
Hi there.

Ah yeah, forgot about that.  I think you need to look at the RowDataBound event for the GridView control.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx
http://msdn.microsoft.com/en-us/library/aa479342.aspx

This will give you access to the data that is going to be placed in the grid, thus you can then call the UrlEncode function on that data, since it will then be displayed in the grid after RowDataBound has been called.

Sorry that I don't have any personal code examples of this.

Jas.
0
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 21868859
Hi,

Maybe something like this in Your code-behind:
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

        Dim strForEnc As String = Nothing

        Dim strEncoded As String = Nothing

        For Each row In GridView1.Rows

            If e.Row.RowType = DataControlRowType.DataRow Then

                strForEnc = e.Row.Cells(3).Text.ToString

                strEncoded = HttpUtility.UrlEncode(strForEnc)

                e.Row.Cells(3).Text = strEncoded

            End If

        Next

    End Sub

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:web_ohh
ID: 21873916
Currently using C# in the code behind...
0
 
LVL 18

Assisted Solution

by:carlnorrbom
carlnorrbom earned 125 total points
ID: 21874019
Ok, my bad, I'm a VB guy...
    protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) {

        string strForEnc = null;

        string strEncoded = null;

        foreach (row in GridView1.Rows) {

            if ((e.Row.RowType == DataControlRowType.DataRow)) {

                strForEnc = e.Row.Cells[3].Text.ToString;

                strEncoded = HttpUtility.UrlEncode(strForEnc);

                e.Row.Cells[3].Text = strEncoded;

            }

        }

    }

Open in new window

0
 

Author Closing Comment

by:web_ohh
ID: 31470679
Small errors in the code - but generally useful/accepted
Good link to MSDN article
0
 

Author Comment

by:web_ohh
ID: 21877341
Thanks everyone
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

22 Experts available now in Live!

Get 1:1 Help Now