?
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,298 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: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
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!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

850 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