hi, i have a base user control (WC1) which inherits from WebUserControl and a child user control (WC2) which inherits from WC1. I added a textBox and a label to WC1 in vs design view not code behind. But all the server controls that i put in WC1 are null and give null pointer exception.I tried to access textBox and label in different events but the all of them are null.
Anyone knows what went wrong here?thanks.
Steps to reproduce the error:
- create a web user control called WC1 by add a new item in VS
- added a textBox and label server control to WC1 in design view of VS and put code behind: var value = txetBox.Text;
- created another web user control called WC2 by add a new item in VS and change its base class to WC1 in its code behind
- create a web page by adding a new item in vs and drag WC2 to the design view of the page
- run the page then you will see the exception for the codebehind in WC1
code:
WC1: code behind: public partial class WC1: System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) {
}
}
page:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WC1.ascx.cs" Inherits="WebApplication1.WC1" %>
WC2: code behind: public partial class WC2: WC1 { var txt = TextBox1.Text}
page:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WC2.ascx.cs" Inherits="WebApplication1.WC2" %>
To sum up my question:
can web server controls of WC1 be instantiated in this case? or can user control inherit from another user control and web server controls of parent user control be carried to its children without worring the layout?
Ok i have tested if i dont add control in WC1 codebehind, it will never be instantiated.