I've got an Asp.Net GridView inside an UpdatePanel. It all works fine, except when one of the columns includes HTML special characters like < and >. The GridView is bound to a List<Entity> and the Entity class has a property Regex which is a System.Text.RegularExpressions.Regex.
At first I had this:
<asp:TemplateField HeaderText="RegEx">
<ItemTemplate>
<asp:Label ID="RegExLabel" runat="server" Text='<%#Eval("Regex") %>'
ToolTip='<%#Eval("Regex") %>' Width="102px" CssClass="Wrap" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="RegExTextBox" runat="server" Text='<%#Eval("Regex") %>'
Width="98px" />
</EditItemTemplate>
</asp:TemplateField>
With a value of (?<capture>\d+) this displayed ?\d+ when not editing, and when editing this row I got a script error and the edit, update and cancel buttons no longer work.
Then I tried the answer in this question and had this:
<asp:TemplateField HeaderText="RegEx">
<ItemTemplate>
<asp:Label ID="RegExLabel" runat="server"
Text='<%#System.Web.HttpUtility.HtmlEncode(Eval("Regex").ToString()) %>'
ToolTip='<%#System.Web.HttpUtility.HtmlEncode(Eval("Regex").ToString()) %>'
Width="102px" CssClass="Wrap" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="RegExTextBox" runat="server"
Text='<%#System.Web.HttpUtility.HtmlEncode(Eval("Regex").ToString()) %>'
Width="98px" />
</EditItemTemplate>
</asp:TemplateField>
This is slightly better, in that the tooltip and the non-editing version display correctly, but when I start editing I see: (?<capture>\d+) with the HTML Entities displayed raw. Does anyone know a way to encode the values (to stop the script error) while still displaying them correctly without the HTML entities in their raw state when editing?
<asp:TemplateField HeaderText="RegEx">
<ItemTemplate>
<asp:Literal Mode="Encode" ID="RegExLabel" runat="server" Text='<%#Eval("Regex") %>'
ToolTip='<%#Eval("Regex") %>' Width="102px" CssClass="Wrap" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="RegExTextBox" runat="server" Text='<%#Eval("Regex") %>'
Width="98px" />
</EditItemTemplate>
</asp:TemplateField>
and in the page directive add this ValidateRequest="false"
Related
Been trying to fix this for hours and I know that the problem is with the '#' but I could not find any solution to this problem. My field name in the database is 'HRTRN#'.
<asp:TemplateField HeaderText="Transaction#">
<EditItemTemplate>
<asp:TextBox ID="TextBox13" runat="server" Text='<%# Bind("HRTRN#") %>' Width="50px" Height="17px" MaxLength="14"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("HRTRN#") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Does anyone know the solution for this to make the field name containing # symbol accessible?
You should change your code from :
Bind("HRTRN#")
To
Bind("[HRTRN#]")
Since it contains special charcters. ( you would do that also for columns with spaces).
How should DataFormatString of BoundField in Gridview look like that values won't have leading zeros?
So far I got this:
<asp:BoundField DataField="NUMBER" HeaderText="Id. number" DataFormatString="{0:d}">
Expected result:
000001 -> 1
002101 ->2101
I tried to figure that problem out with official documentation and this page. So far unsuccessful.
When you want to format something properly in a boundfield, I always suggest converting it to a templatefield. It's much easier to work with than boundfields. Here is an example of how the templatefield will look like once it's converted.
<asp:TemplateField ShowHeader="False" Visible="False">
<EditItemTemplate>
<asp:TextBox ID="tbEditNumber" runat="server" Text='<%# Bind("Number","{0:n}") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="tbNumber" runat="server" Text='<%# Bind("Number","{0:n}") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
within that template field I placed..
Text='<%# Bind("yourfield","{0:n}") %>'
This should format it into a number and should drop the leading zeros.
EDIT: You might be able to try
Text='<%# String.Format("{0:n}", Eval("Number") ) %>'
Another approach is to use String.Trim function. The following code is how I accomplished what you are trying to do:
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" Text="<%#Eval("NUMBER").ToString().TrimStart('0')%>" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
The tricky part was using the correct combination of quotes and single quotes. You can use
"
in place of the quote character around your datafield column that you are displaying.
Simplifiying A Kimmels answer, no need to place in a server label, no concern of single or double quotes, no concern of underlying datatype, as it casts the object to string, then performs a string.TrimStart, no need for codebehind
<asp:TemplateField HeaderText="Work Order">
<ItemTemplate>
<%# Eval("WorkOrder").ToString().TrimStart("0".ToCharArray()) %>
</ItemTemplate>
</asp:TemplateField>
0123456 = 123456
0012345 = 12345
if a server label is required, just start and end the text tag with single quotes
<asp:Label ID="lblWorkOrder" runat="server" Text='<%# Eval("WorkOrder").ToString().TrimStart("0".ToCharArray()) %>' />
Try this. Much cleaner this way.
Markup
<asp:TemplateField HeaderText="Id. number">
<EditItemTemplate>
<asp:TextBox ID="tbEditNumber" runat="server" Text='<%# ConvertToDigit(Eval("Number")) %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="tbNumber" runat="server" Text='<%# ConvertToDigit(Eval("Number")) %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
Code-behind
protected int ConvertToDigit(object oNum)
{
var i = 0;
if (oNum != null) int.TryParse(oNum.ToString(), out i);
return i;
}
I have a list of games. Each game has a visiting team and a home team. I would like to allow the user to choose either the visiting team or the home team. So I converted to template fields and put radio buttons in. But clearly that isn't right. I need a radio button list, I think. Because with just radio buttons, they can select both. is this possible with a gridview? most tutorials I see only have one radio button in the row, so this isn't the issue. here is my gridview:
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="id_game" DataSourceID="sqlPopulateGames">
<Columns>
<asp:BoundField DataField="id_game" HeaderText="id_game" InsertVisible="False" ReadOnly="True" SortExpression="id_game" />
<asp:TemplateField HeaderText="visitor" SortExpression="visitor">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("visitor") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<br />
<asp:RadioButton ID="RadioButton1" runat="server" Text='<%# Eval("visitor") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="home" SortExpression="home">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("home") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:RadioButton ID="RadioButton2" runat="server" Text='<%# Eval("home") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="game_time" HeaderText="game_time" SortExpression="game_time" />
</Columns>
</asp:GridView>
because with just radio buttons, they can select both.
Not if those radio buttons are grouped in the markup. Radio buttons are designed to be mutually-exclusive, but they need to know which other radio buttons to be exclusive with. The GroupName property in ASP.NET is used to set this.
Something like this:
<asp:RadioButton GroupName="SomeGroup" ID="RadioButton1" runat="server" Text='<%# Eval("visitor") %>' />
With that set, this would be mutually exclusive for any other RadioButton with the GroupName set to "SomeGroup".
Since this is happening inside a repeated control, you probably want a separate group for each iteration. So you can bind it to some value in the backing data:
<asp:RadioButton GroupName='<%# Eval("someValue") %>' ID="RadioButton1" runat="server" Text='<%# Eval("visitor") %>' />
If someValue is unique for each record in the result, then you would end up with a grouped pair of mutually exclusive radio buttons for each row.
I have a column that has an ImageButton. my database field has bit data type. I want when my record has true value in that column show True.jpg and my command become MakeFalse and when it has false value show False.jpg and my command become MakeTrue. How I can do this?Is it possible to do it with one TemplateField?
thanks
You could include two ImageButtons in a TemplateField and evaluate Visible from your bit_field
<asp:TemplateField HeaderText="YourField">
<ItemTemplate>
<asp:ImageButton runat="server" ImageUrl="True.jpg" Visible='<%# (bool)Eval("bit_field") %>' />
<asp:ImageButton runat="server" ImageUrl="False.jpg" Visible='<%# !(bool)Eval("bit_field") %>' />
</ItemTemplate>
</asp:TemplateField>
I'm not sure how you'd want your Command to tie in.
This is the remaining part of the above Brissles code
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Select" Text='<%#(bool)Eval("bit_field")? "Make False":"Make True" %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
My DataGrid contain totally 32 columns.I need to display my first 4 column as default column.
For the remaining Column i need to view using scroll bar.(not Page scroll need scroll bar inside my grid view)
When I've had to do this, what I did was create 2 Gridviews next to each other
The first Gridview should have only the freezable columns. The second will have the rest, and will be in a scrollable div tag.
I had defined amount of of rows, so it wasn't a problem for me, but if you scroll down on one grid, the rows won't match the second grid.
Here's another solution using CSS..but I haven't tried it:
http://www.codeproject.com/KB/webforms/FreezePaneDatagrid.aspx
If you use third party controls like the Telerik RadGrid, they usually have built in properties to control Freezing Columns.
Try this
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField HeaderText="Column 1" />
<asp:BoundField HeaderText="Column 2" />
<asp:BoundField HeaderText="Column 3" />
<asp:BoundField HeaderText="Column 4" />
<asp:TemplateField>
<ItemTemplate>
<div style="overflow: scroll; width: 400px;">
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Column 5") %>'></asp:Label>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("Column 6") %>'></asp:Label>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("Column 7") %>'></asp:Label>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("Column 8") %>'></asp:Label>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("Column 9") %>'></asp:Label>
....
<asp:Label ID="Label7" runat="server" Text='<%# Eval("Column 32") %>'></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>