i have this code in an itemtemplate in a gridview:
<%# DataBinder.Eval (Container.DataItem, "DiscountAmount")%>
It's a decimal value, and it shows 20.300000000000, which is technically right, but i'd prefer to show 20.30 or 20,30, depending on the culture.
But i've never been a big fan of gridviews, and the DataBinder.Eval and Container.DataItem haven't been good friends either, and i'm lost with how to use it.
it has a special prefix (<%#) and when i type anything other then the original code it's no good, but changing <%# to <%= or <% doesn't seem to work either?
This will also work:
<%#= String.Format("{0, 0:N2}",DataBinder.Eval (Container.DataItem, "DiscountAmount"))%>
Edit: I share your discomfort with declarative databinding syntax. You can accomplish the same thing in code-behind by calling the RowDataBound event and implementing whatever changes you want to make as the data is bound to the GridViewRow.
To do this, you need to wire up the event in the markup by setting OnRowDataBound to the name of your event handler, something like this:
<asp:GridView ID="InvoiceGrid" OnRowDataBound="InvoiceGrid_RowDataBound".....>
Then you create an event handler in code behind with a signature like this:
protected void InvoiceGrid_RowDataBound(object sender, GridViewRowEventArgs e)
The first thing you do in that event handler is test which type of GridViewRow type it is:
if (e.Row.RowType == DataControlRowType.DataRow)....
Then you do whatever formatting you want to do.
For folks happy with declarative markup, this may seem burdensome. But for people who are comfortable writing code, you can do a whole lot more here in code behind.
Did you try this?
<%= String.Format("{0:0,0.00}", DataBinder.Eval (Container.DataItem, "DiscountAmount"))%>
or just
<%# DataBinder.Eval(Container.DataItem, "DiscountAmount", "{0:0,0.00}")
You can read more format options in the article String Format for Double.
There are several ways...some of them stated above and here is another one:
Text='<%# GetFormattedDiscount(Eval("DiscountAmount").ToString())%>'
GetFormattedDiscout is a function in your code-behind where you can do whatever formatting you need and return it as string:
protected void GetFormattedDiscount(string amount){
return String.Format("{0:N2}",amount);
}
Even this should work:
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#String.Format("{0:n2}",Eval("DiscountAmount")) %>'></asp:Label>
</ItemTemplate>
Related
I want to add some asp.net linkbutton controls to a literal on a c# web form application like code below:
StringBuilder sb = new StringBuilder("<ul>");
sb.Append("<li>");
sb.AppendFormat("<asp:LinkButton runat='server' class='add-row' ID='BtnAdd' OnClick='BtnAdd_Click' CommandArgument='test'>{0}</asp:LinkButton>", "Text on the link");
sb.Append("</li></ul>");
this.Literal.Text = sb.ToString();`
and also i want to fire event of click and get CommandArgument's value of that.
private void BtnAdd_Click(object sender, EventArgs e)
{
//get the value of CommandArgument
}
I tried these and the linkbutton added to the literal successfully. but the linkbutton didn't convert to <a> tag and i found that with the firebug like this and no any events couldn't fire:
I want make a tree with leafs as linkbuttons using <ul><li> tags overall.
It would be very helpful if someone could explain solution for this problem.
The correct approach here is via the WebControl api and not from the template.
Your approach may work for HTML content only.
What you need to do is handle a page lifecycle event like OnInit, get a reference to your container control (this.MyPanel), and dynamically add items to that control's children property (this.MyPanel.Children.Add(new....))
The suggested Repeater solution is not suited for tree structure data sources, works for a list though...
As suggested by Andrei using linkbuttons in this way will not work.
I have used a similar solution in the past by creating a query parameter for each a tag in the literal and redirecting to either the current page or a different page and extracting the query parameter from there.
I would however advise you to first try and see if there is not a different way to accomplish what you are trying to do as the literal solution ends up very cluttered and code heavy.
I think a Repeater would better suit your purpose.
<ul>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<li><asp:LinkButton ID="BtnAdd" runat="server" CssClass="add-row" OnClick="BtnAdd_Click">Text on the link</asp:LinkButton></li>
</ItemTemplate>
</asp:Repeater>
</ul>
And if you want to use CommandArgument you need an OnCommand, not a OnClick
<asp:LinkButton ID="BtnAdd" runat="server" CssClass="add-row" OnCommand="BtnAdd_Command" CommandArgument='<%# Eval("value") %>'>
protected void BtnAdd_Command(object sender, CommandEventArgs e)
{
string commandArgument = e.CommandArgument.ToString();
}
If you have single <asp:LinkButton ..> then you can set it's property at run time. Like
BtnAdd.Id="";
or if you multiple then you can use AddControl method below is link
How to add controls dynamically when click button in asp.net?
How to add controls dynamically to ASP.NET form?
If you have to use asp: LinkButton just for one time then do it in this way.
<asp:Literal runat="server" ID="DocLink"></asp:Literal>
<asp:LinkButton runat="server" ID="LinkButton1" OnCommand='LinkButton1_Click'></asp:LinkButton>
and in .cs file
LinkButton1.Text = CurrentListItemAbsoluteUrl;
LinkButton1.CommandArgument = CurrentListItemAbsoluteUrl;
public void LinkButton1_Click(Object sender, CommandEventArgs e)
{
e. CommandEventArgs //To get your parameter
I have constructed an ASP.NET user control "Box.ascx" wtih the following code.
<div id="divContent" runat="server" visible='<%# AllowedToView(this.Privacy) %>'>
Content
</div>
In the codebehind, "Box.ascx.cs" has the following code.
public string Privacy = string.Empty;
public bool AllowedToView(string privacy)
{
return true;
}
When I use this control in a repeater, AllowedToView() function is hit. If I use this control without a repeater, AllowedToView() function isn't called. I want to know why this weird situation happens and how can I make the control call the AllowedToView() function when used without repeater.
Details are below.
I use this control in a repeater in "Default.aspx".
<asp:Repeater ID="rpRecords" runat="server">
<ItemTemplate>
<uc1:Box ID="myBox" runat="server" RecordID = '<%# Eval("RecordID") %>' />
</ItemTemplate>
</asp:Repeater>
The repeater is databound in "Default.aspx.cs" with the following code:
DataTable dt = FillTable();
rpRecords.DataSource = dt;
rpRecords.DataBind();
I use the "Box.ascx" control in "ShowBox.aspx" with the following code.
<body>
<uc1:Box ID="myBox" runat="server" />
</body>
I give values to the user control from the codebehind with the following code.
protected void Page_Load(object sender, EventArgs e)
{
myBox.RecordID = "1";
}
As mentioned in another answer, the # means it will require databinding to be executed.
So to answer your question "How to make it run outside of the repeater" the simple answer is to call myBox.DataBind().
Your question is very similar to asp.net inline code <%# MyboolVal %>. The problem is that <%= is equal to Response.Write and outputs straight HTML, so it won't work when setting the visible property.
I don't think you need the # but instead = in the ASP tag. Pretty sure # is only for databinding events and that's why it works in a repeater because a repeater performs a databound for rendering.
Check this link: http://blogs.msdn.com/b/dancre/archive/2007/02/13/the-difference-between-lt-and-lt-in-asp-net.aspx
Im no expert on webforms but i think that your problem is that you are trying to databind that method and thats not working for you, try putting it in a <%= AllowedToView(this.Privacy) %>
In an ASP.NET project, I have a literal. In order to set the text property, I used the following code:
<asp:Literal ID="ltUserName" runat="server" Text="<%= session.UserName %>" />
But instead of value of session.UserName, literal shows <%= session.UserName %>. I feel that solution is simple but I couldn't do it. How can set the text with inline code?
The syntax =<%# ... %> is Data binding syntax used to bind values to control properties when the DataBind method is called.
You need to call DataBind - either Page.DataBind to bind all the controls on your page, or this.DataBind() to bind just the label. E.g. add the following to your Page_Load event handler:
<asp:Literal ID="ltUserName" runat="server" Text='<%# Session["UserName"]%>'></asp:Literal>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["UserName"] = "Sample";
this.DataBind();
}
}
If you actually want to print the session value in the HTML page just use
<% =Session["UserName"].ToString()%> as "<% %> will act as server tag and you cant give it inside the literal control
I mean no need of Literal Control can just use mentioned coding instead of literal.
Did you tried:
Text='<%= session.UserName %>'
Single quotes may solve it
EDIT:
Based on this thread: stackoverflow.com/a/370263/360171
I would simply replace
<asp:Literal ID="ltUserName" runat="server" Text="<%= session.UserName %>" />
by
<%= session.UserName %>
You can't mix controls (<asp:Literal />) with code blocks (<%= .. %>).
You can access the Text property from within code:
ltUserName.Text = session.UserName;
Renatos answer is correct, you should put single quotes when you are going to evaluate an expression in a property.
The same can be said with a ItemTemplate, where you have controls to databind, where you would use Text='<%=Eval("MyDataProperty")%>'.
This may be a very dumb question but I can't seem to get it working. I use at many places the following syntax for dynamically binding a property of a control in aspx file to the resource entry, e.g.
<SomeFunnyControl Text="<%$ Resources : ResClass, ResEntry %>" />
I want to do a similar thing with a class containing some constants, something like
<SomeFunnyControl Text="<%= MyConstantsClass.MyStringConstant %>" />
But this doesn't seem to work, it simply sets the text to the exact expression without evaluating it. I am using ASP.NET 3.5 btw.
I have tried the databinding approach but I get an HttpParseException saying
Databinding expressions are only
supported on objects that have a
DataBinding event.
This article: The CodeExpressionBuilder might be interesting/helpful (although written for ASP.NET 2.0).
It (seems) to enable you to write ... Text="<%$ Code: DateTime.Now %>" .... That might help, no? It is quite a bit of overhead, though.
Your code should look like this:
<asp:Label ID="lblMyStringConstant" runat="server" Text='<%# MyConstantsClass.MyStringConstant %&>'></asp:Label>
You also need to call DataBinding on that control, like this:
lblMyStringConstant.DataBind();
(It is not necessary if you are calling DataBind on entire Page or parent container of this label, because it will call DataBind for all its children)
<asp:Label ID="lbl" Text="<%# SomeText %>" runat="server" />
Then call lbl.DataBind(); or databind some container of the label.
If you have it like this it should work actually:
public static class MyConstantsClass
{
public static string MyStringConstant = "Hello World!";
}
or alternatively
public class MyConstantsClass
{
public const string MyStringConstant = "Hello World!";
}
If you declare it like
<asp:Label ID="Label1" runat="server" Text="<%= MyNamespace.MyConstantsClass.MyStringConstant %>"></asp:Label>
it won't work and the output will be "<%= MyNamespace.MyConstantsClass.MyStringConstant %>".
What you could do alternatively is to write it like this:
<asp:Label ID="lblTest" runat="server"><%= MyNamespace.MyConstantsClass.MyStringConstant %></asp:Label>
This works perfectly for me, but note you have to provide the fully qualified namespace to your class in the ASPX definition. At least otherwise it didn't work for me.
I am using databinding to iterate through a recordset returned from the database, and one of those recordsets is a comma separated list of items - I'm trying to use a second repeater to display each of those items as a hyperlink. So far this is the code that I have:
<asp:Repeater ID="myRepeater" runat="server" DataSource='<%# DataBinder.Eval(Container.DataItem, "SomeList").ToString().Trim(',') %>'>
<ItemTemplate>
<a href='http://somesite/downloadattachment.aspx?itemid=<%# Container.ItemIndex %>'><%# Container.DataItem %></a>
</ItemTemplate>
</asp:Repeater>
The trouble is that so far there are 3 reasons why this doesnt work:
I get a server tag is not well formed error unless I remove the runat="server" - why is this? (And why does it work without the runat="server"?)
Container.DataItem Evaluates to an instance of System.Data.DataRowView - how do I get the current piece of the string that I split?
More importantly, this only seems to print out 1 Container.DataItem, even when I know there is a comma in the string I've given it - any ideas?
Instead of Eval(), for non-trivial scenarios I generally cast Container.DataItem to the type I want, and then act on it from there in a type-safe way.
The "not well formed" error is caused by the single-quotes around the parameter to Trim(). If you use single quotes on the outside of your attribute definition, you can't use them inside it. In cases like yours where a databinding definition has a lot of code in it, I often create a helper method (either inside a script runat=server for for MVC views and other inline-code-friendly cases, or in code-behind for traditional web forms apps) which handles the code I want to run. By refactoring into a method, it clarifies the HTML and sidesteps the lame single/double-quote restrictions.
Regardless of where you put the code, In your case, you want to:
cast Container.DataItem to DataRowView
extract the SomeList column value using the [] operator
call String.Split() on that string to turn your CSV string it into an array of strings
use that as a data source of your inner repeater
The code should look something like this:
<asp:Repeater ID="myRepeater" runat="server"
DataSource='<%# ((System.Data.DataRowView)Container.DataItem)["SomeList"].ToString().Split(new string[] {","}, StringSplitOptions.RemoveEmptyEntries)%>'>
<ItemTemplate>
<a href='http://somesite/downloadattachment.aspx?itemid=<%# Container.ItemIndex %>'>
<%# Container.DataItem %>
</a>
</ItemTemplate>
</asp:Repeater>
Did you specify the updatecommand, deletecommand to the sqldatasource?
Even if the proper parameters haven't been supplied, the affected rows will always be 0. If it has two parameters for the update command, two parameters have to be supplied through updatecommand.
For more information on this please check this URL: http://www.itpian.com/Coding/4774-Data-binding.aspx