ASP.NET Grid view Data filed Column Formatting Not working - c#

I have a problem with the GridView column formatting.
I have a PhoneNumber that is currently coming from database as (###)###-#####
but I want the format to be ############ without spacing and brackets. I have tried every thing like DataFormatString="{0:###-####}" or than converting it into TemplateField and giving it format but not working.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="Phone Number">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("PhoneNumber") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("PhoneNumber", "{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is before converting the column to TemplateField:
<asp:BoundField DataField="PhoneNumber" HeaderText="Phone Number Home"
SortExpression="Phone" DataFormatString="{0:######-####}" HtmlEncode="false" />
But nothing is working. I still get the format of phone number like this (123) 123-4567.

If you are dynamically binding the rows in code behind, you should be formatting the columns in the DataBind event of the gridview.
The semantical column formatting works only when you use bind data semantically, for example declare

Try to replace Text property in TemplateField:
Text='<%# Bind("PhoneNumber", "{0:d}") %>'
with:
Text='<%# String.Format("{0:##########}",
Convert.ToInt64(DataBinder.Eval(Container.DataItem, "PhoneNumber")))%>'
Use "#" Custom Specifier.

Formatting is solely additive, not subtractive. To accomplish this, you would need to expose a version of the values which has non-numerics stripped out, so that your client-side formatting can act upon it.

Related

Accessing Header Text property of GridView TemplateField in the Code Behind when it is set from Header Template

I have a GridView in an asp .net application, where the Header Text of a template field is set in the Header Template of the field, as a label (where it will come from the resource file). Below is the code
<asp:GridView ID="gridView" ClientIDMode="Static" runat="server" AutoGenerateColumns="False" meta:resourcekey="grdViewResource">
<Columns>
<asp:TemplateField meta:resourcekey="TemplateFieldResource1">
<HeaderTemplate>
<asp:Label ID="lblNameHeader" Text="Name" runat="server" meta:resourcekey="lblNameHeaderResource1"/>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblNameValue" Text='<%# Eval("Name") %>'/>
</ItemTemplate>
</Columns>
</asp:GridView>
In code behind I am trying to access the HeaderText set on the column like this
var headerText = gridView.Columns[0].HeaderText;
But the value is coming Empty and I am not able to retrieve it from the gridView.Columns' HeaderTemplate property as well.
Please help me.
You have several problems with your code.
You are missing a closing </asp:TemplateField>. Your asp:Label is missing a runat="server" attribute.
If you want to use the .HeaderText property, this should be your markup:
<asp:GridView ID="gridView" ClientIDMode="Static" runat="server" AutoGenerateColumns="False" meta:resourcekey="grdViewResource">
<Columns>
<asp:TemplateField meta:resourcekey="TemplateFieldResource1" HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblNameValue" Text='<%# Eval("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
If you want to use a HeaderTemplate with custom markup in it, then you need to cast the column to a TemplateField, then access the controls within it.

can I have a return value from Eval() method in a databound Gridview?

Please refer to the code below. The template field is a part of a gridview. I have a requirement where I want to pass the string from Boundfield "TriggerEvent" to a method "Alert()" that should do some operation on the string and display it back in the grid. I have encountered error in this which is explainable. How do I achieve this functionality?
<asp:TemplateField HeaderText="TriggerEvent" SortExpression="TriggerEvent" ItemStyle- Wrap="false">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Alert(Eval("TriggerEvent")) %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
As Eval is returning object and Alert is expecting string, you can cast is with as:
Text='<%# Alert(Eval("TriggerEvent") as string) %>'

ASP.NET C# BoundField format (Remove leading zeros)

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;
}

How to wrap the DropDownList in the GridView FooterTemplate?

Please help me to fix the issue with wrapping the text in asp.net GridView FooterTemplate.
I am alyready wrapping <ItemTemplate> and <EditItemTemplate> using the below command
<asp:TemplateField ItemStyle-Wrap="true"
but the same code is not working for the FooterTemplate.
Please see the below code
<asp:TemplateField ItemStyle-Width="120px" HeaderText="Bureau" ItemStyle-Wrap="true">
<ItemTemplate>
<asp:Label ID="lblBureau" runat="server" Text = '<%# Eval("Bureau_Ref_Type")%>'
></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblBureauEdit" runat="server" Visible="false" Text = '<%# Eval("Bureau_Ref_Id")%>'></asp:Label>
<asp:DropDownList ID="ddlBureauEdit" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlBureauFooter" runat="server">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
If i reduce the size in the FooterTemplate Dropdowlist items text page seems to fit in 1024 px resolution,but our client wants the same resolution with out reducing the Item text size. So i need all experts to help wrapping the items in the FooterTemplate.
Thanks
Sam
You need to use the FooterStyle to style that section. Your ItemStyle settings only apply to actual data items (not footers or headers).
You could rewrite the first section of your markup like this:
<asp:TemplateField HeaderText="Bureau">
<ItemStyle Width="120px" Wrap="true"></ItemStyle>
<FooterStyle Wrap="true"></FooterStyle>
<ItemTemplate>
...
Notice that I moved your ItemStyle settings into the <ItemStyle> settings block, as well as adding the <FooterStyle> settings block.

Javascript Breaking on < & > in a GridView edit box

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"

Categories

Resources