查找和访问WebUserControl元素 [英] Finding and accessing elements of a WebUserControl

查看:159
本文介绍了查找和访问WebUserControl元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建在ASP.net一个WebUserControl并希望以后能够访问控制的元素。当我访问他们,虽然我试图一起访问它们。例如,增加一个CheckBox和一个表来控制,然后找出该复选框,检查它是否已经被选中,如果它已被选中,从文本框得到的值。

I am creating a WebUserControl in ASP.net and want to be able to access elements of that control later. When I access them though I am trying to access them together. For example, adding a CheckBox and a table to a control and then finding the CheckBox, checking whether it has been ticked and if it has been ticked, get the values from the TextBoxes.

我现在拥有的一切,从加载自定义控件的页面上,而是通过页面上的控件时迭代,似乎没有成为我的WebUserControl类型的控件。所有的控制都在那里页面上,但它们的存在为单独的ASP.net控制。

I currently have everything loading on the page from the custom controls, but when iterating through the controls on the page, there doesn't appear to be a control of my WebUserControl type. All of the controls are there on the page, but they exist as seperate ASP.net controls.

我在想这个问题?有没有更好的方式来做到这一点?

Am I thinking about this wrong? Is there a better way to do this?

我不知道这是很好的解释,请随时提出澄清的问题。

I'm not sure this is explained very well, please feel free to ask clarifying questions.

推荐答案

您需要通过公共属性或功能,让它做你需要什么,或者像你希望公开用户控制功能。因此,例如你的情况,你可以有像你的用户控件的属性(你也可以做一个函数):

You need to expose the user controls functionality by making public properties or functions to make it do what you need or act like you want. So for example in your case you could have a property in your user control like (you could also do a function):

public List<string> SomeValues
{
    get
    {
        // return null if checkbox is not checked, you could just as easily return an empty list.
        List<string> lst = null;
        if (yourCheckBox.Checked)
        {
            lst = new List<string>();

            // You could have something that iterates and find your controls, remember you  
            // are running this within your user control so you can access all it's controls.
            lst.Add(yourTextBox1.Text);
            lst.Add(yourTextBox2.Text);
            lst.Add(yourTextBox3.Text);
            // etc...
        }
        return lst;
    }
}

然后在你的页面,您可以访问您的用户控制和调用这个属性来获取值:

Then in your page you can access your user control and call this property to get the values:

// assuming you defined your usercontrol with the 'yourUserControl' ID
List<string> lst = yourUserControl.SomeValues;

关键是要揭露只是你想要在你的用户控件什么,所以无论是用它并不需要了解它的细节或执行。你应该能够只使用它,就像任何其他的控制。

The key is to just expose what you want IN your user control so whatever is using it doesn't need to know about it's details or implementation. You should be able to just use it as you would any other control.

这篇关于查找和访问WebUserControl元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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