从aspx页面的用户控件的Asp.net访问控制 [英] Asp.net access controls of user control from aspx page

查看:169
本文介绍了从aspx页面的用户控件的Asp.net访问控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控件,它有被点击aspx页面上的按钮时执行的方法,我为传递用户控制的两个ID的方法,现在我想在用户的文本框的值控制,但不幸的是它是不承认的文本框我有codeD: -

I have a user control and it has a method which is executed when button on aspx page is clicked, I m passing two ids of the user control in the method, Now i want to get the values of the textbox in the user control but unfortunately it is not recognizing textbox I have coded :-

 //Method to copy values from one control into another
        public void copyInfo(Control ctrl1, Control ctrl2) {  
            List<string> vals = new List<string>();
            foreach (Control c in ctrl1.Controls)
            {
                if (c is TextBox)
                {    
                    if (string.IsNullOrEmpty(((TextBox)c).Text)) { }
                    else {
                   //values from textbox
                        vals.Add(((TextBox)c).Text);
                    }
                }

                              .............
                                ..........
                                  ......

我如何能得到TextBox控件有值。

how can i get the textbox control and there values.

推荐答案

我会在你的用户控件添加一个公共属性,如:

I would add a public property on your UserControl such as:

public string SomeTextboxValue
{
  get
  {
    return SomeTextBox.Text;
  }

  set
  {
    SomeTextBox.Text = value;
  }
}

然后,你需要投你的控件到他们的实际类型,而不是普通的控制类:

SomeControl someControl1 = (SomeControl)ctrl1;
SomeControl someControl2 = (SomeControl)ctrl2;
someControl1.SomeTextboxValue = someControl2.SomeTextboxValue;

或者周围的其他方法取决于从哪个你和dest在副本中。

Or the other way around depending on which your from and dest are in the copy.

如果正在动态创建您的控件,你不能找到它的回传它可能是由于你没有重新在回发的控件。我发现这个系列文章试图了解如何使用和什么时候是很有趣的动态控制过程数据:

If your controls are being dynamically created and you can't find it on postback its probably because you haven't recreated the controls on postback. I have found this article series to be very interesting when trying to understand how to work with and process data from dynamic controls:

  • http://www.4guysfromrolla.com/articles/090308-1.aspx

这环节是第三部分,但它其中重要组成部分,是覆盖您的方案:

That link is to part three but its where the important part is which covers your scenario:

创建自定义客户属性的UI和加载客户端的
  从数据库当前值

当编程添加Web控件的ASP.NET页是
  至关重要的是,控件添加到页面的对每一个
  页面访问
即可。这包括在第一页的访问和的所有的后续
  回发。

When programmatically adding Web controls to an ASP.NET page it is essential that the controls are added to the page on each and every page visit. This includes the first page visit and all subsequent postbacks.

这篇关于从aspx页面的用户控件的Asp.net访问控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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