Solved

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

Posted on 2008-06-25
8
3,219 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

624 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