Michells Mesterværk

Kommunikation imellem 2 UserControls (WebForms)

by Administrator 3. November 2009 13:17

Har du nogensinde forsøgt at få 2 eller flere UserControls til at “tale” med hinanden i WebForms frameworket ?

En UserControl skal bruge en side som “host”, og på den måde kan vi få siden til at være en slags event-proxy, og styre de events der bliver kastet af de enkelte UserControls.

Du skal bruge 2 UserControls (.ascx) og en 1 WebForm (.aspx).

Tilføj en PlaceHolder til din aspx side, dernæst de ønskede kontroller i dine .ascx filer. Jeg har brugt en CheckBox kontrol i den ene UserControl og en DropDownList i den anden. Nu er meningen, at når jeg vinger CheckBoxen af, så skal min DropDownList populeres med navne. Jeg lader min aspx side styre hvordan dette bliver gjort.

Tilføj dette til din aspx fil:

<%@ Register Src="~/User1.ascx" TagName="uc1" TagPrefix="DMF" %>
<%@ Register Src="~/User2.ascx" TagName="uc2" TagPrefix="DMF" %>

Dernæst skal du tilføje noget kode i din .aspx fils code-behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
private User2 ctrl2;

protected void Page_Load(object sender, EventArgs e)
{
//load first and second UC
User1 ctrl = (User1)LoadControl("User1.ascx");
ctrl2 = (User2)LoadControl("User2.ascx");

//adding the UC's
phControls.Controls.Add(ctrl);
phControls.Controls.Add(ctrl2);

//find checkbox from first UC
CheckBox cbCheck = (CheckBox)FindControl("cbCheck", ctrl);

if (cbCheck != null) {
//add event handler for the first UC
cbCheck.CheckedChanged += new EventHandler(cbCheck_CheckedChanged);
}
}

protected void cbCheck_CheckedChanged(object sender, EventArgs e)
{
//find dropdown in second UC
DropDownList ddlNames = (DropDownList)FindControl("ddlNames", ctrl2);

if (ddlNames != null) {
//add item(s) to the dropdown in the second UC
ddlNames.Items.Add(new ListItem() { Text = "daniel", Value = "1" });
}
}

/// <summary>
/// Recursive FindControl
/// </summary>
/// <param name="controlName"></param>
/// <param name="control"></param>
/// <returns></returns>
private Control FindControl(String controlName, Control control)
{
if (control.HasControls())
{
foreach (Control item in control.Controls)
{
if (item.ID == controlName)
{
return item;
}
FindControl(item.ID, item);
}
}
return null;
}
}

På den måde kan du lade dine UserControls blive “styret” fra en reel side. Det kan der være fordele ved, men man skal også være en smule forsigtig med ikke at forurene ens aspx sider med logik der rent faktisk bør være i selve UserControlen.

Du kan downloade koden til eksemplet her.

Tags:

Comments

2/14/2010 1:38:20 PM #

اخبار

Thank you very much for the possibility to have a look into the office. Nice boy, he has got the talent to explain.

اخبار Egypt

2/28/2010 3:14:39 AM #

stock trading

I was wondering if you would like to be a guest poster on my website? and in exchange you could include a link your post? Please let me know when you get a chance and I will send you my contact details - thanks.

stock trading United States

2/28/2010 11:30:45 AM #

money

Awesome blog it's not often that I comment but I felt you deserve it.

money United States

3/4/2010 6:21:52 AM #

hd movies

I enjoy streaming movies online, it is way easier than going to the theaters.

hd movies United States

3/4/2010 8:12:33 AM #

watch movies online

I like streaming movies online, it is way easier than going to the theaters.

watch movies online United States

3/9/2010 8:42:17 PM #

WoW Mobile

Do you know which mobile phone companies allow tethering?

WoW Mobile United States

3/10/2010 12:00:34 AM #

WoW Mobile

Do you know which mobile phone companies allow tethering?

WoW Mobile United States

3/14/2010 11:23:21 PM #

ssk sorgulama

This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.

ssk sorgulama United States

3/17/2010 11:29:20 AM #

arac sorgulama

I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!

arac sorgulama United States

3/20/2010 3:22:17 PM #

ehliyet

Excellent post.I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work.

ehliyet Denmark

4/4/2010 12:42:38 PM #

download word games

Certainly, the post is absolutely the most desirable on this precious field. I fit in with each of your results and will desperately watch for your coming up-dates. Saying thankx will not likely just be sufficient, for the increadible clarity in your writing. I will quickly get your rss to stay privy of any updates. Delightful work and much success in your business endeavors! Best wishes, ..

download word games United States

Add comment


(Will show your Gravatar icon)

(De 3 specielle karaktere i det danske alfabet?)
  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen