【问题标题】:ASP - Send data from UserControl to another oneASP - 将数据从 UserControl 发送到另一个
【发布时间】:2017-03-17 05:08:37
【问题描述】:

我正在与您联系,因为我不知道如何将数据从 UserControl 发送到另一个。

上下文:

第一个 UserControl,UrlControl (uc1) 有一个 TextBox 和一个 LinkBut​​ton,它们允许用户将 URL 添加到事件(到我的数据库中)。

另一个用户控件,TileColorControl (uc2) 允许用户在他的事件中添加一些颜色。添加的颜色可以附加到所有 url 或特定 url。 在此控件中,有一个 DropDownList 包含 Event 的不同 url。

我想要的是:当我通过单击“添加”(在 uc1 中)创建新 URL 时,DropDownList(在 uc2 中)使用更新的数据自动刷新自己。

This is what I want to do

提前感谢您的帮助

UrlControl (uc1) 代码

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillUrls();
        }
    }
    private void FillUrls()
    {
        RepeaterUrl.DataSource = UrlController.FindByEvent(EventId);
        RepeaterUrl.DataBind();
    }
    protected void LinkButtonSaveNew_Click(object sender, EventArgs e)
    {
        string txtUrl = TextBoxNewUrl.Text;
        if (UrlController.IsValidUrl(txtUrl))
        {
            DMCAccess.Event evt = EventController.FindByUrl(txtUrl);
            if (evt != null)
            {
                btnMsgFailed.Visible = false;lbInfo.Visible = false;
                showMsgAlreadyUsed(evt);
            }
            if (!UrlController.IsAlreadyExist(txtUrl))
            {
                Guid userGuid =                      PersonsController.GetPersonByUserGuid((Guid)Membership.GetUser().ProviderUserKey).Guid;
                Url url = new Url
                {
                    Guid = Guid.NewGuid(),
                    UrlLink = TextBoxNewUrl.Text,
                    IsDeleted = false,
                    EventGuid = EventId,
                    CreationByGuid = userGuid,
                    ModificationByGuid = userGuid,
                    CreationDate = DateTime.Now,
                    ModificationDate = DateTime.Now
                };
                UrlController.Create(url);
                msgSuccess("http://" + url.UrlLink + " was created");
                FillUrls();
            }
        }
        else
        {
            HyperLinkViewEvent.Visible = false;
            msgFailed("Please, enter a valid URL.");
        }
    }

TileColorControl (uc2) 代码

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillColors();
            FillUrls();
        }
    }

    protected void RepeaterColor_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DropDownList ddlUrls = (DropDownList)e.Item.FindControl("ddlUrls");
            ddlUrls.DataSource = urls();
            ddlUrls.DataTextField = "Value";
            ddlUrls.DataValueField = "Key";
            ddlUrls.DataBind();
            ddlUrls.Items.Insert(0, new ListItem("Default", Guid.Empty.ToString()));

            Guid? urlGuid = ((DMCAccess.TileColor)e.Item.DataItem).UrlGuid;
            if (urlGuid.HasValue)
            {
                ddlUrls.SelectedIndex = ddlUrls.Items.IndexOf(ddlUrls.Items.FindByValue(urlGuid.Value.ToString()));
            }
        }
    }

    public void FillUrls()
    {
        DropDownListBoxUrl.DataSource = urls();
        DropDownListBoxUrl.DataTextField = "Value";
        DropDownListBoxUrl.DataValueField = "Key";
        DropDownListBoxUrl.DataBind();
        DropDownListBoxUrl.Items.Insert(0, new ListItem("Default", Guid.Empty.ToString()));
    }

    private Dictionary<Guid, string> urls()
    {
        Dictionary<Guid, string> dUrls = new Dictionary<Guid, string>();
        foreach (Url u in UrlController.FindByEvent(EventId))
        {
            dUrls.Add(u.Guid, u.UrlLink);
        }
        return dUrls;
    }

    private void FillColors()
    {
        TextBoxNewIdColor.Text = TileColorController.MaxColorIdByEvent(EventId).ToString();
        if (UrlController.FindByEvent(EventId).Count < 1)
        {
            upNoWebsite.Visible = true;
            upColor.Visible = false;
        }
        RepeaterColor.DataSource = TileColorController.FindByEvent(EventId);
        RepeaterColor.DataBind();
    }

【问题讨论】:

    标签: c# asp.net user-controls updatepanel panel


    【解决方案1】:

    您可以使用事件冒泡在控件层次结构的父级连接这两个控件。

    我想你的控件都包含在另一个父控件或页面中,像这样:

    <control:UrlControl runat="server" id="uc1" />
    <control:TileColorControl runat="server" id="uc2" />
    

    首先你必须在 uc1 中声明一个事件——例如"OnNewUrl":

    public event EventHandler OnNewUrl;
    

    然后将此事件绑定到“主”页面中具有所需功能的方法:

    protected void Page_Load(object sender, EventArgs e)
    {
        uc1.OnNewUrl += RebindTileColorControl;
    }
    
    protected void RebindTileColorControl(object sender, EventArgs e)
    {
      // rebind uc2 here ...
    }
    

    有关事件冒泡的更多信息,您可以在 MSDN herehere 中找到。

    【讨论】:

      【解决方案2】:

      在我看来,UrlControl 应该知道那里的 TileColorControl 并且应该告诉它更新。

      将 TileColorControl 类型的公共变量/属性添加到 UrlControl 类

      public TileColorControl AssociatedTileColor { get; set; }
      

      在父控件/页面上,您将对 uc2(TileColorControl)的引用分配给 uc1 的变量/属性

      uc1.AssociatedTileColor = uc2;
      

      在UrlControl中可以使用AssociatedTileColor来调用uc1的FillUrls方法

      if (uc1.AssociatedTileColor != null)
      {
          uc1.AssociatedTileColor.FillUrls();
      }
      

      【讨论】:

        【解决方案3】:

        我终于找到了方法:

        TileColorControl:没什么可改变的 Event.aspx(包含用户控件的页面),添加公共方法:

        public void FillUrls()
        {
          TileColorControl.FillUrls();
        }
        

        UrlControl:在函数 Add 或 Update 添加行:

        DMC.Event currentEvent = (DMC.Event)Page;
        currentEvent.FillUrls();
        

        完成了,它会更新我的 DropDownList !

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-09-27
          • 1970-01-01
          • 2018-06-21
          • 1970-01-01
          • 2012-04-14
          • 1970-01-01
          • 1970-01-01
          • 2017-12-17
          相关资源
          最近更新 更多