Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project, I need to set Visible/Invisibel Link Button
I used the Follwoing Code::

C#
LinkButton masterControl = (LinkButton)this.Master.FindControl("lnkbPostDetails");                       
   if ( User == "Honey")
   {
      masterControl.Visible = true;
   }
   else
   {
      masterControl.Visible = false;
   }


When i am execting the above the code i m getting the follwoing error, at the line
LinkButton masterControl = (LinkButton)this.Master.FindControl("lnkbPostDetails");

NullReferenceException was Handled by user code.
Object reference not set to an instance of an object.

Thanks & regards

Honey.
Posted
Updated 26-May-12 0:13am
v3
Comments
Technoses 26-May-12 6:28am    
remove
xmlns:asp="#unknown"
from tags

this is a code project problem

 
Share this answer
 
v2
YOu can dow following process:

For Master Page
aspx page
ASP.NET
<div>
   <asp:label id="lblName" text="BBB" runat="Server" xmlns:asp="#unknown"></asp:label>
   <asp:linkbutton id="lnkbPostDetails" text="AAA" runat="Server" xmlns:asp="#unknown"></asp:linkbutton>
       <asp:contentplaceholder id="ContentPlaceHolder1" runat="server" xmlns:asp="#unknown">
       </asp:contentplaceholder>
   </div>


aspx.cs page

C#
public partial class Master_test3_MasterPage : System.Web.UI.MasterPage
{
    public LinkButton lnkbPostDetailsMS;
    protected void Page_Init(object sender, EventArgs e)
    {
        lnkbPostDetailsMS = lnkbPostDetails;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
}



Content Page

Aspx.cs page


C#
protected void Page_Load(object sender, EventArgs e)
{
    Master_test3_MasterPage ms = (Master_test3_MasterPage)this.Master;
    ms.lnkbPostDetailsMS.Visible = false;
}
 
Share this answer
 
Object reference not set to an instance of an object.
This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

Based on what you have shared, it means,
1. Either, there is no Master page for the given page. OR
2. this.Master.FindControl("lnkbPostDetails") returns NULL and when you try to cast it into LinkButton, it throws an error. Check if their is linkbutton on MasterPage as defined.
 
Share this answer
 
Comments
honey4bee 29-May-12 7:08am    
Hi Sandeep,
Link Button exists in my master page, I m pasting here Html code of link button exsisting,

<tr>
<td bgcolor="#CC9900" height="300px" valign="top">
<br />
<asp:LinkButton ID="lnkbPostDetails" runat="server" OnClick="lnkbPostDetails_Click">Post Details
</td>
<td bgcolor="#FFFFFF" valign="top" colspan="2" >
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

</div>
</td>
</tr>
Sandeep Mewara 29-May-12 7:42am    
If this is your master page HTML, linkbutton can be directly accessed. Why to use a FindControl?
honey4bee 30-May-12 1:18am    
In the Intelligency Master Page not comes, then how I can directly access link button..
Sandeep Mewara 30-May-12 7:27am    
In Masterpage.cs file, directly access lnkbPostDetails control object.

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