我怎样才能访问控件我的ASP.NET页面上从一个类的解决方案中? [英] How can I access the controls on my ASP.NET page from a class within the solution?

查看:183
本文介绍了我怎样才能访问控件我的ASP.NET页面上从一个类的解决方案中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我不使用母版,我只是有一个Default.aspx的项目有几个标签,文本框等,我已经花了几个小时寻找一个解决方案,我已经发现了一些事,但在我的脑海还是我的错误,我没有做到我想要的方式。

Basically I am not using a MasterPage and I just have a project with a Default.aspx with a few labels, textBoxes etc. I've spent several hours looking for a solution and I have found one, but something in my head still bugs me, that I didn't do it the way I wanted to.

我没有烦恼访问它们,并从pageLoad的设置其属性。

I have no troubles accessing them and setting their properties from the PageLoad.

不过,我做了一个类,就像下面的code片段,它抛出我一个对象未设置为一个对象的实例引用。而我似乎无法弄清楚,我错过了什么。

But I made a class, just like the code snippet below, and it throws me an "Object reference not set to an instance of an object". And I cannot seem to figure out what I am missing.

   class Functions 
    {
       public static void myMethod()
       {
            WebForm1 mainForm = new WebForm1();
            mainForm.myTextBox.Text = "Something.";
       }
    }

我设法像这样的事:

I managed to do with like this:

   class Functions 
    {
       public static void myMethod()
       {
           System.Web.UI.Page myMainForm;
           myMainForm = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
           TextBox myTextBox = (TextBox)myMainForm.FindControl("myTextBox");
       }
    }

但它不是只是在做它,它是关于学习的地方我错了,因为我无法弄清楚,我希望有一个简单的方法来做到这一点,考虑到我是很新的ASP.NET 。通常,当我编码的基本应用程序的WinForms - 我没有访问不同形式的问题。

But it's not about just doing it, it's about learning where I am wrong as I cannot figure it out and I want a simpler way to do it, considering I am very new to ASP.NET. Normally when I am coding basic applications in WinForms - I don't have problems accessing different forms.

和我不喜欢做的的FindControl()作为每个控制我试图访问。如果有一个的简单而有效的的变通方法我很想听到它。

And I don't like doing a FindControl() for every control I am trying to access. If there is a simple and yet effective workaround I would love to hear it.

感谢您提前。

修改:我已经从我的项目使这个例子不谈,但它是我想要实现的想法。有可能是在code段的一些小错误,请原谅我。

Edit: I've made this example aside from my project, but it's the idea I am trying to accomplish. There might be some small errors in the code snippets, please excuse me.

推荐答案

使用的code隐藏文件这一点。不要紧密结合业务逻辑类到你的用户界面。

Use the code-behind files for this. Don't tightly couple business logic classes to your user interface.

WebForm1.aspx的应有WebForm1.aspx.cs中,如果使用C#或WebForm.aspx.vb如果用vb,所以如果你有WebForm1.aspx的一个TextBox控件,你应该能够访问它在code-隐藏文件(WebForm1.aspx.cs中或WebForm1.aspx.vb)

WebForm1.aspx should have WebForm1.aspx.cs if using c# or WebForm.aspx.vb if using vb, so if you have a TextBox control on WebForm1.aspx, you should be able to access it in the code-behind file (WebForm1.aspx.cs or WebForm1.aspx.vb)

public class Validate
{
    public bool IsEmail(string email)
    {
        //return false if invalid e-mail or true if valid
    }
}

然后在你的WebForm.aspx.cs,你可以叫

Then in your WebForm.aspx.cs, you can call

 Validate validate = new Validate();
 bool isValidEmail = validate.IsEmail(txtEmail.Text);

在哪里txtEmail是控制。您可以进行验证类静态的,所以你可以做:

Where txtEmail is the control. You can make the Validate class static, so you can just do:

 bool isvalidEmail = Validate.IsEmail(txtEmail.Text);

这篇关于我怎样才能访问控件我的ASP.NET页面上从一个类的解决方案中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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