Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-06-25
8
Medium Priority
?
3,261 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:Jason Evans
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:
Jason Evans earned 500 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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 500 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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

715 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