This is a good Question. If I re-phrase
it will be somehow like below.
How to pass some internal information of a Class to its parent Class.
Suppose
Class A
contains
Class b
and something occurs in
Class B
, how would it pass information to
Class A
.
This is best achieved with Events. Class B would expose some event and Class A consumes that Event.
For instance when a combo Box is Selected, it raise a
OnselectedIndexChanged
event which we catch on .aspx page.
In same manner you have to make a event
OnComboSelected
in the UserControl Class.
Your aspx page will catch this event and the things is done.
In your problem context.
In webuserControl Class
This is the DropDown
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
</asp:DropDownList>
Usercontrol Code-behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public class ComboEventArgs : EventArgs
{
public ListItem Item;
public ComboEventArgs(ListItem _L)
{
Item = _L;
}
}
public partial class WebUserControl : System.Web.UI.UserControl
{
public delegate void Comboselected(object sender, ComboEventArgs e);
protected Comboselected Comboselected1;
public event Comboselected EComboSelected
{
add
{
Comboselected1 += value;
}
remove
{
Comboselected1 -= value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (Comboselected1 != null)
{
Comboselected1(this, new ComboEventArgs(DropDownList1.SelectedItem));
}
}
}
The .aspx page
<uc1:WebUserControl ID="WebUserControl1" runat="server" OnEComboSelected="ComboSelected" />
.aspx page code-behind
protected void ComboSelected(object sender, ComboEventArgs e)
{
Response.Write(e.Item);
}
Even If you dont make AUTOPOSTBACK property of the DropDownList to True. the Event will still raise and your page would show the Selected value when the postback occurs by some other control.