【发布时间】:2017-03-17 05:08:37
【问题描述】:
我正在与您联系,因为我不知道如何将数据从 UserControl 发送到另一个。
上下文:
第一个 UserControl,UrlControl (uc1) 有一个 TextBox 和一个 LinkButton,它们允许用户将 URL 添加到事件(到我的数据库中)。
另一个用户控件,TileColorControl (uc2) 允许用户在他的事件中添加一些颜色。添加的颜色可以附加到所有 url 或特定 url。 在此控件中,有一个 DropDownList 包含 Event 的不同 url。
我想要的是:当我通过单击“添加”(在 uc1 中)创建新 URL 时,DropDownList(在 uc2 中)使用更新的数据自动刷新自己。
提前感谢您的帮助
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