c# - User Control error Object reference not set to an instance of an object? -


i have 2 webusercontrol.

1. uc_1.axcx 2. uc_2.ascx 

i tried access uc_2.ascx.cs method uc_1.axcx.cs. below uc_1.ascx.cs method.

protected void page_load(object sender, eventargs e) {     uc_2 objuc = new uc_2();     objuc.assignname("123'); } 

uc_2.ascx.cs:

public string assignname(string nameparam) {   textbox1.text = nameparam;   //here getting object null error.   retrun "access uc_2 successfully."; } 

while accessing uc_2 method uc_1, getting:

object reference not set instance of object.

how solve issue ?

you need register uc_2.ascx in uc_1.ascx instead of instantiate it. in uc_1.ascx :

<%@ register src="~/uc_2.ascx" tagprefix="uc1" tagname="uc_2" %>  <uc1:uc_2 runat="server" id="uc_2" /> 

and in uc_1 code behind change page_load this:

protected void page_load(object sender, eventargs e) {     uc_2.assignname("123"); } 

edit: call uc2 method dynamically without register in ascx, try this:

var uctrl = (uc_2)loadcontrol("~/uc_2.ascx");  controls.add(uctrl); uctrl.assignname("123"); 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -