Diver Dan
Diver Dan

Reputation: 9963

Trigger function inside a usercontrol from a page

I have created a usercontrol to capture education details, it contains 5 textboxes and an functionto insert that values into my db. I have added the usercontrol 5 times to a page. I have a button on my aspx page which I want to be able to click and call the function to insert the values.

Can anyone suggest how I can do this?

Upvotes: 0

Views: 505

Answers (2)

John K
John K

Reputation: 28869

Put a public method on your user control.

// Inside MyUserControl
public void SaveIt() {
    // logic to perist the values to db.
}

From your ASPX page (or code behind) when the button is clicked, loop over your user control instances and call the method:

// On Click...

MyUserControl[] arrControls = new MyUserControl[] {myUC1, myUC2, myUC3, myUC4, myUC5};
foreach (MyUserControl c in arrControls)
    c.SaveIt();

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460148

The function which inserts values into your db should not be in your usercontrols but in your page. Does your usercontrol has a save button itself or only the page? On first variant you should raise an event and the page could catch it to save the values. On second variant you should iterate through your usercontrols(added dynamically or fixed?). You should add public properties which return the textbox's values to the page, then you can save these values into the db.

Upvotes: 1

Related Questions