add text after dataGrid cell - asp.net

This is dataGrid . How to add text(string) after <%# Bind("Value") %>
for example 123.432 and i want after any record to have "$" dolar sign
<asp:TemplateField HeaderText="Стойност">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Value") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelValue" runat="server" Text='<%# Bind("Value") %>'>
</asp:Label>
</ItemTemplate>
<ControlStyle Width="100px" />
</asp:TemplateField>

<% String.Format("{0}$",Eval("Value")); %>

You can take benefit of Standard Numeric Format String
<asp:Label ID="LabelValue" runat="server"
text='<%# Bind("Value").ToString("C", CultureInfo.CurrentCulture) %>'/>
which is a standard format available with C#.
But you want to show it in the last then you can simply add it in the last.

<ItemTemplate>
<asp:Label ID="LabelValue" runat="server" Text='<%# Bind("Value") %>'>
</asp:Label>
$
</ItemTemplate>

I suggest you to use MaskedEdit extender in ajax toolkit if you already using ajax toolkit in your project. MaskedEdit extender can be use in edit template and it will handle the mask on client side you don't want to worry about value and $ sign when you read the value back.
without using ajax toolkit you can use one label for $ sign and textbox for value field on edit template. In normal template also use two labels for both value and $ sign. then it will be easy to read the values.

Related

ASP.Net GridView TemplateField Inserting HTML

I'm relatively new to ASP.Net so this may be a simple question. I'm using Visual Studio Express 2012 for Web and I have a GridView setup (which works fine) that I am trying to get HTML wrapped round one of the columns. My code at the moment:
<asp:TemplateField HeaderText="" SortExpression="teamviewer">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("teamviewer") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<a href="javascript:teamviewerclientconnect('999999999')">
<img src="Images/teamviewer_icon.png" /><asp:Label ID="Label1" runat="server" Text='<%# Bind("teamviewer") %>'></asp:Label>
</a>
</ItemTemplate>
</asp:TemplateField>
At the moment this puts my image in as the hyperlink and then outputs the SQL resulting field as text next to it. What I need to do is replace where I have put 999999999 with the actual SQL result. I don't want it to print the text at all, it should just be in the hyperlink. Thanks in advance.
Use a code-behind method to build the href string, like this:
protected string BuildHref(string clientId)
{
return "javascript:teamviewerclientconnect('" + clientId + "')";
}
<ItemTemplate>
<a href='<%# BuildHref(Eval("DATABASE_COLUMN_NAME_HERE")) %>'>
<img src="Images/teamviewer_icon.png" />
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("teamviewer") %>'>
</asp:Label>
</a>
</ItemTemplate>

How to show current date in detailview asp.net automatically

I'm using asp.net and I'm creating a detail view from a database. The database I create got a column I want set auto show current when edit or insert like using timer like vb.net code
lblClock.Text = TimeOfDay
My Sample source from asp.net :
<asp:TemplateField HeaderText="Time Out" SortExpression="TIME_OUT">
<EditItemTemplate>
<asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("TIME_OUT") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox10" runat="server" Text='<%# Bind("TIME_OUT") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Bind("TIME_OUT") %>'></asp:Label>
</ItemTemplate>
What I want is when I click the edit button it will automatically show the current time and then I just click update.
try this..........TextBox1.Text = DateTime.Now.ToString("HH:mm");
In your Page Load event add this code
lblClock.Text = DateTime.Now.ToString();

How to format a time-stamp to show the date only in a grid view

