Prevent HTML encoding in auto-generated GridView columns - asp.net

I have a GridView bound to a DataTable that I construct. Most columns in the table contain the raw HTML for a hypelinklink, and I would like that HTML to render as a link in the browser, but the GridView is automatically encoding the HTML, so it renders as markup.
How can I avoid this without explicitly adding HyperLink, or any other, columns?

Simply set the BoundColumn.HtmlEncode property to false:
<asp:BoundField DataField="HtmlLink" HtmlEncode="false" />
I am afraid that there is no easy way to disable HTML encoding of the contents in a GridView with AutoGenerateColumns= true. However, I can think of two workarounds that might solve the problem you are facing:
Option 1: Inherit the GridView class, override the Render method, loop through all cells, decode their contents, before executing the base method:
for (int i = 0; i < Rows.Count; i++)
{
for (int j = 0; j < Rows[i].Cells.Count; j++)
{
string encoded = Rows[i].Cells[j].Text;
Rows[i].Cells[j].Text = Context.Server.HtmlDecode(encoded);
}
}
Option 2: In a class inheriting from GridView or in the Page or Control using it, make your own inspection of the DataTable and create an explicit BoundColumn for each column:
foreach (DataColumn column in dataTable.Columns)
{
GridViewColumn boundColumn = new BoundColumn
{
DataSource = column.ColumnName,
HeaderText = column.ColumnName,
HtmlEncode = false
};
gridView.Columns.Add(boundColumn);
}

I was able to achieve this by using the solution that Jørn Schou-Rode provided, I modified a little bit to make it work from the RowDataBound Event of my Gridview.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int j = 0; j < e.Row.Cells.Count; j++)
{
string encoded = e.Row.Cells[j].Text;
e.Row.Cells[j].Text = Context.Server.HtmlDecode(encoded);
}
}
}

Another way is to add something like the following to the RowDataBound event handler...
If e.Row.RowType = DataControlRowType.Header Then
For Each col As TableCell In e.Row.Cells
Dim encoded As String = col.Text
col.Text = Context.Server.HtmlDecode(encoded)
Next
End If

Use OnRowCreated
protected void gvFm_RowCreated(object sender, GridViewRowEventArgs e)
{
foreach (TableCell cell in e.Row.Cells)
{
BoundField fldRef = (BoundField)((DataControlFieldCell)cell).ContainingField;
switch (fldRef.DataField)
{
case "ColToHide":
fldRef.Visible = false;
break;
case "ColWithoutEncode":
fldRef.HtmlEncode = false;
break;
}
}
}

Well since the html for the link is in your db already, you could just output the html to a literal control.
<asp:TemplateField HeaderText="myLink" SortExpression="myLink">
<ItemTemplate>
<asp:Literal ID="litHyperLink" runat="server" Text='<%# Bind("myLink", "{0}") %>' />
</ItemTemplate>
</asp:TemplateField>
This should render your link as raw text allowing the browser to render it as the link you expect it to be.

Since all the answers seem to be in C# and the questions was not specific, I ran into this issue using ASP.Net and VB.Net and the accepted solution did not work for me in VB (though I imagine it does work in C#). Hopefully this helps anyone working with VB.Net in ASP who stumbles upon this as I have.
In VB.Net BoundColumn cannot be added to Gridview.Columns as it is not a System.Web.UI.WebControls.DataControlField so instead one must use a BoundField which is a DataControlField.
BoundColoumn also does not have an HtmlEncode property however BoundField does. Also, in VB.Net DataSource becomes DataField.
For Each dataCol As DataColumn In dv.Table.Columns
Dim boundCol As New BoundField With {
.DataField = dataCol.ColumnName,
.HeaderText = dataCol.ColumnName,
.HtmlEncode = False
}
gvResult.Columns.Add(boundCol)
Next
gvResult.DataSource = dv
gvResult.Databind()
Also note that you must explicitly set AutoGenerateColumns="False" or the GridView will still generate columns along with the columns added above.

