How to use Substring in Bind text - asp.net

I am binding a Gridview with some data and I have taken labels in template field and I am binding the data to that label's text property,
<asp:TemplateField HeaderText="Proceedings" SortExpression="PROCEEDINGS" ItemStyle-Width="60px">
<ItemTemplate>
<asp:Label ID="lblProceedings" runat="server" CssClass="Label_Value" Text='<%# Bind("PROCEEDINGS") %>' ToolTip='<%# Bind("PROCEEDINGS") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Wrap="true" HorizontalAlign="Left" />
I want the length of the Bound text till 10 characters how to achieve this

Try this:
<asp:Label ID="lblProceedings" runat="server" CssClass="Label_Value" Text='<%# Eval("PROCEEDINGS") == null ? "empty" : Eval("PROCEEDINGS").ToString().Substring(0,10)%>'
or check this link Substring in a label
I hope that help.

10Label has no MaxLangth property, you can use here textbox and give MaxLength="10", and style it as a lable.
EDIT:
You can use substring like this:
<asp:label id="lDesc" runat ="server" text ='<%# (Eval("Description") .Length>=10) ? Eval("Description").SubString(0,10) :Eval("Description") %>'></asp:Label>

You can use like this
Text='<%# Eval("PROCEEDINGS").ToString().Substring(0,9) %>'
Or you can write a function in .cs file like this.
protected getString(string str)
{
return (str..Substring(0,9));
}
And use this in gridview label as
Text='<%# getString(Eval("PROCEEDINGS").ToString()) %>'

Related

How to set ItemTemplate text value to combobox DataTextField instead of DataValueField

I have a telerik radgrid with a GridTemplateColumn as seen below in a C# ASP.NET 4.0 webform.
As you can see, in the EditItemTemplate I am using a RadComboBox with an id number for DataValueField and a human readable text for DataTextField.
My question is, how can I change the Text in my ItemTemplate to show the human readable value instead of the Id? The value Alias1 comes from the grid datasource and it is the Id.
Thank you for any help you can provide!
<telerik:GridTemplateColumn UniqueName="Alias1" Display="true" DataField="Alias1" HeaderText="Alias1" SortExpression="Alias1">
<ItemTemplate>
<asp:Label ID="lblField30" CssClass="text" runat="server" Text='<%# Bind("Alias1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadComboBox ID="RadComboBox16" runat="server" Skin="Outlook" Height="150" DataSourceID="SqlDataSourceAliasOptions" DataTextField="aliasText" DataValueField="aliasid" SelectedValue='<%#Bind("Alias1") %>'>
</telerik:RadComboBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
You need to change ItemTemplate binding like this:
<ItemTemplate>
<asp:Label ID="lblField30" CssClass="text" runat="server" Text='<%# Eval("aliasText") %>'></asp:Label>
</ItemTemplate>
Of course your binded entity must have "aliasText" property. If you are binding something like DataTable make sure that contains "aliasText" column.

DataBinding: 'System.Data.DataRowView' does not contain a property with the name

I'm adding data using footer template and I have button to add. So when I try to add
error 'System.Data.DataRowView' does not contain a property with the name '
It shows error in this line:
<asp:TemplateField ItemStyle-Width = "30px" HeaderText ="Name">
<FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblMembershipName" runat="server" width ="150px"
Text='<%# Eval"MembershipName")%>'> </asp:Label**>
</ItemTemplate>
</FooterTemplate>
</asp:TemplateField>
You should change: Text='<%# Eval"MembershipName")%>' to Text='<%# Eval("MembershipName")%>'
You where missing the opening ( of the Eval function.
The problem looks to be in your binding statement assuming your code is as you posted and not a copy typo:
<asp:Label ID="lblMembershipName" runat="server" width ="150px"
Text='<%# Eval"MembershipName")%>'> </asp:Label**>
Your missing a bracket as it should be:
<asp:Label ID="lblMembershipName" runat="server" Width="150px"
Text='<%# Eval("MembershipName") %>' />
Also verify that what you are binding to has a field called MembershipName.
You've ommited a (, it should be:
Eval("MembershipName") instead of Eval"MembershipName").
And instead of </asp:Label**> you should write </asp:Label>.
Sounds like you are trying to databind data in the footer - ensure you have nothing that looks like this in the footer section - posting the code would help loads.
text = '<%#....
Ross

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.

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>

Gridview : Hyperlink and description in the same column cell

Apologies for the newbie question. My client wishes me to make a small change to the gridview on his http://www.flogitdonegal.com/SearchPage.aspx page.
Note the way the Title column is a hyperlink to view more information. This comes from a 'BriefDescription' field in the database.
How can I add 250 chars from the 'FullDescription' underneath the Title in the same cell, but I dont want it be a hyperlink.
Essentially it will be 2 fields coming into the same column.
Thanks in advance for all help.
John
If this is using a GridView you are most likely using a TemplateField as it is to display the HyperLink.
Within the ItemTemplate of the TemplateField you can specify an additional Label underneath using something as follows:
<asp:Label runat="server" id="FullDescLabel" Text='<%# DataBinder.Eval(Container.DataItem, "FullDescription") %>' />
You need to use the TemplateField and here is a tutorial that explains some of the other fields that GridView offers as well.
<asp:GridView ID="gvwCompounds" runat="server" DataSourceID="objItemsFromYourDB">
<Columns>
....
<asp:TemplateField>
<ItemTemplate HeaderText="Title">
<asp:HyperLink runat="server" ID="Hperlink1" NavigateUrl='<%# Eval("BriefDescriptionUrl") %>' Text='<%# Eval("BriefDescription") %>' />
<br />
<asp:Label runat="server" ID="Label1" Text='<%# Eval("FullDescription") %>' />
</ItemTemplate>
</asp:TemplateField>
....
</Columns>
</asp:GridView>

Resources