Passing parameter from one user control to another user control in an aspx page
I have two user controls on one aspx page. UC1 has a grid which contains a link button column which user clicks. Based on the value of clicked cell, I need to show some data into UC2.
- How do I pass data from UC1 to UC2?
- How do I invoke a function of UC2 from UC1? 开发者_如何转开发
Please advise. Thanks
AJ
If you follow good OO principles then UC1 and UC2 should not know about each other. What is okay is for the containing page to know about the functionality of its child controls and assist with routing messages/events/data from one to the other.
This means UC1 can raise an event, and the appropriate data (like a row identifier or the actual bound data item) can be passed in the event args of the event. The containing page can then invoke a function on UC2, passing in the piece of info that was communicated from UC1.
- You can set values on UC2 in a postback for UC1.
- You can call functions on UC2 in a postback for UC1.
These solutions rely on a post-back, so it's more complex if you're doing AJAX. However a postback method should work for you.
.aspx page
<uc:MyControlOne runat="server" OnClick="DoPostBack" ID="UC1" />
<uc:MyControlTwo runat="server" ID="UC2" />
.aspx.cs page
public void DoPostBack(object sender, EventArgs e)
{
UC2.Value = UC1.Value;
UC2.UpdateSomething();
}
精彩评论