Cordon
Cordon

Reputation: 7

Placing code from webform into class

I have some code in a button_click event in my webform that performs some actions (writing some xml files).

But i wish to place all my XML code in 1 class within my webproject.

But how do i exactly acces controls from my webform on this class? And how do i link those 2 with each other?

Ive tried inherit from System.Web.UI.Page in my class but seems thats not quite it.

Kind Regards.

Upvotes: 0

Views: 121

Answers (1)

Jaime
Jaime

Reputation: 6814

Create a class that has the logic to write your XML file and receives the data though parameter. That class does not access your web controls. Now in your code behind in your click handler, you take the data from the control, instantiate your XML writer class and call the method with the data as parameter

Button_click(object sender, EventArgs e)
{
    var data = MyControl.Text;
    var writer = new MyWriterClass();
    writer.WriteToXml(data);
}

Upvotes: 4

Related Questions