且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET控件暴露给其他类

更新时间:2023-12-06 12:33:46

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

您可以删除 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.