Solved

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

Posted on 2008-06-25
8
3,185 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
[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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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
 

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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
Multi-Column Repeater 3 39
Display info from DB to Label in asp.net 7 44
Call to SQL server times out 5 55
ASP.NET VB code for check and insert in a table 4 45
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.…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

738 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