I have ASP.NET GridView in my web app and would like to bind two fields into one column and use string formatting. I am providing example below, is it possible to implement into GridView?
I have two data fields Name and DefaultParam and would like to display these fields into one GridView column.
Like this
Name=DefaultParam
If DefaultParam value is empty I would like to show only Name value and do not include =
I was using Repeater and code below to achieve this but now decided to move data display to GridView
<%#Eval("Name")%>
<%# (!string.IsNullOrEmpty(Eval("DefaultParam").ToString())) ? "= " + Eval("DefaultParam"):"" %>
You can use a TemplateField and put your logic in there:
<asp:TemplateField HeaderText="Header">
<ItemTemplate>
<%#Eval("Name") + (!string.IsNullOrEmpty(Eval("DefaultParam").ToString())) ? "= " + Eval("DefaultParam"):""%>
</ItemTemplate>
</asp:TemplateField>
Another option would be to use a Property on your object to do that logic for you behind the scenes and just use that as a BoundField, but you didn't mention what the object is your binding is.
You can simple write server-side code between <%# ... %> as you write in code behind. just put it in '' (between single quotes) .
<asp:Lable id="lblxx" runat="server"
Text='<%# Eval("Name") + (!string.IsNullOrEmpty(Convert.ToString(Eval("DefaultParam")))) ? "= " + Eval("DefaultParam"):"" %>' />
Follow this tutorial link to know that how to Custom Formatting Based Upon Data using Template Fields.
Related
this is definitely an easy question but I still don't know what exactly it is for. Can anybody tell me what ImageUrl='<%# Eval("FileName") %>' means? I still don't get the idea why we need to include %#.
<%# Eval("FileName") %> is used in the context of binding data from a collection to a control. Probably the value for the imageurl is coming from a property of an object in the collection
For example, List<Photo> where Photo has a property of FileName. If you are binding that to a gridview, a repeater, etc, you'll access that property for each item in the collection when binding to such controls
Asp.net data binding overview
in this Line...
ImageUrl='<%# Eval("FileName") %>'
ImageURL the attribute of your asp:ImageButton control that is used to specify the Url of the Image File to be Used
Code between '<% and %>' tags are writtent to be Executed on the Server
'#' is used to specify that the result of server side execution will be bound hear
Eval KeyWord is Used To Evaluate the perticular Column Value (that you specify ("--hear--")) from The DataSourse
When you are using a Template Control like Repeater , GridView , etc. you are actually iterating in a list of data records, and <%# Eval("FileName") %> here means give me the value of the column named FileName.
Here we have used the Eval function which is used for one way databinding. FileName is the field name you are associating. Anything that's written inside <%# %> is parsed by asp.net engine before generating the webpage source which is pure client side script and html tags.
So Eval function is executed at server end by ASP.net engine.
Please please help!! I've been searching this for hours but maybe the solution is so obvious I'm completely overlooking it :(
I have a listview that binds to a sql table. The table contains a bunch of fields that hold only 'T' or NULL values, except for the last two fields which hold free-type text for user comments.
For the T/NULL values - The listview is configured so that an empty cell will not display, and where there is a 'T' value, it will instead show a custom text and create a line break for the next item. This syntax does exactly that:
<asp:Label ID="LymeLabel" runat="server" Text='<%# Eval("Lyme","Lyme Disease<br />") %>' />
However, I want the last two fields (the free-type text) do display in a paragraph style, with paragraph tags surrounding the Eval statement. The tags have to form part of the Eval so that they won't render whitespace if the cells are empty. My thinking was to do it this way but it doesn't work - the whitespace shows up either way:
<asp:Label ID="OtherCommentLabel" runat="server" Text='<%# "<p>"+Eval("OtherComment")+"</p>" %>' />
I'm guessing the " "+ ... +" " doesn't care what's in or not in the middle.. but how to fix it? There must be a very simple solution to this and I feel really stupid even asking this. Any advice please?
From the description, you don't need the asp:Label at all. Why not just wrap the Eval() in a P tag?
<p><%# Eval("Lyme","Lyme Disease") %></p>
Even if the label worked, you wouldn't want the resulting markup (paragraph inside a label makes no sense).
To handle the show/hide if the item is empty, you could do something like:
<ItemTemplate>
<p runat="server" visible='<%#!string.IsNullOrEmpty(Eval("OtherComment"))%>'>
<%# Eval("Lyme","Lyme Disease") %>
</p>
</ItemTemplate>
The white space will show up because p is not part of the Eval; <p>Eval()</p>.
Not sure if elegant, but you could wrap the Eval inside a method:
public string ParagraphIfData(string input)
{
if(!string.IsNullOrEmpty(input))
return "<p>" + input + "</p>";
return "";
}
Then:
<%# ParagraphIfData(Eval("Lyme","Lyme Disease")) %>
I have a requirement to display row number in grid view. What is the best way to display the row number using BoundField or TemplateField?
Note: This need to be done using markup only (without code behind).
Note: When sorting happens, the row number should not be in sequence, the first row should go down with its content.
I have already referred the following:
http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/2eead3e3-5cc2-40f7-a91c-8f7942d5329c/
<asp:TemplateField HeaderText="#" >
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
By the way, this solution proposed in article you referred. Why you don't like it and ask here?
The best place to do this would be to use the templatefield
<asp:TemplateField HeaderText="Row Number">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
An examples http://www.devcurry.com/2010/01/add-row-number-to-gridview.html
The BoundField displays the value of specified DataSource field as text.
The TemplateField allows to mix html or make use of web controls.
Please refer to the following explanation to confirm the difference.
http://forums.asp.net/t/1804988.aspx/1
The gridview is rendered as html table. If you don't want to calculate the row number in code behind, you should use JQuery.
var rowCount = $('#myTable tr').length;
And you should fill the table footer with rowCount value.
I have a gridview where i want to pass two different ID when i select a row to a new page.
I need them to be two different variables so i dont want to bind them together.
Currently, the way i am doing it with link select the values of SUBMITTEDJOBAPPLICATION & the
JOBAPPLICATIONID variables are the same(which is incorrect). What i mean is the SUBMITTEDJOBAPPLICATION is correct but the
JOBAPPLICATIONID is the same as SUBMITTEDJOBAPPLICATION and that is incorrect.
<asp:HyperLink ID="lnkSelect" runat='server' NavigateUrl='<%# String.Format("~/UnscreenedApplicantPage.aspx?SubmittedID={0}&jobID={0}", Eval("SUBMITTEDJOBAPPLICATION"), Eval("JOBAPPLICATIONID")) %>'>Select</asp:HyperLink>
How can i remedy this?
change SubmittedID={0}&jobID={0}
to
SubmittedID={0}&jobID={1}
in String.Format("SubmittedID={0}&jobID={0}", Eval("SUBMITTEDJOBAPPLICATION"), Eval("JOBAPPLICATIONID")) you only use zero index so fist parameter will replace both places.
You should try this
<asp:HyperLink ID="lnkSelect" runat='server' NavigateUrl='<%# String.Format("~/UnscreenedApplicantPage.aspx?SubmittedID={0}&jobID={1}", Eval("SUBMITTEDJOBAPPLICATION").ToString(), Eval("JOBAPPLICATIONID").ToString()) %>'>Select</asp:HyperLink>
You need Eval("SUBMITTEDJOBAPPLICATION").ToString(), Eval("JOBAPPLICATIONID").ToString() as well.
I declared my template as follows
<EditItemTemplate>
<asp:DropDownList ID="ddlYear" runat="server" DataSource='<%#GetYears() %>' DataTextField="year" DataValueField="year"></asp:DropDownList>
</EditItemTemplate>
I need to bind the data from the function i used GetYears() i don't know how to function it can any one help me
I need the data for example name to be loaded in dropdown when i click on Edit of gridview is it the correct way or is there any best way to do this
in the code behind you can find dropdown using findcontrol method on the click of edit link and can bind easily.
You could create an objectDatasource on the page and then set the datasourceid to that objectDataSource. Then you can create your POCO class
Look at this link for more info on objectdatasource
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/objectdatasource.aspx