I have created a new project in asp.net and i'm struggling to insert a hyperlink inside a ternary operator.
Here is code snippet:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="50" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:TemplateField HeaderText="Status" ItemStyle-Width="100">
<ItemTemplate>
<a href='<%# Eval("Status", "Questions/{0}.aspx") %>'>'<%# Eval("Status") %>'</a>
<%# Eval("Status").ToString() == "A" ? "Absent" : "Present" %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In this above code, if the Status is "A", i need to insert the hyperlink (<a href='<%# Eval("Status", "Questions/{0}.aspx") %>'>'<%# Eval("Status") %>'</a>) in the place of "Absent".
Here is my Output:
How can i do this? Any advice would be healpful. Thank you.
Update #1:
I need only the absent to be the link, for present, i'm just displaying it as a plain text.
Try this:
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='Questions/Absent.aspx'
Text='<%# Eval("Status") %>' Visible='<%# Convert.ToBoolean(Eval("Status").ToString() == "A" ? "True" : "False")) %>'>
</asp:HyperLink>
<asp:Label ID="Label1" runat="server" Text='Present' Visible='<%# Convert.ToBoolean((Eval("Status").ToString() == "A" ? "False" : "True")) %>'>
</asp:Label>
</ItemTemplate>
Related
I am currently working on a project for shopping cart. My program will display a grid view after I have retrieved from the database.
I hit an error when I want to retrieve from Quantity with a text box and a total value of the price.
I tried to use eval but there is an error stating the server tag is not well formed.
Anyone can help me ?
Thanks in advanced.
<asp:GridView ID="gv_Cart" runat="server" EmptyDataText="There is nothing in your shopping cart." CssClass="table table-striped table-bordered" ShowFooter="True" GridLines="Vertical" CellPadding="4" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField HeaderText="ID" DataField="productID" />
<asp:BoundField HeaderText="Name" DataField="productName" />
<asp:BoundField HeaderText="Model" DataField="Model" />
<asp:BoundField HeaderText="Price (each)" DataFormatString="{0:c}" DataField="Price"/>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="PurchaseQuantity" Width="40" runat="server" Text="<%# Eval("Quantity") %>"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item Total">
<ItemTemplate>
<% String.Format("{0:c}", ((Convert.ToInt16(<%# Eval("Quantity") %>) * Convert.ToInt16(<%# Eval("price") %>) )) %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remove Item">
<ItemTemplate>
<asp:CheckBox id="Remove" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here the code behind:
protected void Page_Load(object sender, EventArgs e)
{
string a = Session["customer_Username"].ToString();
List<cart> allCart = new List<cart>();
allCart = cBLL.getUsername(a);
gv_Cart.DataSource = allCart;
gv_Cart.DataBind();
}
I'm not 100% sure where you are saying the error is.. but this looks wrong..
<asp:TemplateField HeaderText="Item Total">
<ItemTemplate>
<% String.Format("{0:c}", ((Convert.ToInt16(<%# Eval("Quantity") %>) * Convert.ToInt16(<%# Eval("price") %>) )) %>
</ItemTemplate>
</asp:TemplateField>
Should be something like this I would imagine..
<asp:TemplateField HeaderText="Item Total">
<ItemTemplate>
<asp:Label id="lblOne" runant="server" text="<% String.Format("{0:c}", ((Convert.ToInt16(<%# Eval("Quantity") %>) * Convert.ToInt16(<%# Eval("price") %>) )) %>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Try below code
<asp:TemplateField HeaderText="Item Total">
<ItemTemplate>
<%# String.Format("{0:c}", Convert.ToInt16(Eval("Quantity")) * Convert.ToInt16(Eval("price")) %>
</ItemTemplate>
</asp:TemplateField>
Tag was indeed not well formed. Here is a better version:
<%# String.Format("{0:c}", Convert.ToInt16(Eval("Quantity")) * Convert.ToInt16(Eval("price")) %>
Note that it uses just section of <% %>, there is no inner server tags here. These tags are not supposed to be nested.
Update. As for the Quantity field you just need to correct your quotes. Use single quotes for attribute value and double quotes for Eval:
Text='<%# Eval("Quantity") %>'
I have a gridview with next columns
<asp:TemplateColumn ItemStyle-Width="10%" HeaderText="Active">
<ItemTemplate>
<asp:RadioButton ID="chkActive" runat="server" OnCheckedChanged="fncCheckear"
AutoPostBack="true" Checked='<%# DataBinder.Eval(Container.DataItem, "Active") %>'>
</asp:RadioButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn >
<ItemTemplate>
<asp:ImageButton ID="image" runat="server" CommandName="cmdLaunch" ImageUrl="../../images/arrow.gif" >
</asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>
Both columns run ok. But I need that the second one will be visible only if the first column has property Checked=true. Active has tYpefield bit, and it has values 1 or 0.
I try next with the second column, but doesnt work:
<asp:TemplateColumn >
<ItemTemplate>
<asp:ImageButton ID="image" runat="server" CommandName="cmdLaunch" ImageUrl="../../images/arrow.gif"
Visible='<%# DataBinder.Eval(Container.DataItem, "Active") %>' >
</asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>
Any ideas? Thank you.
Try
Visible='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "Active")) %>'
[SOLVED], used Ondrej Svejdar's answer.
I'm using :
<asp:TemplateField HeaderText="Created By" SortExpression="User.Firstname">
<ItemTemplate>
<asp:Label ID="User" runat="server" Text='<%#Bind("User.Firstname")'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Which shows on my form like this: Created By Firstname.
But I want to make it look like this : Created By Firstname Lastname. How do I attach another string inside the "SortExpression" and "Text" ?
Like this:
<asp:TemplateField HeaderText="Created By" SortExpression="User.Firstname,User.Lastname">
<ItemTemplate>
<asp:Label ID="lblUser" runat="server" Text='<%# string.Format("{0} {1}", Eval("User.Firstname"), Eval("User.Lastname")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Beware that sorting by multiple columns, may not always give you what you want:
http://forums.asp.net/t/1236912.aspx/1
Can you not use
<asp:TemplateField HeaderText="Created By" SortExpression="User.Firstname, User.Lastname">
<ItemTemplate>
<asp:Label ID="User" runat="server" Text='<%#Bind("User.Firstname") <%#Bind("User.Lastname")'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
try this
<asp:TemplateField HeaderText="Created By" SortExpression="User.Firstname,User.Lastname">
<ItemTemplate>
<asp:Label ID="User" runat="server" Text='<%#(Eval("User.Firstname").ToString()+ " " + Eval("User.Lastname").ToString() %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
I have some problems inserting a const "1" into a textbox which is gridview.
The gridview code:
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" EnableViewState="False">
<Columns>
<asp:BoundField DataField="Price" HeaderText="Price" ItemStyle-CssClass="price" >
<ItemStyle CssClass="price"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="ProductID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ProductName">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Summary">
<ItemTemplate>
<asp:Label ID="lblSum" runat="server" Text='<%# Eval("Summary") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="picPath">
<ItemTemplate>
<asp:Label ID="lblPic" runat="server" Text='<%# Eval("picPath") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "quantity">
<ItemTemplate>
<asp:TextBox ID="lblquantity" runat="server" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
All the information is populated from the session of the previous page, beside this textbox which doesn't comes from anywhere, it's a quantity textbox which the user should enter. And I want it to have a default value of "1".
I don't actually know how to insert into a textbox which is in the gridview.
Please help me.
Thanks
This may be because code is also checking for Header and footer template..
just put a null check before settings value...
TextBox tb = (TextBox)e.Row.FindControl("lblquantity");
if(tb!=null)
tb.Text = Convert.ToString(123);
This will surely work...
<asp:TemplateField HeaderText = "quantity">
<ItemTemplate>
<asp:TextBox ID="lblquantity" runat="server" Text='<%# Eval("quantity") == DBNull.Value ? "1" : Eval("quantity").ToString()' ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
if quantity value is null in the table, then Text property will be default 1. Otherwise it will be quantity column in the table.
You can put chis code in your GridView's RowDataBound event...
TextBox tb = (TextBox)e.Row.FindControl("lblquantity");
tb.Text = Convert.ToString(123);
I hope this helps...
Im working on the front end development for a website built in .net, This is the first time I've done this and I'm having trouble trying to alter a table.
The code which outputs my table is...
<asp:GridView ID="GV_Concepts" runat="server" AutoGenerateColumns="False" DataKeyNames="ConCatID"
BorderStyle="None" GridLines="None" ShowHeader="False" BorderWidth="0px" CssClass="DashBoard_Concepts">
<Columns>
<asp:TemplateField HeaderText="Catalog">
<ItemTemplate>
<asp:Label ID="LB_Cata" runat="server" Text='<%# Bind("ConCatalog") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="col-b" />
</asp:TemplateField>
<asp:TemplateField HeaderText=" Concept Version" ItemStyle-Width="" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<div class="conceptstd">
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" BorderStyle="None"
DataSource='<%# Bind("DS_Version")%>'>
<ItemTemplate>
<asp:HyperLink ID="HL_ConcLoc" runat="Server" CssClass="linkage" NavigateUrl='<%# Bind("FileName") %>'
Target="_blank" Text='<%# Bind("Ver") %>'></asp:HyperLink>
<asp:LinkButton ID="LB_remove" runat="server" CommandArgument='<%# Bind("ConceptID") %>'
OnClick="LB_removecon_Click" CssClass="link-btn">Remove</asp:LinkButton>
<asp:LinkButton ID="LB_sign" runat="server" CommandArgument='<%# Bind("ConceptID") %>'
OnClick="LB_signcon_Click" CssClass="sign-off-btn" Visible='<%# SignedCheck(DataBinder.Eval(Container.DataItem,"SignOff")) ?true:false %>'>Sign Off</asp:LinkButton>
<asp:Literal ID="Lit_SignedCon" Visible='<%# SignedCheck(DataBinder.Eval(Container.DataItem,"SignOff")) ?false:true %>'
runat="server"><b>Signed Off</b></asp:Literal>
</ItemTemplate>
</asp:DataList>
</div>
</ItemTemplate>
<HeaderStyle CssClass="col-c" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
Currently no concepts
</EmptyDataTemplate>
</asp:GridView>
The html equivalent of this is something similar too...
<table>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
However I need 5 columns not 2, I've tried reading up on the syntax but thought I'd ask here also, Thanks for any help.
I'm not exactly sure how your dataset is structured, but do you need that datalist in there or can you just put the controls in their own ItemTemplate in the GridView? This would give you 5 columns:
<asp:GridView ID="GV_Concepts" runat="server" AutoGenerateColumns="False" DataKeyNames="ConCatID"
BorderStyle="None" GridLines="None" ShowHeader="False" BorderWidth="0px" CssClass="DashBoard_Concepts">
<Columns>
<asp:TemplateField HeaderText="Catalog">
<ItemTemplate>
<asp:Label ID="LB_Cata" runat="server" Text='<%# Bind("ConCatalog") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HL_ConcLoc" runat="Server" CssClass="linkage" NavigateUrl='<%# Bind("FileName") %>'
Target="_blank" Text='<%# Bind("Ver") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LB_remove" runat="server" CommandArgument='<%# Bind("ConceptID") %>'
OnClick="LB_removecon_Click" CssClass="link-btn">Remove</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LB_sign" runat="server" CommandArgument='<%# Bind("ConceptID") %>'
OnClick="LB_signcon_Click" CssClass="sign-off-btn" Visible='<%# SignedCheck(DataBinder.Eval(Container.DataItem,"SignOff")) ?true:false %>'>Sign Off</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Literal ID="Lit_SignedCon" Visible='<%# SignedCheck(DataBinder.Eval(Container.DataItem,"SignOff")) ?false:true %>'
runat="server"><b>Signed Off</b></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
Currently no concepts
</EmptyDataTemplate>
</asp:GridView>
Of course, this ruins your binding to your datasource, but I'm not sure if I can accurately help you with fixing that :(
the number of column is automatically generated according to asp:templatefield
when you have two templatefield, the only two column will be generated, and the number
of rows depend on data.
so if you need 5 columns you have to put 5 templatefield inside the girdiview
Why do you "need" 5 columns? For layout? Perhaps a GridView isn't the right solution given what you're trying to accomplish. Could a Repeater that generates your content accomplish the same thing?