当用户控件有异动时,网页某部位作出相应变化
很多情况之下,使用用户控件ASCX时,是因为不止一个网页有相同的一个功能。因此让把这部分抽出来,放置于一个用户控件。然后在网页需要用户控件,拉进去即可,但是Insus.NET不单单只是呈现,而是想做更多的交互,也就是说用户控件与网页之间的交互。为了更好分享这个交互功能,Insus.NET现列举一个例子。只要明白其中的原理,很多相似功能,你可以应用得到。
上面演示中,用户控件动态产生了一个DropDownList Web控件,在下拉式菜单选项变化时,去更新网页的数据。Insus.NET把用户控件与网页当作对象,不同对象之间作相同的动作,非接口来实现不可。
ISetable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ISetable
/// </summary>
namespace Insus.NET
{
public interface ISetable
{
void SetValue(string key,string value);
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ISetable
/// </summary>
namespace Insus.NET
{
public interface ISetable
{
void SetValue(string key,string value);
}
}
在网页的.cs件实作这个接口:
View Code
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class _Default : System.Web.UI.Page, ISetable
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void SetValue(string key, string value)
{
if (string.IsNullOrEmpty(key.ToString()))
{
this.Image1.Visible = false;
this.Label1.Visible = false;
}
else
{
this.Image1.Visible = true;
this.Image1.ImageUrl = "~/Images/" + key.ToString() + ".gif";
this.Label1.Text = value.ToString();
}
}
}
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class _Default : System.Web.UI.Page, ISetable
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void SetValue(string key, string value)
{
if (string.IsNullOrEmpty(key.ToString()))
{
this.Image1.Visible = false;
this.Label1.Visible = false;
}
else
{
this.Image1.Visible = true;
this.Image1.ImageUrl = "~/Images/" + key.ToString() + ".gif";
this.Label1.Text = value.ToString();
}
}
}
在用户控件:
ISetable obj = (ISetable)this.Page;
obj.SetValue(ddl.SelectedItem.Value,ddl.SelectedItem.Text);
obj.SetValue(ddl.SelectedItem.Value,ddl.SelectedItem.Text);
演示源程序,环境 Framework4.5 + C#:
http://download.cnblogs.com/insus/ASPDOTNET/Uc_pge_upt_data.rar