我有个a.aspx的窗体页面,里面只放了一个a.ascx的用户控件
a.ascx内只有一个label1的控件请问如何在a.aspx的代码里访问这个label1的子控件
比如在a.aspx的Page_load事件(或其他事件)直接给a.ascx的label1控件赋值?如label1.Text="xxxx"
可以这样吗?

解决方案 »

  1.   

    可以的。试试在a.ascx.cs中找到protected Label label1;
    将protected 改为public.
      

  2.   

    假如 a.ascx 的id 为 a   
    (Label)a.FindControl("label1")
      

  3.   

    先谢谢各位的回答chjlcn(chjl.cn)
    Dim NewLabel As New a
    NewLabel .label1.Text = "Test"
    不行呀,没反映~~
     wang790809(石头)
    你的代码我看的不是很明白,能否给出VB的格式?晕,要是这个问题搞不定的话,那这些用户控件还怎么重用呀?
    各位帮我看看呀
      

  4.   

    可以的。试试在a.ascx.cs中找到protected Label label1;
    将protected 改为public.ctype(a.FindControl("label1"),Label).text = "哈哈";
      

  5.   

    Dim newLabel as Label=CType(a.FindControl("label1"),label)
    然后你不就可以对newLabel做操做了吗
      

  6.   

    ===========================================================================
    jyk(喜欢编程。和气生财。共同提高。共同进步) 
    用的你的方法
    Dim a As New tests
    CType(a.FindControl("lab"), Label).Text = "哈哈"
    结果:异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
    源错误: 
    行 26:         Dim a As New tests
    行 27:         CType(a.FindControl("lab"), Label).Text = "哈哈" (出错行)
    ============================================================================
    renyu732(Good Good Study,Day Day Up.) 、
    你的也不行呀
    异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
    源错误: 
    行 28:         Dim NewLabel As Label = CType(a.FindControl("lab"), Label)
    行 29:         NewLabel.Text = "test"                           (出错行)
    ===========================================================================各位帮帮忙呀,帮看看怎么实现呀
    谁要分我给他
      
      

  7.   

    在用户控件里,,,定义一个公开变量,,,比如
    public labeltext 
    labeltext=label1.text然后在a.aspx里以用户控件属性的方式改变labeltext的值,,,,
    <uc1:mycontrol runat="server" labeltext="hello"></uc1:mycontrol>还可以用
    get
    set 
      

  8.   

    aaron_lly(永远,永远,等她回来!)
    你的这个方法我知道,但页面一但输入框和选择框达到几十或者上百的话,那这么做还不累死?
    而且动态调用的时候还需要对页面绑定~
    有简单的方法吗?
      

  9.   

    private void Page_Load(object sender, System.EventArgs e)
    {
    myControl1 = this.FindControl("WebUserControl11"); // 在此处放置用户代码以初始化页面
    }
    private void Button1_Click(object sender, System.EventArgs e)
    {
    myControl1.Visible = true;
    UserControl myUserControl = (UserControl)myControl1;
    Label uLabel1 = (Label)myUserControl.FindControl("Label1");
    Label1.Text = "控件内容:" + uLabel1.Text;
    }
      

  10.   

    前边有
    private Control myControl1 =null;
      

  11.   

    先谢过WYZSC(007)
    不过你的代码是C#的,我看不明白
    可否换成等价的VB语法呢?
    非常感谢^_^
      

  12.   

    不好意思,我不会vb.net,好象主要是定义控件部分用dim调整,还是请哪位朋友帮下忙吧。