Making only certain cells in a gridview column linkbuttons - c#

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>

Related

Manage Hyperlink base on the condition in Repeater ASP.NET C#

How to manage Hyperlinks base on AttachmentID, In Inside link button there is two hyperlink to manage, If AttachmentID is "NA" Then Hyperlink ID one should visible else Hyperlink ID two should visible. I tried lots into google like this code not able to find. I tried using ItemCommand and ItemDataBound but did not understand this concept. The main concept to do this manage target="_blank".
Below is my Repeater Code.
<asp:Repeater ID="Repeater_News1" runat="server" OnItemDataBound="Repeater_News1_ItemDataBound">
<ItemTemplate>
<asp:Image ID="Image2" runat="server" class="pull-left img-responsive" ImageUrl='<%# Bind("ImageName", "~/images/news_images/{0}") %>' />
<asp:LinkButton ID="lnkbtn_check" runat="server" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Attachmentid") %>'>
<a href='<%# DataBinder.Eval(Container.DataItem, "Attachment")%>' id="one"
target="_blank">
<%# DataBinder.Eval(Container, "DataItem.Heading")%></a>
<a href='<%# DataBinder.Eval(Container.DataItem, "Attachment")%>' id="two">
<%# DataBinder.Eval(Container, "DataItem.Heading")%></a>
</asp:LinkButton>
</h4>
<p>
<%# DataBinder.Eval(Container, "DataItem.SmallDescription")%></p>
</ItemTemplate>
</asp:Repeater>
Use this in your repeater:
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Attachment") %>' Visible='<%# Eval("AttachmentID").ToString() != "NA" %>' Text='<%# Eval("DataItem.Heading") %>' Target="_blank" />
You can set the Visibility with an if statement in the HyperLink itself: Visible='<%# Eval("AttachmentID").ToString() != "NA" %>'
UPDATE
You can also check the AttachmentID for IsNullOrEmpty and show the correct hyperlink.
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Attachment") %>' Visible='<%# string.IsNullOrEmpty(Eval("AttachmentID").ToString()) %>' Text='<%# Eval("DataItem.Heading") %>' Target="_blank" />
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# Eval("Attachment") %>' Visible='<%# !string.IsNullOrEmpty(Eval("AttachmentID").ToString()) %>' Text='<%# Eval("DataItem.Heading") %>' Target="_self" />

ASP.NET repeater get all items and checkbox if user has right

I have an ASP.NET application and I would like to display all roles and add a checkbox which is checked if the user has the right.
I use an ASP repeater to get all roles but how can I check the checkbox in the same repeater to get the user's role ?
Here is my code:
<asp:Repeater ID="RepeaterRole" runat="server" DataSourceID="ObjectDataSource2">
<ItemTemplate>
<div>
<asp:CheckBox runat="server" Checked="False" />
<asp:Label CssClass="lbl" ID="Label1" runat="server" Text='<%# Eval("RoleLabel")%>'></asp:Label>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetAllRolesToCollection" TypeName="Business.BusinessObject.Role"></asp:ObjectDataSource>
<asp:Repeater ID="RepeaterRole" runat="server" DataSourceID="ObjectDataSource2">
<ItemTemplate>
<div>
<asp:CheckBox runat="server" Checked='<%# Convert.ToBoolean(Eval("role")) %>' />
<asp:Label CssClass="lbl" ID="Label1" runat="server" Text='<%# Eval("RoleLabel")%>'></asp:Label>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetAllRolesToCollection" TypeName="Business.BusinessObject.Role"></asp:ObjectDataSource>
Make sure you get 'role' field 1 or 0 from DB
Thanks Hardik,
based on your solution, I created a similar method like this: public Dictionary GetAllRolesFromUserToDictionary() so the checkbox will bind to the boolean value
Then the ASP code is like this:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource3">
<ItemTemplate>
<div>
<asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Eval("Value")%>' />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Key")%>'></asp:Label>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource ID="ObjectDataSource3" runat="server" SelectMethod="GetAllRolesFromCurrentUserToDictionary" TypeName="BusinessObject.RoleCollection"></asp:ObjectDataSource>

Operand "+" with Bind/Eval in aspx files

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") : "") %>' />

How to make checbox checked or uncheked in gridview editmode

i have made a gridview which has many columns and the editmode , some are text and dropdown list are are working perfect .
How can i assign value to a Checkbox to make it Checked or Unchecked?
The value against which i want to do is Yes / No.
So i am having trouble doing this.
My Gridview: Just pasting portion of it as its too big.
<asp:TemplateField HeaderText="CUpdate">
<ItemTemplate >
<asp:Label ID="Label6" runat="server" Text='<%# Eval("CUpdate") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtCupdate" Text='<%# Eval("CUpdate") %>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate >
<asp:Label ID="Label6" runat="server" Text='<%# Eval("Email") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBoxList ID="checkEmail" runat="server">
<%--HOW TO MAKE THIS CHECKED OR UNCHECKED ? when i have values Yes / NO--%>
</asp:CheckBoxList>
</EditItemTemplate>
</asp:TemplateField>
Update
If i make the above as :
<EditItemTemplate>
<asp:CheckBoxList ID="checkEmail" selectedvalue='<%# Eval("Email") %>' runat="server">
<asp:ListItem Value="Yes">Yes</asp:ListItem>
</asp:CheckBoxList>
</EditItemTemplate>
In this case i do get checkbox checked when there us Yes , but in case on NO it wont run possible error as there is no value 'NO' to select in the checboxlist .Any solution.
Thank you
Your CheckBoxList needs to have all the posible values like so..
<asp:CheckBoxList ID="checkEmail" selectedvalue='<%# Eval("Email") %>' runat="server">
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
</asp:CheckBoxList>
Although i don't understand why you want to use a CheckBoxList instead of just 1 checkbox like so..
<asp:CheckBox runat="server" ID="checkEmail" Checked = '<%# Eval("Email").ToString().Equals("yes", StringComparison.OrdinalIgnoreCase) %>' />

Not able to DataBound the `Style` attribute in GridView

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") %>

Categories