ASP.Net Listview Eval time format - asp.net

I can't seem to get the format right for an Eval statement in Label text in a Template on an ASP.Net Listview with VB. I did try escaping the time but that still resulted in an invalid format. The data type in the SQL Server for the bound data is time(0).
Text='<%# Eval("Time", "{0:hh:mm tt}") %>'
Has anyone run into this seemingly simply problem before and found the correct format? I did research what is posted at http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx but did not find a solution to this problem.

This one had me tearing my hair out for a while... but I found the solution:
The second colon character needs to be escaped in your format string with a backslash.
Try this, it should work:
Text='<%# Eval("Time", "{0:hh\:mm}") %>'

Try
"{0:hh.mm tt}")
Does that help?

I hope this solve your problem.
Text='<%#String.Format(System.Globalization.CultureInfo.InvariantCulture,"{0:hh:mm tt}",Eval("Time")) %>'

you can add this to your label :
DataFormatString="{0:dd/MM/yyyy}" //display date only
DataFormatString="{0:hh:mm tt}" //display time only
DataFormatString="{0:dd/MM/yyyy hh:mm tt}" //display both date and time
Text='<%# Eval("Time") %>'
also this link will help you alot for anykind of this problems
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx

Related

How do you insert a newline character within the RegularExpressionValidator ErrorMessage Field?

Consider this code below:
<asp:RegularExpressionValidator
ID="MyTestId"
ValidationGroup="MyTestGroup"
ErrorMessage="You are a silly user. You entered the wrong format for this problem. Please try again. Using this format! ELRLDKX##Z"
ValidationExpression="some unrelated regex"
runat="server"
ControlToValidate="MyTestTextbox">
</asp:RegularExpressionValidator>
Is there a way to insert an escape character of some sort into the ErrorMessage string to ensure that Please try again. Using this format! ELRLDKX##Z is on another line? I have tried, \n with no luck.
The Validator is on a web page, so you can just add html code to the text, like a <br />
ErrorMessage="You are a silly user. You entered the wrong format for this problem.<br />Please try again. Using this format! ELRLDKX##Z"

w3c validation error in asp.net

I am new in W3c validations, I am trying to fix this error but it's not happening. The error is following:
character "&" is the first character of a delimiter but occurred as data.
I am using DataList Control to bind data and here is the line where the w3c validation error occurs.
<asp:Label ID="lblDescription"
runat="server"
Text='<%#Eval("Decr") %>'>
</asp:Label>
In database, the Decr is stored and this(&) special character is also given in the description field. w3c is not validating this line.
& is a special charater for concat, you need to escape it: make them all & not &.
Here is the solution I came up with:
<asp:Label ID="lblDescription" runat="server" Text='<%# Server.HtmlEncode( (string) Eval("Decr")) %>'></asp:Label>

Too many characters in character literal?

