Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have make a common usercontrol (messagecontrol.ascx) which display message box. and I have place this usercontrol in my masterpage.

In my master page also a usercontrol named header.ascx which also call this usercontrol when user click on button of a header.ascx control. its working properly.

But when make a new cart.aspx page with same masterpage. and place a button and call button click same event which i write in header.ascx button its give null referece error.

code in Messagecontrol.ascx

public void Page_Load(object sender, EventArgs e)
{
lblMessage.Text = Message;
lblMessage1.Text = Message1;
if (Message != string.Empty && Message !=null)
SendErrorModelPopupExtender.Show();
}
public string Message { get; set; }
public string Message1 { get; set; }



code in Header.ascx (Here working Properly)

protected void btnCheckOut_Click(object sender, EventArgs e)
{
if (ShoppingCart.Instance.Items.Count == 0)
{
CustomMessageControls uc1 = (CustomMessageControls)this.Parent.Parent.FindControl("MessageControl1");
uc1.Message = "No Item Found in cart. Please add item in cart to checkout";
uc1.Message1 = "No Item Found";
uc1.Page_Load(uc1, EventArgs.Empty);
}
else
Response.Redirect("checkout.aspx");
}


Same code i call in cart.aspx page but its give error

code in cart.aspx

protected void btnCheckOut_Click(object sender, EventArgs e)
{
if (ShoppingCart.Instance.Items.Count == 0)
{
CustomMessageControls uc1 = (CustomMessageControls)this.FindControl("MessageControl1");
uc1.Message = "No Item Found in cart. Please add item in cart to checkout";
uc1.Message1 = "No Item Found";
uc1.Page_Load(uc1, EventArgs.Empty);
}
else
Response.Redirect("checkout.aspx");
}



When I click on cart.aspx page button it's error

I have also add the <%@ Reference VirtualPath="~/Controls/CustomMessageControls.ascx" %>

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 39: {
Line 40: CustomMessageControls uc1 = (CustomMessageControls)this.FindControl("MessageControl1");
Line 41: uc1.Message = "No Item Found in cart. Please add item in cart to checkout";
Line 42: uc1.Message1 = "No Item Found";
Line 43: uc1.Page_Load(uc1, EventArgs.Empty);


Please help me

Thanks
Ram Kumar
Posted

1 solution

I'm not sure how you placed your controls, but you may need to find controls by using Parent property as below
C#
CustomMessageControls uc1 = (CustomMessageControls)this.Parent.FindControl("MessageControl1");

or
C#
CustomMessageControls uc1 = (CustomMessageControls)this.Parent.Parent.FindControl("MessageControl1");
 
Share this answer
 
Comments
Ram Kumar(Webunitech) 5-Jul-14 12:43pm    
Dear Sir, I have tried by both type but i have found the same error . explain the sturcture of my files is below:
site.master
cart.aspx
header.ascx
commonmessage.ascx
both user control are place in site.master
and cart.aspx inherit layout from site.master page

so please give me better solutions.

Thanks for reply and suggestion.
DamithSL 5-Jul-14 12:55pm    
can you update the question with master page aspx code?
DamithSL 5-Jul-14 12:56pm    
also try like below
this.Master.FindControl("YourContentPlaceHolderID").FindControl("MessageControl1")
Ram Kumar(Webunitech) 5-Jul-14 13:52pm    
here is my master page code
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<%@ Reference VirtualPath="~/Controls/CustomMessageControls.ascx" %>
<form runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">

<div class="main-wrapper">
<!-- Header Parts Start-->
<div id="header">
<ucHeader:THeader ID="Header2" runat="server" />
</div>

<div id="content">
<p class="welcome" id="welcomeTxt" runat="server">
" Great Sale Looks on Desktops, Tablets and Mobiles."</p>

<!--Featured Product Part Start-->
<asp:ContentPlaceHolder ID="MainContent" runat="server">

<!--Featured Product Part End-->
</div>
<!--Middle Part End-->
<div class="clear">
</div>
</div>
</div>
<ucCustomMessageControls:CustomMessageControls ID="MessageControl1" runat="server" />
</form>
DamithSL 5-Jul-14 13:54pm    
try this.Master.FindControl("MessageControl1")

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