Not able to DataBound the Style attribute
Style='<%# Eval("LeftPadding","padding-left:{0}") %>'
Full Code
<asp:TemplateField HeaderText="Report Item" SortExpression="ReportItem">
<ItemTemplate>
<asp:Label Style='<%# Eval("LeftPadding","padding-left:{0}") %>' ID="lblReportItem"
runat="server" Text='<%# Eval("Caption") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="350px" />
</asp:TemplateField>
But I can DataBound some other attributes even not a Standard HTML attribute, like below
<asp:Label StyleTemp='<%# Eval("LeftPadding","padding-left:{0}") %>' ID="lblReportItem"
runat="server" Text='<%# Eval("Caption") %>'></asp:Label>
What is the problem with Style ?
Got Answer :)
<asp:Label Style=<%# string.Format("padding-left:{0}px",Eval("LeftPadding")!=DBNull.Value? Convert.ToString(Eval("LeftPadding")): "0") %>
ID="lblReportItem" runat="server" Text='<%# Eval("Caption") %>'></asp:Label>
Tried by without giving the Single Quotes for the style attribute and used string.Format
Style=<%# string.Format("padding-left:{0}px",Eval("LeftPadding")!=DBNull.Value? Convert.ToString(Eval("LeftPadding")): "0") %>
Related
I want to make one of the data bound in my gridview uneditable or in my case, I want to make the textbox in edit mode in read only. here is what I have tried but not successful:
TextBox ProductImage = GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;
ProductImage.ReadOnly = true;
and here is the aspx code:
asp:TemplateField HeaderText="ProductImage" SortExpression="ProductImage">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("ProductImage") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ProductImage") %>' />
</ItemTemplate>
<ControlStyle Width="50px" />
</asp:TemplateField>
can someone help me out?
Have you tried setting ReadOnly to true in your aspx code?
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="true" Text='<%# Eval("ProductImage") %>'/>
</EditItemTemplate>
Or you could use a Label instead of a TextBox
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductImage") %>'/>
I am getting this ~/Data/TestCopy.pdf for my Documents in the GridView but I am trying to get only the filename without the directory from SQL database. Does anyone know how to achieve this?
Thanks!
<asp:TemplateField HeaderText="Documents " SortExpression="filePath">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("filePath") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" Target="_blank" runat="server" Text='<%# Bind("filePath") %>'
NavigateUrl='<%# Eval("filePath") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
The simplest way is, just add the Reference to System.IO and use Path class to get only the file name, little change in your code,
<asp:TemplateField HeaderText="Documents " SortExpression="filePath">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Path.GetFileName(Bind("filePath")) %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" Target="_blank" runat="server" Text='<%# Path.GetFileName(Bind("filePath")) %>'
NavigateUrl='<%# Eval("filePath") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
In Gridview RowDataBound , evaluate filename from filepath.
System.IO.Path.GetFileName(filePath).
Not sure but you can try something like this:-
Text='<%# Eval("filePath").ToString().Split(Eval("filePath").ToString().LastIndexOf("/"))[1] %>'
May be you have stored it this way ~/Data/TestCopy.pdf in the database. You can use split function to split the last '/'
I have a gridview which pulls data from a database:
I wan't to make the content within task a linkbutton that can be clicked to show a popup with additional informatiom.
When I make the column linkbuttons it is also making Total: and Subtotal: a link button:
<asp:TemplateField HeaderText="Task" ItemStyle-Width="20%">
<ItemTemplate>
<asp:LinkButton ID="taskLinkButton" runat="server"
Text='<%# Eval("Task")%>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
How do I prevent these specific rows from being link buttons?
A bit dirty but this should do the trick:
So i coulnt get to call Databinder.Eval inside <% %>, I'm not sure if it's actually possible, so I went with a different solution. The following worked for me (and its even shorter than last version):
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text='<%# Eval("task") %>' Visible='<%# Eval("incidentN") != null %>' runat="server" />
<asp:Literal Text='<%# Eval("task") %>' Visible='<%# Eval("incidentN") == null %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
You can use a PlaceHolder with conditional visibility
<asp:PlaceHolder ID="Ok" runat="server" Visible='<%# (Eval("LoadStatus").ToString()=="false"?true:false) %>'><%----%>
<asp:Label ID="Label1" Text='<%# Eval("Task")%>' runat="server" />
</asp:PlaceHolder>
<asp:PlaceHolder ID="Ko" runat="server" Visible='<%# (Eval("LoadStatus").ToString()=="false"?false:true) %>'><%----%>
<asp:LinkButton ID="taskLinkButton" runat="server"
Text='<%# Eval("Task")%>'>
</asp:LinkButton>
</asp:PlaceHolder>
I have this aspx code:
<asp:TemplateField HeaderText="Name" SortExpression="Firmierung">
<ItemTemplate>
<asp:HyperLink ID="HyperLink" runat="server" NavigateUrl='<%#Eval("Id", "DetailInfo.aspx?Id={0}") %>' Text='<%#Bind("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
I want to add an if condition so that I can use operand + with Text, something like this:
<asp:TemplateField HeaderText="Name" SortExpression="Firmierung">
<ItemTemplate>
<% if(Condition is true) { %>
<asp:HyperLink ID="HyperLink" runat="server" NavigateUrl='<%#Eval("Id", "DetailInfo.aspx?Id={0}") %>' Text='<%#Bind("Name") + ("Active") %>' />
<% } else { %>
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%#Eval("Id", "DetailInfo.aspx?Id={0}") %>' Text='<%#Bind("Name") %>' />
<% } %>
</ItemTemplate>
</asp:TemplateField>
I don't know how to use operand + in this case. Any help appreciated. Thanks!
Try this one
Text='<%# String.Format({0}{1},Eval("Name"),Eval("Active")) %>'
OR
Text='<%# String.Format({0}{1},Bind("Name"),Bind("Active")) %>'
For more help visit the link:
HyperLink with NavigateUrl with Eval(). Where is the mistake?
You can't do it with Bind. Although with Eval you can add some C# code with it, with Bind it is not possible. Eval is really a method call but Bind is just a declaration that turned by the framework to some binding code, and its format has to be Bind("FieldName") (You can add formatting though).
The Hyperlink Text property is not editable by the client so Eval should be enough and you can write it in one row.
<asp:HyperLink ID="HyperLink" runat="server"
NavigateUrl='<%#Eval("Id", "DetailInfo.aspx?Id={0}") %>'
Text='<%# Eval("Name") + (Condition ? + Eval("Active") : "") %>' />
I have a bound field in my Gridview which is getting its value from a database table.
I have got the data but don't know how to format it inside the gridview.
For example I get total data from below like "123456", but I want to display as "123,456"
<asp:BoundField DataField="totaldata" HeaderText="Total Data"
ReadOnly="True" SortExpression="totaldata" />
How can I do this? Do I need to convert the bound field into a template field ? But what do i do after that.
please help.
I have used DataFormatString="{0:n0}" and it solved the above problem.
how do i do for this:
<asp:TemplateField HeaderText="Failed Files"
SortExpression="NumFailed">
<ItemTemplate>
<asp:Image ID="Image2" runat="server" ImageUrl="~/NewFolder1/warning_16x16.gif" />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "GetFilesFailed.aspx?id="+Eval("MachineID") %>' Text='<%# Bind("NumFailedFiles") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
the hyperlink has the number which need to be formatted...
Use DataFormat property :
<asp:BoundField DataField="totaldata" HeaderText="Total Data"
ReadOnly="True" SortExpression="totaldata" DataFormatString="{0:n3}" />
EDIT : For the second part of your question use Eval method's second parameter to format your data :
<%# Eval("NumFailedFiles", "{0:n3}") %>
Then your template will be like that :
<asp:TemplateField HeaderText="Failed Files"
SortExpression="NumFailed">
<ItemTemplate>
<asp:Image ID="Image2" runat="server"
ImageUrl="~/NewFolder1/warning_16x16.gif" />
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# "GetFilesFailed.aspx?id="+Eval("MachineID") %>'
Text='<%# Eval("NumFailedFiles", "{0:n3}") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
A couple of ways in how you can do that
Option 1
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "GetFilesFailed.aspx?id="+Eval("MachineID") %>' Text='<%# Bind("NumFailedFiles","{0:n0}") %>'></asp:HyperLink>
Option 2
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "GetFilesFailed.aspx? id="+Eval("MachineID") %>' Text='<%# Bind("NumFailedFiles").ToString("N") %>'></asp:HyperLink>
Option 3
Create a method in the code behind page that returns the formatted number.
protected string GetFormatedNumber(object number)
{
if ( number != null )
{
return number.ToString("N");
}
return "0";
}
And call the method in your aspx page as below:
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "GetFilesFailed.aspx? id="+Eval("MachineID") %>' Text='<%#GetFormatedNumber(Eval("NumFailedFiles")) %>'></asp:HyperLink>
I think you need to take a look at this MSDN article on How to Format Data in the DataGridView
if you want to format your data on gridview use "{0:n3}"
<asp:Label ID="label" runat="server" Visible="true" Text='<%#DataBinder.Eval(Container.DataItem, "data","{0:n3}") %>'></asp:Label>