Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am using GridView in asp.net using C# in which placed an anchor in the first column in with an image so once clicked the image it goes to the other pag.
I need to validate this, if not the logged in uses has access to edit the anchor, anchor should be disabled that is click event should be happen,

I tried this way to get the property of the anchor in row databound, but this is not working.

My code as fallows

if (e.Row.RowType == DataControlRowType.DataRow)
{
//Condition for access
HtmlAnchor hreft = (HtmlAnchor)e.Row.FindControl("btnedit");
hreft .Disabled = true;



XML
<a  href='<%# Eval("Testing", "testing.aspx?ID={0}") %>' id="btnedit">
                                            <img src="Images/edit.png" alt="View" border="0px" /></a>


At debugging I am just getting the null value,

Can any one find me where I went wrong...

Thanks in advance
Posted
Updated 19-Mar-11 0:16am
v2
Comments
Dalek Dave 19-Mar-11 6:17am    
Edited for Grammar and Readability.
That's Aragon 19-Mar-11 8:17am    
I have just found one spelling mistake in the subject line. How to "Disable" NOT "Diable".

1 solution

You can assign the href attribute dynamically. Something like below.

if (e.Row.RowType == DataControlRowType.DataRow)
{
//Condition for access
HtmlAnchor hreft = (HtmlAnchor)e.Row.FindControl("btnedit");
if(IsUserLogin)
{
hreft.HRef = assign URL
}
else
{
hreft.HRef = "javascript:void(0);";
}
}



Hope it helps.
 
Share this answer
 
Comments
sathya4260 19-Mar-11 3:28am    
hi dude
when i use hreft.HRef="javascript:void(0);";

I am getting error as "Object not set to instance."

Can U provide me an solutions
That's Aragon 19-Mar-11 3:35am    
This error ("Object not set to instance.") only occurs when the object is null.Make sure that you get the HtmlAnchor before its usage. Like place a null checking condition before the if(IsUserLogin) like if(hreft!=null). Hope it helps.
sathya4260 19-Mar-11 3:46am    
every time for hreft i am getting null value only...
That's Aragon 19-Mar-11 3:57am    
oops.. you have not make it runat="server". For using any control server side, you need to apply that attribute.
sathya4260 19-Mar-11 4:28am    
yes now i am getting the value in hreft, but after loading the page, when i click on the link it just shows error Void(0) no value in it, but while debugging the value of hreft.HRef is giving me the correct url

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900