web_ohh
asked on
How to URL Encode value in <asp:HyperLinkField> field? Trying to format value in DataNavigateUrlFormatString
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_s ynonym" HeaderText="Synonym" ItemStyle-HorizontalAlign= "Center" ItemStyle-BackColor=#E9F1D 9/>
<asp:BoundField DataField="language" HeaderText="language" ItemStyle-HorizontalAlign= "Center" ItemStyle-BackColor=#E9F1D 9/>
<asp:TemplateField HeaderText="Remove" ItemStyle-HorizontalAlign= "Center" ItemStyle-BackColor="#E9F1 D9">
<ItemTemplate>
<a href='adminsynonym.aspx?ai d=1&synony m=&action= remove'>Re move</a>
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField Text="Edit" HeaderText="Editing" ItemStyle-HorizontalAlign= "Center" DataNavigateUrlFields="pra ctice_area _id, practice_area_synonym" DataNavigateUrlFormatStrin g="adminsy nonym.aspx ?aid={0}&s ynonym={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="pra ctice_area _id, practice_area_synonym" DataNavigateUrlFormatStrin g="adminsy nonym.aspx ?aid={0}&s ynonym={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!
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
<Columns>
<asp:BoundField DataField="practice_area_s
<asp:BoundField DataField="language" HeaderText="language" ItemStyle-HorizontalAlign=
<asp:TemplateField HeaderText="Remove" ItemStyle-HorizontalAlign=
<ItemTemplate>
<a href='adminsynonym.aspx?ai
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField Text="Edit" HeaderText="Editing" ItemStyle-HorizontalAlign=
</Columns>
<EditRowStyle Font-Bold="False" Font-Italic="False" />
</asp:GridView>
The field in question is:
<asp:HyperLinkField Text="Edit" HeaderText="Editing" ItemStyle-HorizontalAlign=
{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!
ASKER
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,
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,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
Maybe something like this in Your code-behind:
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
ASKER
Currently using C# in the code behind...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Small errors in the code - but generally useful/accepted
Good link to MSDN article
Good link to MSDN article
ASKER
Thanks everyone
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.