You can use this code in RowDataBound event if you want to disable HTML encoding in all rows and columns.
protected void GV_Product_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell ObjTC in e.Row.Cells)
{
string decodedText = HttpUtility.HtmlDecode(ObjTC.Text);
ObjTC.Text = decodedText;
}
}
}

Related

How to add tooltip for Commandfield ButtonType as image

I have to add tooltip for my gridview edit image button. I coded as Commandfield.
<asp:CommandField ButtonType="Image"
HeaderStyle-HorizontalAlign="center"
EditText="Edit" UpdateText="Edit"
ShowEditButton="True"
ItemStyle-VerticalAlign="Top"
EditImageUrl="~/Images/edit.png"
UpdateImageUrl ="~/Images/Save.png" CancelImageUrl ="~/Images/cancel.png"
ItemStyle-Width="15px" ControlStyle-Width="15px">
</asp:CommandField>
Is it possible to add a tooltip in gridview commandfield button type as image?
Following code works well:
protected void gvResourceEditor_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[4].ToolTip = "Edit Resource Details";
if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState.ToString() == "Alternate, Edit")
{
int i = 0;
foreach (TableCell cell in e.Row.Cells)
{
if (e.Row.Cells.GetCellIndex(cell) == 4)
{
((System.Web.UI.WebControls.ImageButton)(e.Row.Cells[4].Controls[0])).ToolTip = "Update Resource Details";
((System.Web.UI.LiteralControl)(e.Row.Cells[4].Controls[1])).Text = " ";
((System.Web.UI.WebControls.ImageButton)(e.Row.Cells[4].Controls[2])).ToolTip = "Close Resource Details";
}
i++;
}
}
}
}
catch (Exception _e)
{
}
}
or you can
define the EditText property of your gridview's commandfield (as you did), which will render as alt atribute of the <input type='image' alt='Edit' /> tag:
<asp:CommandField ButtonType="Image" EditText="Edit" etc />
then add a script tag to set the title attribute with the following code:
VB:
Protected Sub gvResourceEditor_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvResourceEditor.RowDataBound
Dim strScript as String = "$('#" + gvResourceEditor.ClientID + " input[type=image]').each(function(key, el) {el.title=el.alt;});"
Page.ClientScript.RegisterStartupScript(Me.GetType, "SetEditButtonTitle", strScript, True)
End Sub
CS:
protected void gvResourceEditor_RowDataBound(object sender, GridViewRowEventArgs e) {
string strScript = "$('#" + gvResourceEditor.ClientID + " input[type=image]').each(function(key, el) {el.title=el.alt;});"
Page.ClientScript.RegisterStartupScript(this.GetType(), "SetEditButtonTitle", strScript, true);
}
Of course, you may need to taylor the javascript to your grid. This script will set the title attribute of all input tag with type=image.
I didn't try solutions suggested by Sekaran and Show and took a little different approach as I was not willing to rely on JavaScript; thought it might be worth sharing.
A Show's mentioned in his solution that EditText property of Edit button will be rendered as alt attribute of image; this means it should also be stored as AlternateText of the .NET counterpart.
In my case; I have Edit, Update, Delete and Cancel buttons in my CommandField and I wanted to do same for all buttons.
So the (localized) text is in AlternateText property of buttons; I am looping through each buttons and put this value in ToolTip property.
Here is my code.
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (Control ctrl in e.Row.Cells[2].Controls)
{
if (ctrl.GetType().BaseType == typeof(ImageButton))
((ImageButton)ctrl).ToolTip = ((ImageButton)ctrl).AlternateText;
}
}
}
I had hardcoded Cell Id "2" in my code as it was well known.
Every Image Button control is of System.Web.UI.WebControls.DataControlImageButton type which is not accessible. Therefore I had used it's BaseType which should be ImageButton
The RegisterStartupScript part of #show's answer does not neeed to run during RowDataBound. There it will run for each row doing exactly the same thing during the grid databind process. You can drop those two lines into the OnPreRender event for the page and it will have the same effect but only happens once during code execution on the server.

How do I change the image on a <asp:buttonField type="Image" /> in code behind?

