Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In aspx page (details.aspx) i have a user control called attach.ascx

I have a dropdown control in details.aspx page. what i require is i need to get dropdown value in attach.ascx user control.

How to achieve?

thanks in advance.
Posted

This sounds like a bad design - a user control should be self-contained, and shouldn't rely on any details of the parent control or page.

If you can't change the design, then you'll want to use an interface to expose the value you need. Have the parent control or page implement the interface, and the code in your user control can walk up the control tree to find the nearest ancestor which implements that interface:
C#
Control container = NamingContainer;
var valueProvider = container as ISomeCustomInterface;
while (container != null && valueProvider == null)
{
    container = container.NamingContainer;
    valueProvider = container as ISomeCustomInterface;
}
if (valueProvider != null)
{
    string theValue = valueProvider.TheValue;
    // Do something with the value...
}
 
Share this answer
 
As mentioned this is bad design. The user control should accept a value, and you can then "do work" when that value is set, or when a method on it is called etc. The aspx page is the parent and the control is the child, and interaction should be one way; the parent controls the child, the child doesn't control the parent. This allows your control to sit happily on any page, even one without the desired combo. If you want any kind of child->parent communication that is done using events, and that way the parent cab choose to subscribe to what they are interested in, and again your control will work on any page.

Attach.ascx.cs
C#
public partial class Attach : System.Web.UI.UserControl
{
    public string Text { get; set; }

    public delegate void MyProcessHandler(string data);
    public event MyProcessHandler MyProcess;

    public void DoSomething(int x)
    {
        // this function is void, but it can return something if you want it to
        // this example doesn't make much sense, it is just showing the principals

        string data = Text + " " + (x * 2).ToString();

        // raise an event to anyone that is subscribed
        if (MyProcess != null)
        {
            MyProcess(data);
        }

    }
}


Page;

XML
<asp:DropDownList ID="DropDownData" runat="server">
    <asp:ListItem Text="One" Value="1" />
    <asp:ListItem Text="Two" Value="2" />
    <asp:ListItem Text="Three" Value="3" />
</asp:DropDownList>

<ctl:Attach ID="Attach1" runat="server" />

<asp:Button ID="Button1" runat="server" Text="Click" OnClick="Button1_Click" />

<asp:Label ID="Label1" runat="server" />


C#
protected void Button1_Click(object sender, EventArgs e)
{
    Attach1.MyProcess += new Attach.MyProcessHandler(Attach1_MyProcess);
    Attach1.Text = DropDownData.SelectedValue;
    Attach1.DoSomething(int.Parse(Attach1.Text));
}

protected void Attach1_MyProcess(string data)
{
    Label1.Text = data;
}
 
Share this answer
 

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