I am currently doing Encoding on Dropdownlist in our project.
Could you please review and provide your comments about the following approach? After the encoding on dropdown, if there are any special characters it is displaying in the form of encoded characters, please let me know is there any better approach than this.
// raising the databound event to to encoding
protected void ddlCountry_DataBound(object sender, EventArgs e)
{
if (ddlCountry.Items.Count > 0)
{
foreach (ListItem list in ddlCountry.Items)
{
list.Text = EncodeDropDownItem(list.Text);
}
}
}
//below are the methods to do encoding
//encoding with antixss's htmlencode method
private string EncodeDropDownItem(string DropdownText)
{
return Replacecharacters(AntiXss.HtmlEncode(DropdownText));
}
//below method will replace the antixsstags with normal character.
private string Replacecharacters(string value)
{
string dropdowntext;
StringBuilder sb = new StringBuilder(value);
sb.Replace("&", "&");
sb.Replace("<", "<");
sb.Replace(">", ">");
sb.Replace(""", "\"");
sb.Replace("\", "\\");
return dropdowntext = sb.ToString();
}