ASP.Net newb - accessing controls from separate classes
Sorry for the newb question but I am an experienced old-school programmer but I am new to a lot of the .NET ways and want to do it right. Please forgive any bad terminology or ask for clarification of I butcher it too much.
I have an ASP.NET Web Site I am creating in Visual We开发者_如何学运维b Developer 2008 Express using Visual Basic as the language. It is actually a rewrite-with-mods of an existing ASP script and I'm trying to chew what I've bitten off. I created my page with a text box (txtOutput) on it and since the end product is very complex, and it's generally good practice to separate like functions, I have created 4 separate classes in the App_Code section.
My main page is Sync.aspx and has a code file Sync.aspx.vb with it.
One of my code files is SyncDatabases.vb and in it I have created
Public Class SyncDB
In that class I have created some routines such as
Public Function ReadMSDB(ByVal SQLString as string) as Boolean
In this routine I want to put information in my text box txtOutput on the main form (the default - Form1).
My problem is that if I try:
Form1.txtOutput.Text = "Hello world"
or just
txtOutput.Text = "Hello world"
it says Name 'Form1' (or 'txtOutput') is not declared. I am sure I am missing something simple but have no clue what it is. I assume it's the fact that the write command is in a file (class?) outside the file (class?) containing the page itself but I don't know how to address it properly.
Any help is appreciated but please, don't assume a very high base level knowledge of .Net - I'm an old fart that's not up on these new-fangled contraptions. :) Thanks!
This answer might be too simple for what you are trying to accomplish, but have you thought about using a partial class instead of 4 separate classes? A partial class would allow you to separate like functions into different files, but will allow you access to controls on your form. The other answers provide good information also, but like a lot of different things with .NET there is more then one way accomplish what you are trying to do.
Are you trying to refer to txtOutput.Text within the ReadMSDB method? If you are, you need to pass it in as a reference, as in :
Public Function ReadMSDB(ByVal txtOutput As TextBox, ByVal SQLString as string) as Boolean
And in the code-behind of the web page do:
Dim o as new SyncDB
o.ReadMSDB(txtOutput, "...")
HTH.
Generally in ASP.Net you want the page class to be driving things. It should call out to the methods in your other classes and set the results to controls. This means that rather than returning a boolean, look at having your ReadMSDB function return a string. That boolean looks like a good candidate for an exception instead.
More than that, though, I look with deep suspicion on any method that accepts a parameter named "SQLString". That tells me there's a good chance you have an sql injection vulnerability in your code because there's no mechanism left to use propery query parameters. You may want to re-think how you're doing your entire data access layer. I know that sounds like a big job, but your current code looks like it has a very serious problem with it. Myself and others have answers all over Stack Overflow with examples of better patterns for this.
In addition to previous answers.
This is good that you are trying to keep the separation of concerns, but you might want to do this more consciously using one of available patterns. This could help you understand ASP.NET architecture and write a cleaner code from beginning. I will not touch ASP.NET MVC, you can check it yourself.
The first way is to use Model-View-Presenter pattern. Here is a brief description of how to use it: http://www.codeproject.com/KB/architecture/ModelViewPresenter.aspx
The second way is to use Microsoft's Web Client Software Factory. This is more complex way and mostly used in large enterprise applications.
精彩评论