使用c#回发后如何动态初始化用户控件的控件? [英] How to Dynamically initilatize controls of User control after postback using c#?

查看:78
本文介绍了使用c#回发后如何动态初始化用户控件的控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个Aspx页面和一个ascx页面.在aspx页面中,我有radTabstrip,在每个RadTb中,我都在使用Ascx.
我想在aspx页面中动态加载ascx页面的控件.
如果我再次尝试将值分配给ascx.cs页中的这些ascx控件,则无法在aspx页完成后立即找到ascx的控件.
它说了像标签这样的控件:

"Object reference not set to an instance of an object"

ASCX代码:

Hi,
I have a Aspx page and a ascx page. And in aspx page I have radTabstrip and in each RadTb I am using Ascx.
I want to dynamically load the controls of ascx page in aspx page.
It is not able to find the controls of ascx once after aspx page is done if I again try to assign value to those ascx controls in ascx.cs page.
It says for the controls like label:

"Object reference not set to an instance of an object"

ASCX code:

protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            bindControls();

        }public void bindControls()
    {
//setting controlls values
lbl.text=some value;
    }

         
   }



ASpx代码:



ASpx Code:

protected voidGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
//calling the ascx method
acsxpage objacsxpage=new acsxpage(); 
objacsxpage.bindControls();
}


这里没有找到控件lbl.


谢谢,
请帮助解决此问题.


Here it is not fininding the control lbl.


Thanks,
Please help to resolve this issue.

推荐答案

我认为您正在创建新的用户控件,而不是从RadTab或使用的任何容器中找到用户控件. >
I think you are creating new user controls instead of finding the usercontrols from RadTab or from whatever container you are using.

acsxpage objacsxpage=new acsxpage(); 



除了上面的代码,您还必须访问已在aspx页面中初始化的实例.



Instead of the above code you have to access the instance which is already initialized in the aspx page.


它找不到,因为它无法直接访问它.

考虑一下:如果在另一个ASPX页面上使用此控件,则它将没有lbl控件.那么控件应该怎么做?

找到更好的解决方案,或将lbl 实例传递给ASCX控制方法.
It can''t find it, because it has no direct access to it.

Think about it: if you use this control on a different ASPX page, it will not have a lbl control. What is the control supposed to do then?

Find a better solution, or pass the lbl instance through to the ASCX control method instead.


这篇关于使用c#回发后如何动态初始化用户控件的控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