如何在页面上找到我的用户控件。 [英] how to find my user control on a page.

查看:82
本文介绍了如何在页面上找到我的用户控件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个显示消息框的公共用户控件(messagecontrol.ascx)。我已将此usercontrol放在我的母版页中。



在我的母版页中还有一个名为header.ascx的usercontrol,当用户单击header.ascx控件的按钮时,它也会调用此usercontrol。它的工作正常。



但是当制作一个带有相同主页的新cart.aspx页面时。并放置一个按钮和调用按钮单击我在header.ascx按钮中写入的同一事件,它给出了null referece错误。



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;组; }

公共字符串Message1 {get;组; }






Header.ascx中的代码(此处正常工作)



protected void btnCheckOut_Click(object sender,EventArgs e)

{

if(ShoppingCart.Instance.Items。计数== 0)

{

CustomMessageControls uc1 =(CustomMessageControls)this.Parent.Parent.FindControl(MessageControl1);

uc1 .Message =购物车中没有找到商品。请在购物车中添加商品以结帐;

uc1.Message1 =找不到商品;

uc1.Page_Load(uc1 ,EventArgs.Empty);

}

else

Response.Redirect(checkout.aspx);

}




我在cart.aspx页面中调用相同的代码,但是它给出了错误



cart.aspx中的代码



protected void btnCheckOut_Click(object sender,EventArgs e )

{

if(ShoppingCart.Instance.Items.Count == 0)

{

CustomMessageControls uc1 =(CustomMessageControls)this.FindControl(MessageControl1);

uc1.Message =购物车中找不到商品。请在购物车中添加商品以结帐;

uc1.Message1 =找不到商品;

uc1.Page_Load(uc1,EventArgs.Empty);

}

其他

Response.Redirect(checkout.aspx);

}






当我点击cart.aspx页面按钮时出现错误



我还添加了<%@ Reference VirtualPath =〜/ Controls / CustomMessageControls.ascx%>



异常详情:System.NullReferenceException :对象引用未设置为对象的实例。



源错误:





第39行:{

第40行:CustomMessageControls uc1 =(CustomMessageControls)this.FindControl(MessageControl1);

第41行:uc1.Message = 购物车中没有找到物品。请在购物车中添加商品以结帐;

第42行:uc1.Message1 =找不到商品;

第43行:uc1.Page_Load(uc1,EventArgs.Empty );





请帮帮我



谢谢

Ram Kumar

解决方案

我不确定你是如何放置控件的,但你可能需要通过使用Parent属性找到控件,如下所示

 CustomMessageControls uc1 =(CustomMessageControls) this  .Parent.FindControl(   MessageControl1); 





 CustomMessageControls uc1 =(CustomMessageControls) this  .Parent.Parent.FindControl(  MessageControl1\" ); 


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

解决方案

I'm not sure how you placed your controls, but you may need to find controls by using Parent property as below

CustomMessageControls uc1 = (CustomMessageControls)this.Parent.FindControl("MessageControl1");


or

CustomMessageControls uc1 = (CustomMessageControls)this.Parent.Parent.FindControl("MessageControl1");


这篇关于如何在页面上找到我的用户控件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