Can you tell me please what is wrong with this code??? About to getting crazy!!!
<asp:LinkButton ID="LinkButton1" OnClick="DivAc('griddiv')" Font-Size="Smaller" runat="server" CommandName='<%# Eval("harf").ToString().ToUpper()%>'><%# Eval("harf").ToString().ToUpper() %></asp:LinkButton>
Error: Too many characters in character literal... :(
Is DivAc('griddiv') a javascript function?
Then you have to use OnClientClick instead of OnClick.
OnClick is reserved for .NET functions. With OnClientClick you generates the OnClick-attribute in HTML.
This is probably a bit confusing.
So this is what you have to do:
<asp:LinkButton ID="LinkButton1" OnClientClick="DivAc('griddiv')" Font-Size="Smaller" runat="server" CommandName='<%# Eval("harf").ToString().ToUpper()%>'><%# Eval("harf").ToString().ToUpper() %></asp:LinkButton>
The immediate issue is that you placed a string (griddiv) in character quotes (a single quote, in C#, is for a single character only). You would need to write something like OnClick="DivAc(\"griddiv\")"
BUT
OnClick is a server-side event handler that takes the name of a public or protected function that takes (object,EventArgs) and returns void. So this won't compile anyway.
Where is DivAc? In JavaScript? If so, you want OnClientClick, in which case you can leave the single and double quotes as they are.
I think that your error is here:
CommandName='<%# Eval("harf").ToString().ToUpper()%>'><%# Eval("harf").ToString().ToUpper() %></asp:LinkButton>
I think that its should be:
CommandName='<%# Eval("harf").ToString().ToUpper()%'></asp:LinkButton>

Concatenate two or more strings in inline code ASP.NET

I am trying to place a * next to the name based on a condition.
My code :
<asp:Label ID="lblOne" runat="server" Text= '<%# Eval("name") + ((Eval("StatusId").Equals(0) && Eval("assignfilename") == null) ? " *" : "") %>' > </asp:Label>
Thanks
BB
I'm not really familiar with in-line codes and your code seems to be a bit complicated.
But I also need to concatenate an Eval("record") and a text. So to answer the question on how to concatenate, ampersand worked for me.
'<%# Eval("name") & " *" %>'
hope this helps anyone.
If you're pushing the limits of what you can easily handle with inline code, you could always write a function instead. Then you can do something like:
<asp:Label ID="lblOne" runat="server" Text= '<%# EmitSomeText(Eval("name"), Eval("StatusId"), Eval("assignfilename")) %>' />
This lets you break a complex expression up into however many lines it needs to be, which can be a little less awkward. You can use a function in your CodeBehind or any other class.
If you're binding to a class that you have access to, you could add a readonly property. Then you can do something like Eval("MyNewProperty").
I use that for exposing formatting that I need to re-use. For instance, Customer.CustomerFullName might return last name first seperated be a comma (intelligently handling situations where one or the other or both are missing) plus an optional title, since maybe my customers are medical folks and some of them have PhDs and MDs.
For simple one-off scenarios the code-behind function works okay.
You may also want to consider coding them as a property in the underlying object.
For example, if the text generated is going to be used in more than one instance, you'd need to code the function with Evals several times in different forms or controls.
I would create a property on the data object, e.g. NameWithStatusStar, then your label can be bound directly to the property with the code inside Eval("NameWithStatusStar")
This is more descriptive and reusable than a series of expressions, plus it's easier to change (e.g. add another field, change the formula etc.)
You can do it like this:
Text='<%#"CustomText "+Eval("Name")%>'
Text='<%#String.Concat(Eval("UserId"), Eval("Username")) %>'
This worked for me in my project. Found it here:
Concatenate text with Eval
Text='<%# string.Concat(Eval("FirstName"), " ", Eval("LastName"))%>'
This worked for me in my project. Found it here:
Concatenate text with Eval

I need CompareValidator control to accept date in format month/day/year

I have the following template defined within DetailsView ( DetailsView is bound to object data source ):
<EditItemTemplate>
<asp:TextBox ID="txtReleaseDate" runat="server"
Text='<%# Bind("ReleaseDate", "{0:d}") %>'>
</asp:TextBox>
<asp:CompareValidator ID="valReleaseDateType" runat="server"
ControlToValidate="txtReleaseDate" Type="Date" Operator="DataTypeCheck"
Display="Dynamic" > *
</asp:CompareValidator>
</EditItemTemplate>
Assuming I enter into TextBox txtReleaseDate a date in format month/day/year, then upon clicking an Update or Insert button, a CompareValidator control complains that date format is not valid. But if I enter date in format day/month/year, then object data source throws an exception Cannot convert value of parameter 'releaseDate' from 'System.String' to 'System.DateTime', while CompareValidator doesn’t complain.
I need the two controls to accept the same date format, so:
a) Since my DB stores date in format day/month/year, the best option would be for ODS to also accept this as valid date format. Can ODS be configured that way?
b)Can CompareValidator be configured to also accept month/day/year format?
thanx
Probably you can set the CurrentUICulture of thread to en-GB format.
Try setting the web.config Globalization
I think
format is "dd/MM/yyyy"
try putting "eb-US" one.
Gud luck.

Resources