开发者

Passing User Control Class to Host Page

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. 开发者_JS百科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);


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:

  • Getting data from child controls loaded programmatically
  • How to change the value of a control in a MasterPage.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