如何在c#中从usercontrol中找到aspx页面的控件 [英] how to find a control of aspx page from a usercontrol in c#

查看:112
本文介绍了如何在c#中从usercontrol中找到aspx页面的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在aspx页面(details.aspx)中我有一个名为attach.ascx的用户控件



我在details.aspx页面中有一个下拉控件。我需要的是我需要在attach.ascx用户控件中获取下拉值。



如何实现?



提前谢谢。

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.

推荐答案

这听起来像是一个糟糕的设计 - 用户控件应该是自包含的,不应该依赖于任何细节父控件或页面。



如果您无法更改设计,那么您将需要使用界面来公开您需要的值。让父控件或页面实现接口,并且用户控件中的代码可以向上走控制树以找到实现该接口的最近的祖先:

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:
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...
}


如上所述,这是糟糕的设计。用户控件应该接受一个值,然后您可以在设置该值时工作,或者在调用其上的方法时等.aspx页面是父级,控件是子级,并且交互应该是一个办法;父控制子,父控制父。这使您的控件可以快乐地放在任何页面上,即使是没有所需组合的页面也是如此。如果您想要使用事件完成任何类型的孩子 - >父母沟通,那么父母驾驶室选择订阅他们感兴趣的内容,并且您的控件将再次适用于任何页面。



Attach.ascx.cs

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





Page;

<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" />







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;
}


这篇关于如何在c#中从usercontrol中找到aspx页面的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