Sunday, March 11, 2012

Web User Control with AJAX

hello.

youdon't need to use findcontrol to get a reference to the label. just use the label id to get a reference to that control.


ToLuis Abreu , if you do it like this:

<%@. Control Language="VB" ClassName="Budget" %>

<script runat="server">
Public Property MonthName() As String
Get
Return lblMonth.Text
End Get

Set(ByVal value As String)
lblMonth.Text = value
End Set
End Property
</script>

<asp:UpdatePanel ID="upPanelBudget" runat="server">
<ContentTemplate>
<table border="1" cellpadding="1" cellspacing="1">
<tr>
<td>
<asp:Label ID="lblMonth" runat="server" Text="MMM" Font-Bold="true" BackColor="Black"
ForeColor="white" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>

It fire an error:

Object reference not set to an instance of an object.

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:

Line 8: Line 9: Set(ByVal value As String)Line 10: lblMonth.Text = valueLine 11: End SetLine 12: End Property
It must be some thing about LifeCycle of ASPX Page that involved updatepanel. I answer a question about this before, but i cann't find out it now :(
If put lblMonth out of the updatepanel, it works well, no errors:
 <%@. Control Language="VB" ClassName="Budget" %>

<script runat="server">
Public Property MonthName() As String
Get
Return lblMonth.Text
End Get

Set(ByVal value As String)
lblMonth.Text = value
End Set
End Property
</script>

<asp:UpdatePanel ID="upPanelBudget" runat="server">
<ContentTemplate>
<table border="1" cellpadding="1" cellspacing="1">
<tr>
<td>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Label ID="lblMonth" runat="server" Text="MMM" Font-Bold="true" BackColor="Black"
ForeColor="white" />


hello.

well, i think that you should always call EnsureChildControls from the properties that use internal controls.

anyway, i've built a simple page and did not get that exception. can you post here the page you're using to get the exception? thanks.


The workaround:

<%@. Page Language="VB" %>

<%@. Register src="http://pics.10026.com/?src=Budget.ascx" TagName="Budget" TagPrefix="CBA" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
CType(ucFeb.BudgetupPanel.ContentTemplateContainer.FindControl("lblMonth"), Label).Text = "AAA"
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<CBA:Budget ID="ucFeb" runat="server" />
</div>
</form>
</body>
</html>

Budget.ascx:

<%@. Control Language="VB" ClassName="Budget" %>

<script runat="server">
Public ReadOnly Property BudgetupPanel() As UpdatePanel
Get
Return upPanelBudget
End Get
End Property
</script>

<asp:UpdatePanel ID="upPanelBudget" runat="server">
<ContentTemplate>
<table border="1" cellpadding="1" cellspacing="1">
<tr>
<td>
<asp:Label ID="lblMonth" runat="server" Text="MMM" Font-Bold="true" BackColor="Black"
ForeColor="white" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>


Hi,

Test with the following usercontorl,when you use the MonthName property in the aspx page,that erroe fires:

<%@. Control Language="VB" ClassName="Budget" %>

<script runat="server">
Public Property MonthName() As String
Get
Return lblMonth.Text
End Get

Set(ByVal value As String)
lblMonth.Text = value
End Set
End Property
</script>

<asp:UpdatePanel ID="upPanelBudget" runat="server">
<ContentTemplate>
<table border="1" cellpadding="1" cellspacing="1">
<tr>
<td>
<asp:Label ID="lblMonth" runat="server" Text="MMM" Font-Bold="true" BackColor="Black"
ForeColor="white" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>


hello.

well, not sure on when/how you're acessing the property. as i've said, i've built a really simple page + user control. here's the code i'm using (i0m getting a full postback, but i believe the problem was not that):

<%@. Control Language="C#" AutoEventWireup="true" CodeFile="ctl.ascx.cs" Inherits="ctl" %>
<asp:updatepanel runat="server" id="panel">
<ContentTemplate>
<asp:Label runat="server" ID="lbl" />
</contenttemplate>
</asp:updatepanel>

code:

public String LabelText
{
get
{
EnsureChildControls();
return lbl.Text;
}
set
{
EnsureChildControls();
lbl.Text = value;
}
}

simple page:

<%@. Page Language="C#" %>

<%@. Register src="http://pics.10026.com/?src=ctl.ascx" tagname="ctl" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

void Click(Object sender, EventArgs e)
{
//ctl1.LabelText = DateTime.Now.ToString();
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
ctl1.LabelText = DateTime.Now.ToString();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="manager" />
<uc1:ctl ID="ctl1" runat="server" />
<asp:Button runat="server" id="bt" Text="Click me" OnClick="Click" />
<%=DateTime.Now.ToString() %>
</form>
</body>
</html>

i might be missing something, so i think that the easies way to reproduce the problem is for you to put everything here :)


Oh, i just missed "EnsureChildControls();", Thank you for pointing outSmile

Best Regards,

No comments:

Post a Comment