ASP.NET控件暴露给其他类 [英] ASP.NET expose controls to other class

查看:128
本文介绍了ASP.NET控件暴露给其他类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在争夺这一段时间,我需要一些指导。

我在编码ASP.NET 4.0 web表单。

的问题是:如何公开一个文本框,标签或任何其他控件到另一个类

我有一个WebForm(见下文)。

 公共部分类WebForm1的:System.Web.UI.Page
{}

这是引用的话,并发送到另一个类。

 公共类SearchInitializer
{
    私人WebForm1的_webform1;    公共SearchInitializer(WebForm1的WebForm1的)
    {
        _webform1 = WebForm1的;
    }    公共无效ChewSettings()
    {        _webform1 //不能在这里找到任何控制?
    }}

首先我想到的创建一个公共属性,我想我可以从我送到新类的引用访问。但拿去!

 公共部分类WebForm1的:System.Web.UI.Page
{
   公共字符串KeywordBox1
    {
        {返回txt_keyword.Text;}
        集合{txt_keyword.Text =值;}
    }}

在我试图继承的Webform成其他类。使得现有的财产,但没有运气。

 公共类SearchInitializer:WebForm1的
{
    私人WebForm1的_webform1;    公共SearchInitializer(WebForm1的WebForm1的)
    {
        _webform1 = WebForm1的;
    }    公共无效ChewSettings()
    {        _webform1 //不能在这里找到任何控制?
    }}

好吧抽象类migth是用在这里,一切都继承。但我认为我得到了错误的。我有事件和静态类,这样他们可以与网页交谈。但我真的想不使用静态类作为容器保存在我的控制的所有信息。
因此,这些都是我曾尝试例子和他们都失败了。所以这是我basicly试图扩大我所知道的)感谢您阅读!
为什么他们失败了,我应该怎么办呢?

编辑的要求!

 公共部分类WebForm1的:System.Web.UI.Page
{     保护无效btn_click(对象发件人,EventArgs的发送)
     {
         SearchInitializer searchIni =新SearchInitializer(本);
     }}


解决方案

要暴露的控制有两种方法我能想到的,你可以使用。

您可以删除 myPage.designer.cs 文件中的以下语句,并把它放在你的code身后,公开声明:

 保护全球:: System.Web.UI.WebControls.TextBox myTextBox;

变为

 公共System.Web.UI.WebControls.TextBox myTextBox;

这应该立即访问。我的preferred方法是添加属性要提供对每一个具体的控制。

 公共System.Web.UI.WebControls.TextBox MyTextBoxElement
{
    得到
    {
        返回myTextBox;
    }
}

这使得如果你需要他们或其他条件语句提供补充访问控制。在任何情况下,访问无论外地或财产,消费对象必须通过特定的页面类型参照此。

I have been battling this for some time and I need some guidance.

I'm coding in ASP.NET 4.0 WEBFORMS.

Question is: How to expose a textbox, Label or any other control to another class.

I have a webform (see below).

public partial class WebForm1 : System.Web.UI.Page
{

}

This is then referenced and sent to another class.

public class SearchInitializer
{
    private WebForm1 _webform1;

    public SearchInitializer(WebForm1 Webform1)
    {
        _webform1 = Webform1;
    }

    public void ChewSettings()
    {

        _webform1 //can't find any control in here?!
    }

}

First I thought of creating a public property which I thought I could access from the reference I sent to the new class.. But nooo!

public partial class WebForm1 : System.Web.UI.Page
{
   public string KeywordBox1 
    {
        get {return txt_keyword.Text;}
        set {txt_keyword.Text = value;}
    }

}

The I tried to inherit the Webform into the other class. Making the the property available but no luck there.

public class SearchInitializer : Webform1
{
    private WebForm1 _webform1;

    public SearchInitializer(WebForm1 Webform1)
    {
        _webform1 = Webform1;
    }

    public void ChewSettings()
    {

        _webform1 //can't find any control in here?!
    }

}

Okay an abstract class migth be of use here, inheriting everything. But I think I got that wrong to. I have events and static classes, so they can talk with the page. But I really would like not to use a static class as a container to save all the info in my controls. So these are the examples I have tried and they all failed. So this is me basicly trying to expand what I know ;) Thanks for reading!! Why have they failed and how should I do it?

EDIT AS REQUESTED!

public partial class WebForm1 : System.Web.UI.Page
{

     protected void btn_click(object sender, EventArgs e)
     {
         SearchInitializer searchIni = new SearchInitializer(this);
     }

}

解决方案

To expose the controls there are two methods I can think of that you can employ.

You can remove the following statement from the myPage.designer.cs file and place it in your code behind as a public declaration:

protected global::System.Web.UI.WebControls.TextBox myTextBox;

becomes

public System.Web.UI.WebControls.TextBox myTextBox;

This should make it immediately accessible. My preferred method is to add a property for each specific control that you want to provide access to.

public System.Web.UI.WebControls.TextBox MyTextBoxElement
{
    get
    {
        return myTextBox;
    }
}

This allows to provide supplementary access controls if you need them or other conditionals. In any case, to access either the field or the property, the consuming object must reference this by your specific page type.

这篇关于ASP.NET控件暴露给其他类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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