In an aspx page I am binding the labels like this:
<asp:TemplateField HeaderText="Date of Joining">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("date_of_joining") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Paid Priviledge Date">
<ItemTemplate>
<asp:Label ID="Label8" runat="server"
Text='<%# Eval("paid_priviledge_date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
And in the code behind I'm binding the grid view like this :(minimum code is given)
GridView1.DataSource = dt2;
GridView1.DataBind();
But the gridview columns show the date like this :
4/12/2011 12:00:00 AM
4/4/2011 12:00:00 AM
Please suggest how to remove the time stamp part and to display only the date part.
I know how to do this by formatting using ToString and SubString. But I am not able to do this in gridview.
You can specify format strings for the eval statement:
Eval("date_of_joining", "{0:dd/MM/yyyy}")
Create a FormatDate method in your codebehind, and call that from your gridview.
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
http://www.csharp-examples.net/string-format-datetime/
This part will go in your code behind
private object FormatDate(DateTime input)
{
return String.Format("{0:MM/dd/yy}", input);
}
And this bit will go in your markup
<asp:TemplateField HeaderText="Date of Joining">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# FormatDate(Eval("date_of_joining")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Paid Priviledge Date">
<ItemTemplate>
<asp:Label ID="Label8" runat="server"
Text='<%# FormatDate(Eval("paid_priviledge_date")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
This is what I would call a D.R.Y. approach to the problem. If you ever need to modify the formatting in any way. You can simply edit the code behind method and it will rain down sweet love to all of your markup.
Use "{0:d}" for short date format.
Try
Text='<%# Eval("paid_priviledge_date","{0:d}") %>'
and
Text='<%# Eval("date_of_joining", "{0:d}") %>'
You can use the DataFormatString in a bound field the same can be set as below:
<asp:Label ID="Label8" runat="server" Text='<%# Eval("paid_priviledge_date","{0:d}") %>'/>
Text='<%# (Convert.ToDateTime((Eval("date_of_joining")))).ToShortDateString() %>'
This is the simplest way I discovered.

<%# Bind([ReqPo!ItemId])%> doesnt work

In Dynamics ax Enterprise Portal I have created a templatefield in AxGridView. Seems everything ok, but when i try to enter some value to this textbox (manually or through lookup), it doesnt bind to ReqPo!ItemId field. Checked that with info(strfmt("%1", ReqPo.ItemId))); in validateWrite method on ReqPo dataset - it prints nothing; What i'm missing?
<asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="<%$ AxLabel:#SYS12836 %>" Visible="true">
<EditItemTemplate>
<asp:TextBox runat="server" ID="TextBoxFilterItemId" CssClass="AxInputField"
Columns="<%$ AxDataSet:ReqTransPo.ReqTrans.ReqPo!ItemId.DisplayLength %>"
Enabled="<%$ AxDataSet:ReqTransPo.ReqTrans.ReqPo!ItemId.AllowEdit %>"
MaxLength="<%$ AxDataSet:ReqTransPo.ReqTrans.ReqPo!ItemId.StringSize %>"
Text='<%# Bind("[ReqPo!ItemId]") %>'>
</asp:TextBox>
<dynamics:AxLookup
ID="AxLookup3" runat="server" OnLookup="Item_lookup" TargetControlId="TextBoxFilterItemId"
CssClass="AxLookupButtonBF" HoverCssClass="AxLookupButtonHoverBF" ShowFilter="True">
</dynamics:AxLookup>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="ItemIdLabel" runat="server" Text='<%# Bind("[ReqPo!ItemId]") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
I assume you are using the ItemId field from the table ReqPo. If you locate that field in the Data Dictionary you will discover that the field does not allow editing.
As AxGridView only supports editing, which leads me to the conclusion that you are trying the break the data integrity rules of the application.
if this is the case, it is obviously not possible.

Databinding Error when Recreating object

Figure there is a simple solution to this problem but I have been unable to find it.
I have databinding in an ASP.Net application to a GridView. This gridview is bound to an ObjectDataSource as per standard usage.
The problem I have is that one of my bound fields uses the property DataFormatString="{0:C}" and due to the currency format being displayed when an update is attempted and the object recreated I get a error as such "$13.00 is not a valid value for Decimal."
Clearly this is a result of the column using a FormatString and then attempting to bind it back to a decimal property I have in my object called UnitPrice.
I am assuming there is some markup I can set that can specify how the value is translated back?
Thanks in advance for any help.
For anyone curious the solution ended up looking like this...
<asp:TemplateField>
<HeaderTemplate>
UnitPrice
</HeaderTemplate>
<EditItemTemplate>
<asp:Label ID="lblEditItem" runat="server" Text='<%# Bind("UnitPrice", "{0:#,##0.00}") %>' Enabled="false" ></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label Runat="server" Text='<%# Bind("UnitPrice", "{0:c}") %>' ID="lblUnitPrice"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Do not include the format string in the EditItemTemplate. Just bind the raw value.
Something like this:
<asp:TemplateField SortExpression="UnitPrice" HeaderText="Unit Price">
<EditItemTemplate>
<asp:TextBox ID="editUnitPrice" Runat="server" Text='<%# Bind("UnitPrice", "{0:#,##0.00}") %>' ></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label Runat="server" Text='<%# Bind("UnitPrice", "{0:c}") %>' ID="Label1"> </asp:Label>
</ItemTemplate>
</asp:TemplateField>

Resources