Click here to Skip to main content
15,894,250 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose I have a combo in WebUserControl.ascx page. I have used it onto 2 different .aspx pages.
In my one .aspx page I have a Label. If I select one item from combo then the Label text will be change according to combo selected value.
[Combo is in WebUserControl and Label is in .aspx page.]

Thanks in advance.
Posted

I would suggest you to expose a property in your usercontrol. This property would hold the selected value of combobox. Make the property public such that it is accessible outside.

Now, in your ASPX page, use the usercontrolname.exposedProerty whenever needed. Thus, accessing the value of usercontrol in ASPX with type-safe form.
 
Share this answer
 
Comments
Albin Abel 19-Feb-11 8:44am    
my 5
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

XML
<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

C#
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.
 
Share this answer
 
v2
Comments
Sandeep Mewara 20-Feb-11 14:35pm    
Why to go with events when a simple property would do for you?
_Ashish 21-Feb-11 2:27am    
***If I select one item from combo then the Label text will be change according to combo selected.***

Question asks for this. property is good enough as long as there is some postback button.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900