Which will be present in code-behind as a System.Web.UI.HtmlControls.HtmlAnchor object. There you can set any attribute like so:
userDetailsLink.Attributes["href"] = "Details.aspx?id="+userId;
//or HtmlAnchor has a property for HRef
userDetailsLink.HRef = "Details.aspx?id="+userId;
For more information, here's the documentation for the HtmlAnchor class:
I have come up with the following code after reading more on Google search but this code is not formatted properly. I get an error "Invalid expression term '<'" on the first line that starts with <%if(<%#
Container.
khurj01
Member
25 Points
269 Posts
Disable href based on a condition
Jan 28, 2013 10:26 PM|LINK
How do I disable the hyperlink based on user id?
Is there a way to write the same code below in code behind?
AceCorban
Star
12594 Points
2318 Posts
Re: Disable href based on a condition
Jan 28, 2013 11:15 PM|LINK
Which will be present in code-behind as a System.Web.UI.HtmlControls.HtmlAnchor object. There you can set any attribute like so:
For more information, here's the documentation for the HtmlAnchor class:
http://msdn.microsoft.com/en-us/library/3ef7c738.aspx
oned_gk
All-Star
36262 Points
7390 Posts
Re: Disable href based on a condition
Jan 29, 2013 02:07 AM|LINK
then
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { String userid = DataBinder.Eval(e.Row.DataItem, "UserID").ToString(); HyperLink hl = (HyperLink)e.Row.FindControl("Hyperlink1"); hl.NavigateUrl = "Details.aspx?id=" + userid; }Suwandi - Non Graduate Programmer
arunoyour
Participant
1115 Points
264 Posts
Re: Disable href based on a condition
Jan 29, 2013 03:07 AM|LINK
Using simple jquery
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="Scripts/jquery-1.9.0.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { $('.clk').click(function () { // alert($(this).text()); window.location = "geturl.aspx?id=" + $(this).text(); }); }); </script> </head> <body> <form id="form1" runat="server"> <div> <a href="#" class="clk">10</a> </div> </form> </body> </html>in ur case only
<a href='#'> <%# Container.DataItem["UserID"] %> </a>is enought rest jquery will handle. if u didnt provide any userid ,their will be no redirection (disable ,in a way)
Visit :: www.simplyasp.blogspot.com
Stay tune...Keep alive
khurj01
Member
25 Points
269 Posts
Re: Disable href based on a condition
Jan 29, 2013 03:15 AM|LINK
I am still not able to get what I need to get done; here is my entire aspx code.
I just want to enable or disable the link based on a user ID.
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="Details.aspx.cs" Inherits="Info._Default" %> <% @Register Namespace="DataControls" Assembly="DataCalendar" TagPrefix="dc" %> <%@ Register assembly="SLibrary" namespace="SLibrary" tagprefix="cc1" %> <asp:Content ID="Content1" runat="server" ContentPlaceHolderID="HeadContent"> </asp:Content> <asp:Content ID="Content2" runat="server" ContentPlaceHolderID="MainContent"> <h2> Employee Information </h2> <dc:DataCalendar id="DataCalendar1" runat="server" width="100%" DayField="EventDate" VisibleDate="2013-01-17" OnVisibleMonthChanged="MonthChange" SelectedDate="2013-01-17" BackColor="#999966" Font-Bold="True" Font-Size="Medium" Font-Underline="False" ForeColor="#000099"> <DayHeaderStyle ForeColor="#990000" /> <DayStyle HorizontalAlign="Center" VerticalAlign="Top" Font-Size="10" Font-Name="Arial" BackColor="#D9ECFF" /> <OtherMonthDayStyle BackColor="LightGray" ForeColor="DarkGray"/> <ItemTemplate> <br /> <a href='Details.aspx?id=<%# Container.DataItem["userid"] %>&type=1'></a> </ItemTemplate> <TitleStyle BackColor="#990000" ForeColor="Yellow" /> </dc:DataCalendar> <p> </asp:Content>khurj01
Member
25 Points
269 Posts
Re: Disable href based on a condition
Jan 29, 2013 04:06 AM|LINK
I have come up with the following code after reading more on Google search but this code is not formatted properly. I get an error "Invalid expression term '<'" on the first line that starts with <%if(<%# Container.
<ItemTemplate> <% if(<%# Container.DataItem["UserID"] %> == "Get Logged on User's ID here") { %> <a class='<%# (Container.DataItem["EmpType"].ToString() == "Perm") ? "perm" : (Container.DataItem["EmpType"].ToString() == "Cont") ? "cont"%>' href='Details.aspx?id=<%# Container.DataItem["EmpID"] %>'> <font color='<%# Container.DataItem["ItemColor"] %>' size='2'> <%# Container.DataItem["UserID"] %> </font> </a> <% } else { %> <a class='<%# (Container.DataItem["EmpType"].ToString() == "Perm") ? "perm" : (Container.DataItem["EmpType"].ToString() == "Cont") ? "cont"%>' <font color='<%# Container.DataItem["ItemColor"] %>' size='2'> <%# Container.DataItem["UserID"] %> </font> </a> <% } %> </ItemTemplate>molly_c
Participant
1590 Points
401 Posts
Re: Disable href based on a condition
Jan 31, 2013 09:22 AM|LINK
You can use Javascript to disable href based on a condition, it's easier to understand and use in Javascript, I recommand you to use that.
http://stackoverflow.com/questions/11900863/how-to-disable-the-hyperlink-based-on-the-condition-through-javascript
Molly
It's time to start living the life you are imagined.