views:

117

answers:

5

In my page I have a textbox txtMyInfo. I also have a separate business logic class that I use to manipluate stuff on the page. How do I refer to txtMyInfo from a method in the business logic class. Can I reference the control via the page object? I don't see a reference.

public void MyBusinessLogic(Page page){
    page.Controls.txtMyInfo.Text="";
}
+2  A: 

Try...

TextBox txtInfo = (TextBox)page.FindControl("txtMyInfo");
George
It always returns null.
Praesagus
If you call MyBusinessLogic() too early in the page life cycle this can happen. When are you calling it?
George
Also check this...http://stackoverflow.com/questions/190593/page-lifecycle-using-findcontrol-to-reference-a-control-created-programatically
George
A: 

If you did it that way you could have to reference the actual class of hte control. You could also use FindControl, which would be a little more generic but would work

public void MyBusinessLogic(Page page){
    page.findcontrol("txtMyInfo").Text="";
}
NickAtuShip
+1  A: 

You can just have a TextBox parameter and pass in the TextBox rather than the Page.

Mark Redman
+3  A: 

Business logic should not operate on your UI. You are going to end up with a big ball of what is essentially codebehind in your project.

You should try and familiarize yourself with a concept called "Separation of Concerns": http://en.wikipedia.org/wiki/Separation_of_concerns

In this case, you'd really want something more like this for your business logic:

public static class MyBusinessLogicClass
{
     public static string GetMyInfo()
     {
          return string.Empty;
     }
}

And in your UI code, now you'd have:

public void Page_Load(object sender, EventArgs e)
{
     txtMyInfo.Text = MyBusinessLogicClass.GetMyInfo();
}

It will help you avoid this issue altogether.

Edit: I'd also like to point out that it doesn't matter what pattern you use (notice in my example I'm not using anything like MVC, MVP, or "Joe's Pattern D'Jour"). Just separating your concerns is enough.

Edit Edit: Though this answer does not directly answer your question about how to reference controls from outside of the UI, it indirectly answers it by showing you a way to avoid having to do this at all.

Anderson Imes
Thank you, your example is clear. If you added something about control reference (I might need it elsewhere), then you will have solved my problem and answered my question.
Praesagus
I'm saying you should avoid it completely. Other than that, referencing controls is as simple as using the reference to it. Other folks have posted about this, but you should never need to reference a control from anywhere but the UI, which you seem to know how to do already. Even at the risk of not getting an accepted answer, I won't perpetuate this kind of programming... I might have to work on your code someday :)
Anderson Imes
+1  A: 

I also have a separate business logic class that I use to manipluate stuff on the page. How do I refer to txtMyInfo from a method in the business logic class.

If you're trying to implement an MVC pattern, you'll almost never modify the UI directly through your BL class. Instead, create an interface for it, something like:

// Interface definition
public ISearchForm
{
    String Keywords { get; set; }
    int ItemsPerPage { get; set; }
    Action<string> SearchButtonClicked;
    // ...
}

// Implementation
public SearchForm : ISearchForm
{
    public String Keywords
    {
        get { return txtKeywords.Text; }
        set { txtKeywords.Text = value; }
    }

    // ...
}

Your interface should expose the interesting parts of your page to your controller class.

Juliet