I am loading my user control from a string representation of the control (created by XSLT transformation). I am using the OnPreInit to load the control in a placeholder inside the page.
My code looks like
strControl = "<ulc:calendar id=\"cal\" ErrorMessage=\"Please enter calendar date.\" runat=\"Server\" />"
Control ctrl= Page.ParseControl(strControl);
MasterPage master = this.Master; //Line used to instanciate Placeholder control inside preinit event. Tried with init even too without this line.
placeHolder.Controls.Add(ctrl);
Inside My Calender Control there is one textbox and one RequiredFieldValidator (simplified)
My calender property ErrorMessage is as below
public string ErrorMessage
{
set { requiredFldVal.ErrorMessage = value; }
}
Now when I run, I am getting object reference not set error trigerred from ErrorMessage property as the RequiredFieldValidator inside the calender is not instanciated yet.
I can skip the error with little modification of the code
But my question is that if I directly write the control in aspx page, not loading from string representation into place holder, it works fine without checking whether the control is loaded or not. Like Test.aspx page as below
<ulc:calendar id="ca\" ErrorMessage="Please enter calendar date." runat="Server" />
But why?
I can set the In built control's property while loading from string like
strControl = "<asp:RequiredFieldValidator ID=\"rfvCalendar\" runat=\"server\" ErrorMessage="Date cannot be blank."
ControlToValidate=\"txtCalendar\" />"
Control ctrl= Page.ParseControl(strControl);
MasterPage master = this.Master; //Line used to instanciate Placeholder control inside preinit event. Tried with init even too without this line.
placeHolder.Controls.Add(ctrl);
But why not my user control's property? How the property is handled inside in built controls.
I know that the Init event is triggered for User control first and then the aspx Page's Init even. So I tried inside preinit even of Page. But the error is thrown as soon as I do the
Page.ParseControl(strControl);
Note I am not looking how to get rid of the error as I already have the solution. But I want to get rid of the error in best possible way.
What is the best possible approach for this kind of situation. Actually I have hundread of controls with many perperties. So I want some really good approach.
If this post was useful to you, please mark it as answer. Thank you!
aspxdev.blogspot.com
Hi aalokitoaami, I'm not so sure that I did undestand you correctlly but, I tried to write an example of OnPreInit function that contain code that
loads dynamically user control which contains two another ASP.NET controls (Calendar and Textbox). After creating a instance of the user control, the code is creating a reference to TextBox control placed inside the user control and setting
the property Text. After that user control is added to placeholder.
protected override void OnPreInit( EventArgs e )
{
UCTest ctrl = (UCTest)Page.LoadControl( "UCTest.ascx" );
TextBox tb = ctrl.FindControl( "tbxString" ) as TextBox;
tb.Text = "the string";
phTest.Controls.Add( ctrl );
base.OnPreInit( e );
}
I hope that this example can be useful to rewrite your solution. Best,
It will not through any error. Here you are setting the message shown when the Calender is kept blank is set through the "ErrorMessage" property. ErrorMessage internally sets the message for required field validator inside the Calendar control.
But the same thing if you try with Page.ParseControl as below
strControl = "<uc1:Calendar ID='calendar' runat='server' ErrorMessage='Date can't be blank' />"
Control ctrl= Page.ParseControl(strControl);
MasterPage master = this.Master; //Line used to instanciate Placeholder control inside preinit event. Tried with init even too without this line.
placeHolder.Controls.Add(ctrl);
It will through null reference exception
If this post was useful to you, please mark it as answer. Thank you!
aspxdev.blogspot.com
aalokitoaami
Member
158 Points
47 Posts
User Control Attribute Set from XSLT Transformation
Feb 12, 2012 07:10 PM|LINK
I am loading my user control from a string representation of the control (created by XSLT transformation). I am using the OnPreInit to load the control in a placeholder inside the page.
My code looks like
strControl = "<ulc:calendar id=\"cal\" ErrorMessage=\"Please enter calendar date.\" runat=\"Server\" />" Control ctrl= Page.ParseControl(strControl); MasterPage master = this.Master; //Line used to instanciate Placeholder control inside preinit event. Tried with init even too without this line. placeHolder.Controls.Add(ctrl);Inside My Calender Control there is one textbox and one RequiredFieldValidator (simplified)
My calender property ErrorMessage is as below
public string ErrorMessage { set { requiredFldVal.ErrorMessage = value; } }Now when I run, I am getting object reference not set error trigerred from ErrorMessage property as the RequiredFieldValidator inside the calender is not instanciated yet.
I can skip the error with little modification of the code
bool isControlLoaded = false; protected override void OnInit(EventArgs e) { base.OnInit(e); isControlLoaded = true; SelectedDate = _SelectedDate; ValidationGroup = _validationGroup; } strin _errorMessage = ""; public string ErrorMessage { set { _errorMessage = value; if (isControlLoaded) rfvCalendar.ErrorMessage = value; } }But my question is that if I directly write the control in aspx page, not loading from string representation into place holder, it works fine without checking whether the control is loaded or not.
Like Test.aspx page as below
But why?
I can set the In built control's property while loading from string like
strControl = "<asp:RequiredFieldValidator ID=\"rfvCalendar\" runat=\"server\" ErrorMessage="Date cannot be blank." ControlToValidate=\"txtCalendar\" />" Control ctrl= Page.ParseControl(strControl); MasterPage master = this.Master; //Line used to instanciate Placeholder control inside preinit event. Tried with init even too without this line. placeHolder.Controls.Add(ctrl);
But why not my user control's property? How the property is handled inside in built controls.
I know that the Init event is triggered for User control first and then the aspx Page's Init even. So I tried inside preinit even of Page. But the error is thrown as soon as I do the
Note I am not looking how to get rid of the error as I already have the solution. But I want to get rid of the error in best possible way.
What is the best possible approach for this kind of situation. Actually I have hundread of controls with many perperties. So I want some really good approach.
aspxdev.blogspot.com
Phantomot
Member
240 Points
47 Posts
Re: User Control Attribute Set from XSLT Transformation
Feb 12, 2012 08:44 PM|LINK
Hi aalokitoaami, I'm not so sure that I did undestand you correctlly but, I tried to write an example of OnPreInit function that contain code that loads dynamically user control which contains two another ASP.NET controls (Calendar and Textbox). After creating a instance of the user control, the code is creating a reference to TextBox control placed inside the user control and setting the property Text. After that user control is added to placeholder.
protected override void OnPreInit( EventArgs e ) { UCTest ctrl = (UCTest)Page.LoadControl( "UCTest.ascx" ); TextBox tb = ctrl.FindControl( "tbxString" ) as TextBox; tb.Text = "the string"; phTest.Controls.Add( ctrl ); base.OnPreInit( e ); }I hope that this example can be useful to rewrite your solution. Best,
dynamic add
aalokitoaami
Member
158 Points
47 Posts
Re: User Control Attribute Set from XSLT Transformation
Feb 13, 2012 02:22 AM|LINK
Hi Phantomat, Thanks for you response. But here I am not trying to set property of a Textbox inside User Control(.ascx) from main page (.aspx).
If you write a create an aspx page as
<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/MasterPageAdmin.master" AutoEventWireup="true" CodeFile="AddEditItem.aspx.cs" Inherits="Admin_AddEditItem" %> <%@ Register TagName="Calendar" TagPrefix="uc1" Src="~/Controls/Calendar.ascx" %> <uc1:Calendar ID="calendar" runat="server" ErrorMessage="Date can't be blank" /> </asp:Content>It will not through any error. Here you are setting the message shown when the Calender is kept blank is set through the "ErrorMessage" property. ErrorMessage internally sets the message for required field validator inside the Calendar control.
But the same thing if you try with Page.ParseControl as below
strControl = "<uc1:Calendar ID='calendar' runat='server' ErrorMessage='Date can't be blank' />" Control ctrl= Page.ParseControl(strControl); MasterPage master = this.Master; //Line used to instanciate Placeholder control inside preinit event. Tried with init even too without this line. placeHolder.Controls.Add(ctrl);It will through null reference exception
aspxdev.blogspot.com
cnranasinghe
Star
8885 Points
1798 Posts
Re: User Control Attribute Set from XSLT Transformation
Feb 13, 2012 04:37 AM|LINK
Hi
Property is refered before the it is created on the server. Coz control is created on the runtime. Following thread might give some idea on this,
check following lik
http://forums.asp.net/t/1191194.aspx