I have a <asp:GridView > with a <asp:ButtonField ButtonType="Image"> as one of the columns.
Here's the problem: I have to dynamically change the image of this ButtonField during the gridView_RowDataBound(...) event based on the data found in that particular gridview row.
The real question is, is how to access that particular ButtonField inside the gridView_RowDataBound(...) event so I can change its image in C# code?
I can't use
Image imgCtrl = (Image)args.Row.FindControl("ctrlID");
because the <asp:ButtonField> won't allow an ID to be set (get a parser error when I try to run the webPage). And I can't use
args.Row.Cells[0].Controls[0];
because the zeroth index of the .Controls[0] doesn't exist (I get a boundry overflow error).
There's got to be a simple, slick, easy way to do this!
Quick Example :
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
TableCell tableCell = e.Row.Cells[3]; // Column 3 in the grid have the Image Button
foreach (var control in tableCell.Controls)
{
if (control.GetType() == typeof(System.Web.UI.WebControls.ImageButton)) ;
{
ImageButton iButton = control as ImageButton;
iButton.ImageUrl = "/Logo.jpg";
}
}
}
}

GridView and Html Encoding

I've recently upgraded a client's web site to .NET 4 and we've found out during the process that now GridView column values are automatically HTML encoded.
They have wide use of HTML strings in their code so we must turn that off. I know one solution would go over each column and add HtmlEncode="false". My question is - is there a way to set this to be the default for all GridView columns in this application?
Thanks!
I found this solution to solve this problem.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
string encoded = e.Row.Cells[i].Text;
e.Row.Cells[i].Text = Context.Server.HtmlDecode(encoded);
}
}
}
I don't think there is any way to do it by default as this was put in as a safety measure by default so that developers would need to consider turning it off.
To get around it you would need to turn it off column by column or you could inherit a new control from GridView and make it set each column be default to false. You could then just do a search and replace for GridView with your new control. I wouldn't recommend this method though.
Best would be to interrogate each column in the application and turn it off. It's safer and it makes you actually consider where you want to open the door for the possibility of HTML / javascript injection. Better safe than sorry.
You can also create a class that extends GridView to do this
[ToolboxData("<{0}:DecodedGridView runat='server'>")]
public class DecodedGridView : GridView
{
protected override void Render(HtmlTextWriter writer)
{
for (var i = 0; i < Rows.Count; i++)
{
for (var j = 0; j < Rows[i].Cells.Count; j++)
{
if (Rows[i].RowType == DataControlRowType.DataRow
&& !(((DataControlFieldCell)Rows[i].Cells[j]).ContainingField is CommandField))
{
var encoded = Rows[i].Cells[j].Text;
Rows[i].Cells[j].Text = Context.Server.HtmlDecode(encoded);
}
}
}
base.Render(writer);
}
}
You can then just change the GridViews to this where you want to have HTML encode removed.
Just declare the Assembly in a similar fashion:
<%# Register TagPrefix="MyUI" Namespace="MyProject.UI" Assembly="MyProject" %>
Then call the GridView like so:
<MyUI:DecodedGridView ID="MyTableWithHtml" runat="server">
<!-- All the normal GridView stuff -->
</MyUI:DecodedGridView>

radio button list in repeater control

