views:

31

answers:

1

How can I pass user control properties to the page AND make these properties available to all methods on the page (and not just to one method that is fired on a control action, e.g. onControlClick)

I have a set up of essentially 3 pages:

  • user control (ascx/cs)
  • class (cs) - that contains user control properties
  • host page (aspx/cs) - references the user control

The user control consists of 3 interrelated dropdowns. I'm having success passing these dropdown values through a class onto the page via an event that is fired when a user clicks on the dropdown menu. So this way the host page is continously aware of the values in the user control. However, I want the page to use the control's properties (stored in a class) on all of its methods - how do I make this user control class available to all?

Also I'm using ASP.NET and C# by the way.

Here's the Code (not sharing the full code here - just the snippets of a similar code block)

On the ASPX for Menu Host Page:

<linked:LinkMenu2 id="Menu1" runat="server" OnLinkClicked="LinkClicked" />

Host Page (cs):

protected void dropdownclicked(object sender, ddtestEventArgs e)
{
    if (e.Url == "Menu2Host.aspx?product=Furniture")
    {
        lblClick.Text = "This link is not allowed.";
        e.Cancel = true;
    }
    else
    {
        // Allow the redirect, and don't make any changes to the URL.
    }
}

Host Page (aspx)

<asp:dropdowncustom ID="dddone" runat="server" OnddAppClicked="dropdownclicked" />

Control (cs)

public partial class usercontrol_tests_dropdown1 : System.Web.UI.UserControl
{
    public event ddtestEventHandler ddAppClicked;
}

    public void selectapp_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddtestEventArgs args = new ddtestEventArgs(selectlink.SelectedValue);
        ddAppClicked(this, args);
    }

Class:

public class ddtestEventArgs : EventArgs
{
    // Link
    private string link;
    public string Link
    {
        get { return link; }
        set { link = value; }
    }

    public ddtestEventArgs(string link)
    {
    Link = link;
    }
}

public delegate void ddtestEventHandler(object sender, ddtestEventArgs e);
+2  A: 

Hopefully this is what you're after. The best way to do it is to expose your controls as public properties from your user control. So, in your user control, for each drop down list add a property:

public DropDownList DropDown1
{
   get { return dropDownList1; }
}

public DropDownList DropDown2
{
   get { return dropDownList2; }
}

You can do the same for any other properties you want to access on the host page:

public string DropDown1SelectedValue
{
   get { return dropDownList1.SelectedValue; }
   set { dropDownList1.SelectedValue = value; }
}

Then, from your host page you can access the properties through the user control:

string value = UserControl1.DropDown1SelectedValue;

or

string value = UserControl1.DropDownList1.SelectedValue;

Here's a couple of other answered questions that you might find useful as I think (if I've understood correctly) this is what you're doing:

GenericTypeTea
So the idea's basically to set up properties for my user control (in my case 3 properties total for the control for the 3 dropdowns that I have). The page will then be able to reference these properties like on a normal control. My only concern - is this bad design? Because usually when you're using properties you would want the Page to communicate back to the control? But in my case the communication is all about User Control --> Page?
firedrawndagger
So you're saying that you want your UserControl to tell the Page what to do? If so, that's bad design. UserControls should really be standalone and return property values to a page. I.e. The HOST page is in charge and shouldn't take orders.
GenericTypeTea
Essentially yes - the dropdowns set parameters which in turn get a dataset that gets displayed on the page. What should I use instead?
firedrawndagger
You should probably expose the DataSet from the Control as a property. Or have a method, i.e. GetDataSet(); which generates the DataSet based on the selected drop downs.
GenericTypeTea
I actually do have a method similar to GetDataSet which works with SPs to select, update or remove data. So I'm all set in regards to that. But some of the properties that this GetDataSet() needs are determined by the dropdowns in my user control. Also I thought the whole premise of **integrated** user controls was to provide richer interaction between the page and the control?
firedrawndagger
Personally, I never let my user controls do anything to the pages they sit on. My user controls expose data and properties and the page accesses those methods and properties from the user control.
GenericTypeTea
Wow, thanks for answering my questions. I had no idea user controls were going to be that difficult, especially for what I wanted to implement.
firedrawndagger