Concatenate two or more strings in inline code ASP.NET - 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

Related

RegularExpressionValidator to limit input length and allow empty strings

I'm really bad with regex and was looking at another question almost identical to this but can't get it to work as I need.
I was to use a RegularExpressionValidator that will allow any character up to 255 characters or nothing at all. I tried,
ValidateExpression="^.{255}$"
but it throws an unhanded exception when the textbox that I'm checking is empty.
I've also tried
ValidateExpression="^.{,255}$"
Thank you
Rodney
Did you try ^.{0,255}$? Not sure what exception you are getting though.
EDIT: Also, if struggling with .Net regex, Regex Hero tester is a great help. I know there are other sites, but this one is by far the best, if you ask me.
The proper expression is ^.{0,255}$.
^.{255}$ will only match if there is exactly 255 characters, while ^.{,255}$ will match the literal string "{,255}".
If there are still issues after trying that, can you tell us the exception?
If it throws an HttpRequestValidationException exception, you can use the members of the UnvalidatedRequestValues class.
Mind that "Validation succeeds if the input control is empty" (MSDN). You may add a RequiredFieldValidator field to ensure that the user does not leave a text box blank (if you use the RequiredFieldValidator control inside an UpdatePanel control, make sure that the validator control and the control it is associated with are in the same panel - MSDN):
<asp:textbox id="myTB"
runat="Server">
</asp:textbox>
// ... MAKE SURE YOU DO NOT USE TextMode="Number"!!!
<asp:RequiredFieldValidator
ID="Value1RequiredValidator"
ControlToValidate="myTB"
ErrorMessage="Please enter a number.<br />"
Display="Dynamic"
runat="server"/>
And as for regex, ^.{255}$ means match any character (except newline) exactly 255 times between string start and end. I think it makes sense to allow ^[1-9][0-9]{0,254}$ (for values like '34', '104', etc.).

How can i get the values of parameters from query string in aspx file?

Previously i passed the parameters to my page in a query string. Now, how am i supposed to get them and use them in my page ?
I tried this code and it doesn't work :
<asp:Label runat="server" ID="ShowParameter"><%# Request.QueryString["IDProduct"] %></asp:Label>
You need to use <%= Request.QueryString["IDProduct"] %>. # is used for databinding.
It also goes without saying that you should also check to make sure the query string is not null and that it actually exists. It is also generally not a good idea to directly output a query string like this for a number of reasons.

Specifying XPath node indexes for nested repeaters

Let's say I have an XML hierarchy that looks similar to this:
<Animal>
<Kingdom>
<Phylum>
<Class></Class>
<Class></Class>
</Phylum>
<Phylum>
<Class></Class>
<Class></Class>
</Phylum>
</Kingdom>
<Kingdom>
<Phylum>
<Class></Class>
<Class></Class>
</Phylum>
</Kingdom>
</Animal>
(etc.)
Likewise, I have ASP.NET code using nested repeaters, something like this:
<asp:Repeater ID="ShowKingdom" runat="server" DataSource="(SomeDataSource)">
<ItemTemplate>
<asp:TextBox ID="txtKingdom" runat="server" XPath="/*[local-name()='Animal']/*[local-name()='Kingdom'][{0}]">
<asp:Repeater ID="ShowPhylum" runat="server" OnItemDataBound="(SomeDataBinder)">
<ItemTemplate>
<asp:TextBox ID="txtKingdom" runat="server" XPath="/*[local-name()='Animal']/*[local-name()='Kingdom'][{0}]/*[local-name()='Phylum'][???]">
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
My problem: how do I specify the node index selector "[???]" for the XPath inside the nested repeater?!?
Note: my language is VB within ASP.NET.
Edit: I've tried using a different index "[{1}]" (gives me an index out-of-bounds error), a relative Xpath "[local-name()='Phylum']" (no "/*" preceding it -- does not recognize the node/path), and tinkering with the nested repeater data source (it either doesn't recognize the XPath or crashes).
Obviously, I haven't been able to get any of these to work. Do I need to consider another approach?
Edit #2: Another thing I tried that does not want to work: for the nested repeater:
DataSource="<%# XPathSelect('Phylum')%>"
I finally figured this out. I found the key here.
After doing a lot of digging, I realized that my {0} predicate index in my XPath was being replaced in the code-behind using a call to String.format.
I obtained the parent repeater index by referencing e.Item.Parent.Parent (where "e" is a RepeaterItemEventArgs object).
Once I had the indexes for both the current repeater and its parent, I was able to use {0} and {1} for my indexes, and the String.format did the rest.
Voila. Problem solved.

how to force TextBox to only accept strings

Sorry for the Dummy Question , i know :( ,, but it's only the simple things that dont work with me :((
i have many text boxes and i want the user to only insert String and not Numeric numbers ,
how could i handle it in easy way ??
as it takes every thing and apply it to the database , or should i control it from the Database
PS. i have searched a lot but with no good answer
use [a-zA-Z]+ for ValidationExpression:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="RegularExpressionValidator"
ValidationExpression="[a-zA-Z]+"></asp:RegularExpressionValidator>
You could take a look at validation techniques for asp : http://msdn.microsoft.com/en-us/library/7kh55542.aspx
This provide a set of tools to check whether the input match what you expect.
You can do it easily in AJAX,just download it from here
first add a script manager to your page then add FilteredTextBoxExtender to the textbox and set it's properties as you wish.
A Regular Expression could be applied to the input
For the basics on RegEx : http://www.regular-expressions.info/tutorial.html
And also see
http://www.regular-expressions.info/dotnet.html
You can use regex to do that with jQuery.
In this example, I replace only digits.
You can adapt the regex to replace any set of characters with an empty string.

ASP.NET: two ways to access global resource programmatically

I know that I can set a Label's text by using the following syntax.
lblMessage.Text = (string)GetGlobalResourceObject("resxFile", "message");
What are the benefits and drawbacks associated with using the below syntax?
lblMessage.Text = Resources.resxFile.message;
The second method will not work for local resource files. Is there a different syntax for local resource files?
The second way looks better because it is strongly-typed. If you changed the resource file name or the resource value name then you would get a compile error. If you needed to dynamically get a resource, then you would have to do it the first way, else use a switch statement or something similar.
If you are using asp.net 2.0 or higher there is actually a 3rd way to set a label by using markup only:
<asp:Label ID="Label1" runat="server" Text="<%$ Resources:resxFile,message %>" />
Kinda related to localization: http://quickstarts.asp.net/QuickStartv20/aspnet/doc/localization/localization.aspx

Resources