Problems with understanding user controls in C #

I am new to usercontrols by creating only one, so bear with me. Today I read that usercontrols should be autonomous and not rely on any information from the parent container. I get this part, but what I find difficult to understand is the "right" way to develop my program on this principle.

I am making a web form in C # where there is a page with usercontrol on that page. I made usercontrol in its ascx file and dragged it onto my aspx page. Usercontrol is a pair of dates and a gridview to display the results of an SQL stored procedure.

I would really like to reuse this control, but I can’t understand how to β€œtell” usercontrol what stored procedure I want to run for the specific page on which I am, without breaking the β€œdon”, t rely on the rule of the parent container.

thanks

+3
source share
6 answers

Do not rely on the parent container, this does not mean that you can not communicate. Select the property in the user control that will be set by the parent. But have a default value so that it doesn't work.

, , , . , .

+6

, . - . , , - , . , . , . , , , , , , , .

+2

, . , .

, , gridview . , , , , , , , .

, , , . , , , .

, -, .

+2

, , , ?

: , , , .

, , , proc.

0

, . , , , (MyPage.aspx) .

, , - . . - .

( ) . , EventArgs, ( DateTime). , ( EventHandler<YourEventArgs>) EventArgs. , , , .

, , . . UI . (AddRecord.aspx UpdateRecord.aspx). , , , .

, . / . .

0

"tell" usercontrol which stored procedure I want to run

It looks like you are looking for events / delegates
Ask the control to set the event and let the parent add a handler (perhaps to the constructor if you want to force it to add one and only one event)
Alternatively, you can use the Command / Strategy template - the parent encapsulate method and send the encapsulating object to the control in its constructor.

0
source

Source: https://habr.com/ru/post/1707714/


All Articles