Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#asp.net textbox.text是否未设置?_C#_Asp.net_User Controls - Fatal编程技术网

C#asp.net textbox.text是否未设置?

C#asp.net textbox.text是否未设置?,c#,asp.net,user-controls,C#,Asp.net,User Controls,在父类中,我调用在子类中定义的函数,并解析我需要的值 ParentClass.ascx protected void Page_Load { if(info != null) ControlIWantToGetInformationTo.SetInfo(info); } public void SetInfo(Info info) { someTextBox.Text = info.TheVariableWithin.ToString();

在父类中,我调用在子类中定义的函数,并解析我需要的值

ParentClass.ascx

protected void Page_Load 
{       
  if(info != null) 
    ControlIWantToGetInformationTo.SetInfo(info);  
}  
public void SetInfo(Info info)  
{  
  someTextBox.Text = info.TheVariableWithin.ToString(); 
}  
ChildClass.ascx

protected void Page_Load 
{       
  if(info != null) 
    ControlIWantToGetInformationTo.SetInfo(info);  
}  
public void SetInfo(Info info)  
{  
  someTextBox.Text = info.TheVariableWithin.ToString(); 
}  

我能收集到的是ParentClass(control)加载并执行该方法,但当ChildClass(control)页面加载时,它会将以前设置的变量重置为null。我如何解决这个问题?

我认为您面临区分大小写的问题

试试这个

someTextBox.Text = info.TheVariableWithin.ToString(); 

使用会话。在方法中,不要设置控件的值,而是使用对象并填充对象的属性,完成后将其保存到会话。在子类中,从保存到会话中的对象加载值

//Parentclass
protected void Page_Load 
{       
  if(info != null) 
  {
    MyControlObject myObj = new MyControlObject();
    myObj.prop1 = txt1.Text;
    myObj.prop2 = txt2.Text;
    Session["myObj"] = myObj;
  }
} 

//Childclass
public void SetInfo(Info info)  
{  
  MyControlObject myObj = Session["myObj"] as MyControlObject;
  if(myObj != null)
  {
    //assign the values to your controls
    Session["myObj"] = null; //when you are done, clear the session.
  }
}  

如何在控件内设置文本值?您必须确保代码以正确的顺序运行,但由于复杂的页面生命周期规则,这在ASP.Net webforms中可能非常复杂。啊,听起来像是我遇到的问题:'(你是说我必须分解所有内容吗???一个快速修复方法可能是将当前在Page_Load中的代码移动到PreRender事件,该事件在生命周期的后期发生。但这不是一个很好的解决方案,因为一旦你不得不在控件内部使用PreRender,它就会分解。希望避免+1,因为Solutioni上确实有效。这只是一个打字错误。抱歉,现在正在讨论修复问题。一旦会话被创建和使用,我如何销毁它?感谢+1,因为解决方案有效,将等到GMT+0午夜时间,我才接受,以防有人回答,避免会话和预渲染事件。我有同样的问题,希望看到更好的解决方案这一次,我就是这么做的。