I have a repeater control in my page. I need to have a radio button in all the rows(Item template) on checking an radio button the remaining radio buttons must unchecked.
How to do this?
Thanks in advance,
Tor
Unfortunately, it's a known bug ( http://support.microsoft.com/kb/316495 ) that the GroupName property doesn't work as expected when used in a Repeater. The problem is that the Repeater implements the INamingContainer interface which requires all nested controls to have a unique name when rendered out to HTML. This causes the radio buttons to break because in order for them to work properly they must have identical names.
There are 2 work-arounds that I've come across:
1 - The first is a client-side javascript solution. It was provided by Microsoft support. Or an easier to read version here.
The instructions are as follows. Include the following javascript in the HEAD:
function SetUniqueRadioButton(nameregex, current)
{
re = new RegExp(nameregex);
for(i = 0; i < document.forms[0].elements.length; i++)
{
elm = document.forms[0].elements[i]
if (elm.type == 'radio')
{
if (re.test(elm.name))
{
elm.checked = false;
}
}
}
current.checked = true;
}
Now the function needs to be linked to the Radio Buttons in the OnDataItemBound event of the repeater. Replace "RadioButton" with the name of your RadioButton control and "RadioGroup" with the GroupName you have chosen:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
RadioButton rb = (RadioButton) e.Item.FindControl("RadioButton");
string script = "SetUniqueRadioButton('Repeater1.*RadioGroup',this)";
rb.Attributes.Add("onclick", script);
}
2 - The second solution is a server-side solution using a custom usercontrol that inherits from RadioButton. The tutorial and source code can be downloaded here: http://www.codeproject.com/KB/webforms/How_group_RButtons.aspx
Just add a OnCheckedChanged event to the radio button, loop all the radiobuttons in the repeater to uncheck them. You can use UpatePanel if you do not want postback.
.aspx
<asp:Repeater ID="Repeater1" runat="server" >
<ItemTemplate>
<asp:RadioButton ID="RadioButton1" runat="server" OnCheckedChanged="RadioButton1_OnCheckedChanged" AutoPostBack="true" />
</ItemTemplate>
</asp:Repeater>
.cs
protected void RadioButton1_OnCheckedChanged(object sender, EventArgs e)
{
foreach (RepeaterItem item in Repeater1.Items)
{
RadioButton rbtn = (RadioButton)item.FindControl("RadioButton1");
rbtn.Checked = false;
}
((RadioButton)sender).Checked = true;
}
I know this is old, but the reason for the buttons not working is because the name attribute is being overwritten. If using jQuery, you could assign the radio button to a class then set the name attribute to override the new name.
$('.MyRadioClass').attr("name","MyFixedName");
Another simpler alternative, where no fancy formatting is required, is to use a RadioButtonList:
<asp:RadioButtonList ... />

How to programmatically create and use a list of checkboxes from ASP.NET?

I have a page with a table of stuff and I need to allow the user to select rows to process. I've figured out how to add a column of check boxes to the table but I can't seem to figure out how to test if they are checked when the form is submitted. If they were static elements, I'd be able to just check do this.theCheckBox but they are programaticly generated.
Also I'm not very happy with how I'm attaching my data to them (by stuffing it in there ID property).
I'm not sure if it's relevant but I'm looking at a bit of a catch-22 as I need to known which of the checkboxes that were created last time around were checked before I can re-run the code that created them.
Edit:
I've found an almost solution. By setting the AutoPostBack property and the CheckedChanged event:
checkbox.AutoPostBack = false;
checkbox.CheckedChanged += new EventHandler(checkbox_CheckedChanged);
I can get code to be called on a post back for any check box that has changed. However this has two problems:
The call back is processed after (or during, I'm not sure) Page_Load where I need to use this information
The call back is not called for check boxes that were checked when the page loaded and still are.
Edit 2:
What I ended up doing was tagging all my ID's with a know prefix and stuffing this at the top of Form_Load:
foreach (string v in this.Request.Form.AllKeys)
{
if (v.StartsWith(Prefix))
{
var data = v.Substring(Prefix.Length);
}
}
everything else seems to run to late.
I'm going to assume you're using a DataList but this should work with and Control that can be templated. I'm also going to assume you're using DataBinding.
Code Front:
<asp:DataList ID="List" OnItemDataBound="List_ItemDataBound" runat="server">
<ItemTemplate>
<asp:CheckBox ID="DeleteMe" runat="server"/>
<a href="<%# DataBinder.Eval(Container, "DataItem.Url")%>" target="_blank">
<%# DataBinder.Eval(Container, "DataItem.Title")%></a>
</ItemTemplate>
</asp:DataList>
<asp:Button ID="DeleteListItem" runat="server" OnClick="DeleteListItem_Click" ></asp:Button>
Code Behind:
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadList();
}
protected void DeleteListItem_Click(object sender, EventArgs e)
{
foreach (DataListItem li in List.Items)
{
CheckBox delMe = (CheckBox)li.FindControl("DeleteMe");
if (delMe != null && delMe.Checked)
//Do Something
}
}
LoadList();
}
protected void LoadList()
{
DataTable dt = //Something...
List.DataSource = dt;
List.DataBind();
}
protected void List_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
string id = DataBinder.Eval(e.Item.DataItem, "ID").ToString();
CheckBox delMe = (CheckBox)e.Item.FindControl("DeleteMe");
if (delMe != null)
delMe.Attributes.Add("value", id);
}
}
}
First, make sure that each Checkbox has an ID and that it's got the 'runat="server"' in the tag.
then use the FindControl() function to find it.
For example, if you're looping through all rows in a GridView..
foreach(GridViewRow r in Gridview1.Rows)
{
object cb = r.FindControl("MyCheckBoxId");
if(r != null)
{
CheckBox chk = (CheckBox)cb;
bool IsChecked = chk.Checked;
}
}
Postback data is restored between the InitComplete event and the PreLoad event. If your checkboxes are not created until later then the checkboxes will play "catch up" with their events and the data will be loaded into the control shortly after it is created.
If this is to late for you then you will have to do something like what you are already doing. That is you will have to access the post data before it is given to the control.
If you can save the UniqueId of each CheckBox that you create then can directly access the post data without having to given them a special prefix. You could do this by creating a list of strings which you save the ids in as you generate them and then saving them in the view state. Of course that requires the view state to be enabled and takes up more space in the viewstate.
foreach (string uniqueId in UniqueIds)
{
bool data = Convert.ToBoolean(Request.Form[uniqueId]);
//...
}
Your post is a little vague. It would help to see how you're adding controls to the table. Is it an ASP:Table or a regular HTML table (presumably with a runat="server" attribute since you've successfully added items to it)?
If you intend to let the user make a bunch of selections, then hit a "Submit" button, whereupon you'll process each row based on which row is checked, then you should not be handling the CheckChanged event. Otherwise, as you've noticed, you'll be causing a postback each time and it won't process any of the other checkboxes. So when you create the CheckBox do not set the eventhandler so it doesn't cause a postback.
In your submit button's eventhandler you would loop through each table row, cell, then determine whether the cell's children control contained a checkbox.
I would suggest not using a table. From what you're describing perhaps a GridView or DataList is a better option.
EDIT: here's a simple example to demonstrate. You should be able to get this working in a new project to test out.
Markup
<form id="form1" runat="server">
<div>
<table id="tbl" runat="server"></table>
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click" />
</div>
</form>
Code-behind
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
var row = new HtmlTableRow();
var cell = new HtmlTableCell();
cell.InnerText = "Row: " + i.ToString();
row.Cells.Add(cell);
cell = new HtmlTableCell();
CheckBox chk = new CheckBox() { ID = "chk" + i.ToString() };
cell.Controls.Add(chk);
row.Cells.Add(cell);
tbl.Rows.Add(row);
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
foreach (HtmlTableRow row in tbl.Rows)
{
foreach (HtmlTableCell cell in row.Cells)
{
foreach (Control c in cell.Controls)
{
if (c is CheckBox)
{
// do your processing here
CheckBox chk = c as CheckBox;
if (chk.Checked)
{
Response.Write(chk.ID + " was checked <br />");
}
}
}
}
}
}
What about using the CheckBoxList control? I have no Visual Studio open now, but as far as I remember it is a DataBound control, providing DataSource and DataBind() where you can provide a list at runtime. When the page does a postback you can traverse the list by calling something like myCheckBoxList.Items and check whether the current item is selected by calling ListItem.Selected method. This should work.
Add them in an override of the CreateChildControls method of the Page. Be sure to give them an ID! This way they get added to the control tree at the correct time.
IMHO The best way would be to use DataBound Templated Control though, i.e. something like a ListView (in .NET 3.5). then in pageload after postback traverse all items in the databound control and use item.FindControl to get at the actual checkbox.
What I ended up doing was tagging all my ID's with a know prefix and stuffing this at the top of Form_Load:
foreach (string v in this.Request.Form.AllKeys)
{
if (v.StartsWith(Prefix))
{
var data = v.Substring(Prefix.Length);
}
}
everything else seems to run to late.

Resources