关于两个自定义控件的取值问题及接口的应用

所属分类: 网络编程 / ASP.NET 阅读数: 2057
收藏 0 赞 0 分享
“一个.aspx的页面中,用到了两个用户控件,其中想做的到A控件有一个按钮,点击的时候获取到B控件中的一个textbox的值。

因为在生成的时候名字会改变,用findcontrol的时候名字该如何写呢?
另外像这种问题有几种解决的办法呢?”

论坛上看到这个问题,Insus.NET提供自己的解决方法,先看看解决运行的效果:
 
首先创建一个站点,然后创建两个用户控件,一个是UcA,一个是UcB。 在UcB的控件上拉一个TextBox。
复制代码 代码如下:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UcB.ascx.cs" Inherits="UcB" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

创建一个接口IGetValue:
复制代码 代码如下:

IGetValue.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for IGetValue
/// </summary>
namespace Insus.NET
{
public interface IGetValue
{
string GetValue();
}
}

接下来,用户控件UcB实现这个接口,接口返回TextBox的Text值。
复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class UcB : System.Web.UI.UserControl,IGetValue
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string GetValue()
{
return this.TextBox1.Text.Trim();
}
}

创建一个aspx页面,如Default.aspx,切换至设计模式,把两个用户控件UcA,UcB拉至Default.aspx:
复制代码 代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="UcA.ascx" TagName="UcA" TagPrefix="uc1" %>
<%@ Register Src="UcB.ascx" TagName="UcB" TagPrefix="uc2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<fieldset>
<legend>UcA
</legend>
<uc1:UcA ID="UcA1" runat="server" />
</fieldset>
<fieldset>
<legend>UcB
</legend>
<uc2:UcB ID="UcB1" runat="server" />
</fieldset>
</form>
</body>
</html>

到这里,再创建一个接口Interface,目的是为了获取UcB这个用户控件。
复制代码 代码如下:

IGetUserControl.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
/// <summary>
/// Summary description for IGetUserControl
/// </summary>
namespace Insus.NET
{
public interface IGetUserControl
{
UserControl GetUc();
}
}

接口创建好之后,在Default.aspx.cs实现这个IGetUserControl接口。
复制代码 代码如下:

using System;
using System.Collections.Generic;
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,IGetUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public UserControl GetUc()
{
return this.UcB1;
}
}

到最后,我们在UcA这个用户控件的按钮Click事件写:
复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class UcA : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
IGetUserControl ucb = (IGetUserControl)this.Page;
IGetValue value = (IGetValue)ucb.GetUc();
Response.Write("<scr" + "ipt>alert('" + value.GetValue() + "')</scr" + "ipt>");
}
}
更多精彩内容其他人还在看

asp.net网站首页根据IP自动跳转指定页面的示例

本文介绍的程序主要实现根据IP地址或地址段或IP所在城市进行自动跳转到指定页面的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

asp.net无法获取iis目录的问题解决方法

本文介绍了Asp.Net无法获取IIS拾取目录的解决办法,需要的朋友可以参考下
收藏 0 赞 0 分享

.net中 发送邮件内容嵌入图片的具体实例

这篇文章主要介绍了.net中 发送邮件内容嵌入图片的具体实例,需要的朋友可以参考下
收藏 0 赞 0 分享

在.net中用CheckBoxList实现单选

用CheckBoxList实现单选的原因是我觉得CheckBoxList控件页面展示效果要好看一些,需要的朋友可以参考下
收藏 0 赞 0 分享

ASP.NET―001:GridView绑定List、页面返回值具体实现

这篇文章主要介绍了ASP.NET―GridView绑定List、页面返回值具体实现,需要的朋友可以参考下
收藏 0 赞 0 分享

Ajax实现异步刷新验证用户名是否已存在的具体方法

由于要做一个注册页面,看到许多网站上都是使用Ajax异步刷新验证用户名是否可用的,所以自己也动手做一个小实例
收藏 0 赞 0 分享

ASP.NET汉字转拼音 - 输入汉字获取其拼音的具体实现

这篇文章主要介绍了ASP.NET汉字转拼音 - 输入汉字获取其拼音的具体实现,需要的朋友可以参考下
收藏 0 赞 0 分享

.Net消息队列的使用方法

这篇文章主要介绍了.Net消息队列的使用方法,需要的朋友可以参考下
收藏 0 赞 0 分享

C# web api返回类型设置为json的两种方法

web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面为大家介绍几种不错的方法
收藏 0 赞 0 分享

asp.net获取网站目录物理路径示例

这篇文章主要介绍了asp.net获取网站目录物理路径的方法,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多