简体   繁体   English

如何将数据从一个用户控件传递到另一个用户控件都放置在default.aspx中

[英]how to pass the data from one user control to another user Control both are placed in default.aspx

how to pass the data from one user control to another user Control both are placed in default.aspx 如何将数据从一个用户控件传递到另一个用户控件都放置在default.aspx中

i have two user controls ie uc1.ascx and uc2.ascx both are placed into Default.aspx page . 我有两个用户控件,即uc1.ascxuc2.ascx都放置在Default.aspx页面中。

uc1.ascx contains --> one Dropdown List ,

uc2.ascx contains ---> Repeater control

while Dropdown list selected index changed , i want to chatng the uc2.aspx --> Repeater control data based on dropdown Selection . 虽然下拉列表选择的索引已更改,但我想基于dropdown Selection聊天uc2.aspx > Repeater control数据。

Thanks 谢谢

you can do this with event BubbleEvent.. 您可以通过事件BubbleEvent执行此操作。

my event args I using it for transport selected data 我的事件参数我将其用于传输所选数据

public class MyEventArgs : EventArgs
{
    public string Value { get; set; }
    public string Text { get; set; }
}

user control 1 named uc1 用户控件1名为uc1

public partial class uc1 : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void uc1Dorpdown_SelectedIndexChanged(object sender, EventArgs e)
    {
        // event bubbling
        RaiseBubbleEvent(this, new MyEventArgs
        {
            Value = this.uc1Dorpdown.SelectedItem.Value,
            Text = this.uc1Dorpdown.SelectedItem.Text
        });
    }
}

user control 1 dropdown 用户控件1下拉菜单

<asp:DropDownList ID="uc1Dorpdown" runat="server" AutoPostBack="true" OnSelectedIndexChanged="uc1Dorpdown_SelectedIndexChanged">
<asp:ListItem Value="1" Text="Item 1" ></asp:ListItem>
<asp:ListItem Value="2" Text="Item 2" ></asp:ListItem>
<asp:ListItem Value="3" Text="Item 3" ></asp:ListItem>
<asp:ListItem Value="4" Text="Item 4" ></asp:ListItem>
<asp:ListItem Value="5" Text="Item 5" ></asp:ListItem>
<asp:ListItem Value="6" Text="Item 6" ></asp:ListItem>
</asp:DropDownList>

user control 2 named uc2 用户控件2名为uc2

public partial class uc2 : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    // public is important
    public void RepeaterUpdate(MyEventArgs newValue)
    {
       // call repeater update code
    }
}

default aspx 默认aspx

    protected override bool OnBubbleEvent(object source, EventArgs args)
    {
        //handle bubbled event and call uc2 method
        // MyControls.uc1 is uc type -- u can check eventargs type
        if (source is MyControls.uc1)
        {
            this.uc21.RepeaterUpdate((MyEventArgs)args);
        }
        return base.OnBubbleEvent(source, args);
    }

One way of doing it would be to create a public property of the page embedding the two user controls and use this property to send data between the two of them. 一种实现方法是创建嵌入两个用户控件的页面的公共属性,并使用该属性在两个用户控件之间发送数据。

For this to work you need to cast the this.parent property in the user controls to the type of you page before you can get/set the value. 为此,您需要在用户控件中将this.parent属性强制转换为页面类型,然后才能获取/设置该值。 Something like 就像是

(this.parent as MyPage).MyProperty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM