Better "left / substring" solution for <%# Container.DataItem %> - asp.net

This little asp.net code has some drawbacks.
If the itemrow is empty, it will fail.
If the item tends to be shorter than 10 chars, you already know.
<asp:LinkButton ID="lbnHistory" CommandName="lbnHistory"
CommandArgument="'<%# Container.DataItem %>'
Text='<%# ((string)Container.DataItem).ToString().Substring(5,10) %>'
runat="server">
</asp:LinkButton>
I'm also available to solve this with the repeatercommandevent i use for this task.
For simplification, I want it filled from a comma separated string.
The task itself is so simple and not worth creating and iterating a strong type.
What do you think?

Whops, I think I was to deep into other thoughs.
You just have to create a public method and wrap it into..
For the sake of clarify..
<%# SampleTruncing((string)Container.DataItem) %>

Related

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.

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

What is the difference between binding data in data grid view methods?

What is the difference between binding data in data grid view methods ??
<ItemTemplate>
<asp:LinkButton ID="lnkBtnUserName" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem,"UserFirstName")%>'
CommandArgument='<%# Eval("UserID") %>' OnClick="lnkBtnUserName_Click" />
</ItemTemplate>
and this second one
<asp:TemplateField HeaderText="Employee ID">
<ItemTemplate>
<asp:Label ID="lblempid" runat="server" Text='<%# Bind("EmpId.EmpId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
means in
method 1
Text='<%# DataBinder.Eval(Container.DataItem,"UserFirstName")%>'
CommandArgument='<%# Eval("UserID") %>'
method 2
Text='<%# Bind("EmpId.EmpId")
also explain use one this CommandArgument='<%# Eval("UserID") in 1st one ????
Call such as Eval("UserID") corresponds to TemplateControl.Eval method call and that itself actually translates into call such as DataBinder.Eval(GetDataItem(), "UserID"). In summary, Eval is shorthand syntax for DataBinder.Eval - that internally inspects first argument and based on its type, it will try to resolve the second argument over it - for example, for data row, it will try to resolve to a column name while for plain objects, it will use reflection to resolve to property name.
Bind is special syntax that results into bi-directional binding used when editing i.e. control will get value from data source (similar to Eval) and it will also update (modified) value back to data source. AFAIK, Bind does not correspond to a method call (as Eval) but instead ASP.NET compiler would spit the necessary code to ensure two way data binding.
See data binding expressions overview: http://msdn.microsoft.com/en-us/library/ms178366.aspx

what is the use of Eval() in asp.net

What is the use of Eval() in ASP.NET?
While binding a databound control, you can evaluate a field of the row in your data source with eval() function.
For example you can add a column to your gridview like that :
<asp:BoundField DataField="YourFieldName" />
And alternatively, this is the way with eval :
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lbl" runat="server" Text='<%# Eval("YourFieldName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
It seems a little bit complex, but it's flexible, because you can set any property of the control with the eval() function :
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# "ShowDetails.aspx?id="+Eval("Id") %>'
Text='<%# Eval("Text", "{0}") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Eval is used to bind to an UI item that is setup to be read-only (eg: a label or a read-only text box), i.e., Eval is used for one way binding - for reading from a database into a UI field.
It is generally used for late-bound data (not known from start) and usually bound to the smallest part of the data-bound control that contains a whole record. The Eval method takes the name of a data field and returns a string containing the value of that field from the current record in the data source. You can supply an optional second parameter to specify a format for the returned string. The string format parameter uses the syntax defined for the Format method of the String class.
IrishChieftain didn't really address the question, so here's my take:
eval() is supposed to be used for data that is not known at run time. Whether that be user input (dangerous) or other sources.

Resources