draw
draw

Reputation: 4846

how can I implement a user control's method outside user control?

Say I have a user control(SubmitButton) having a submit button that when a user clicked on, I want the control, which contains a SubmitButton instance, decide the behavior of submit button.

I have tried the following in the user control .cs file:

    protected void nextPage_Click(object sender, EventArgs e) {

        submitData();

        Response.Redirect("completed.aspx");
    }

    protected abstract void submitData();

But I don't know where the submitData method should be implemented.

It's like a place holder for method.

Upvotes: 0

Views: 204

Answers (3)

Piotr Perak
Piotr Perak

Reputation: 11088

Your control should expose event. For example SubmitClicked. Than in control that contains it You subscribe to that event and do whatever You choose to do. If you have event exposed You can attach to it as many event handlers as you like.

That's what the asp:Button already does. It exposes Click event and in aspx page You just subscribe to that event and implement event handler in Your code behind file.

Upvotes: 1

Denis Biondic
Denis Biondic

Reputation: 8201

Try something like a function delegate:

using System;

namespace ConsoleApplication1
{
    public delegate void FunctionToBeCalled();

    public class A
    {
        public FunctionToBeCalled Function;

        public void Test()
        {
            Function();
            Console.WriteLine("test 2");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            A instanceA = new A();
            instanceA.Function = TheFunction;
            instanceA.Test();
        }

        static void TheFunction()
        {
            Console.WriteLine("test 1");
        }
    }       

}

Upvotes: 0

IanNorton
IanNorton

Reputation: 7282

This will be an abstract class. You can never create an instance of these, you must create your own class that inherits from it and implement the abstract methods and properties. Eg:

public class MySubmitButton : SubmitButton {
  protected override void submitData() {
    // do somthing
  }
}

Upvotes: 0

Related Questions