關(guān)于兩個(gè)自定義控件的取值問題及接口的應(yīng)用
更新時(shí)間:2013年01月27日 12:34:31 作者:
一個(gè).aspx的頁(yè)面中,用到了兩個(gè)用戶控件,其中想做的到A控件有一個(gè)按鈕,點(diǎn)擊的時(shí)候獲取到B控件中的一個(gè)textbox的值想必大家會(huì)使用findcontrol獲取控件吧,而在生成的時(shí)候名字是不確定的,那么如何書寫呢?接下來為您提供詳細(xì)的解決方法,感興趣的朋友可以了解下啊
“一個(gè).aspx的頁(yè)面中,用到了兩個(gè)用戶控件,其中想做的到A控件有一個(gè)按鈕,點(diǎn)擊的時(shí)候獲取到B控件中的一個(gè)textbox的值。
因?yàn)樵谏傻臅r(shí)候名字會(huì)改變,用findcontrol的時(shí)候名字該如何寫呢?
另外像這種問題有幾種解決的辦法呢?”
論壇上看到這個(gè)問題,Insus.NET提供自己的解決方法,先看看解決運(yùn)行的效果:
首先創(chuàng)建一個(gè)站點(diǎn),然后創(chuàng)建兩個(gè)用戶控件,一個(gè)是UcA,一個(gè)是UcB。 在UcB的控件上拉一個(gè)TextBox。
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UcB.ascx.cs" Inherits="UcB" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
創(chuàng)建一個(gè)接口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實(shí)現(xiàn)這個(gè)接口,接口返回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();
}
}
創(chuàng)建一個(gè)aspx頁(yè)面,如Default.aspx,切換至設(shè)計(jì)模式,把兩個(gè)用戶控件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>
到這里,再創(chuàng)建一個(gè)接口Interface,目的是為了獲取UcB這個(gè)用戶控件。
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();
}
}
接口創(chuàng)建好之后,在Default.aspx.cs實(shí)現(xiàn)這個(gè)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;
}
}
到最后,我們?cè)赨cA這個(gè)用戶控件的按鈕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>");
}
}
因?yàn)樵谏傻臅r(shí)候名字會(huì)改變,用findcontrol的時(shí)候名字該如何寫呢?
另外像這種問題有幾種解決的辦法呢?”
論壇上看到這個(gè)問題,Insus.NET提供自己的解決方法,先看看解決運(yùn)行的效果:
首先創(chuàng)建一個(gè)站點(diǎn),然后創(chuàng)建兩個(gè)用戶控件,一個(gè)是UcA,一個(gè)是UcB。 在UcB的控件上拉一個(gè)TextBox。
復(fù)制代碼 代碼如下:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UcB.ascx.cs" Inherits="UcB" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
創(chuàng)建一個(gè)接口IGetValue:
復(fù)制代碼 代碼如下:
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實(shí)現(xiàn)這個(gè)接口,接口返回TextBox的Text值。
復(fù)制代碼 代碼如下:
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();
}
}
創(chuàng)建一個(gè)aspx頁(yè)面,如Default.aspx,切換至設(shè)計(jì)模式,把兩個(gè)用戶控件UcA,UcB拉至Default.aspx:
復(fù)制代碼 代碼如下:
<%@ 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>
到這里,再創(chuàng)建一個(gè)接口Interface,目的是為了獲取UcB這個(gè)用戶控件。
復(fù)制代碼 代碼如下:
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();
}
}
接口創(chuàng)建好之后,在Default.aspx.cs實(shí)現(xiàn)這個(gè)IGetUserControl接口。
復(fù)制代碼 代碼如下:
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;
}
}
到最后,我們?cè)赨cA這個(gè)用戶控件的按鈕Click事件寫:
復(fù)制代碼 代碼如下:
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>");
}
}
您可能感興趣的文章:
- .Net WInform開發(fā)筆記(三)談?wù)勛灾瓶丶?自定義控件)
- asp.net自定義控件回發(fā)數(shù)據(jù)實(shí)現(xiàn)方案與代碼
- asp.net中使用自定義控件的方式實(shí)現(xiàn)一個(gè)分頁(yè)控件的代碼
- asp.net DropDownList自定義控件,讓你的分類更清晰
- Asp.net 動(dòng)態(tài)加載用戶自定義控件,并轉(zhuǎn)換成HTML代碼
- asp.net 自定義控件實(shí)現(xiàn)無刷新上傳圖片,立即顯示縮略圖,保存圖片縮略圖
- asp.net自定義控件代碼學(xué)習(xí)筆記
- asp.net 虛方法、抽象方法、接口疑問
相關(guān)文章
asp.net下 jquery jason 高效傳輸數(shù)據(jù)
jquery jason 高效傳輸數(shù)據(jù)轉(zhuǎn)自網(wǎng)上稍有修改2009-03-03
ASP.NET中控件的EnableViewState屬性及徹底禁用
如果我們?cè)陂_發(fā)Web應(yīng)用程序時(shí),某些控件是不需要接受用戶的操作或只需要接受一次操作的時(shí)候,我們可以將這些控件的EnableViewState屬性改為false,這樣可以優(yōu)化我們的程序,提高網(wǎng)絡(luò)訪問的速度。2016-06-06
使用Lucene.NET實(shí)現(xiàn)站內(nèi)搜索
提到Lucene,想必大家都有所耳聞,已經(jīng)是數(shù)年前就出現(xiàn)的開源技術(shù)。很多站點(diǎn)都是利用它搭建自己網(wǎng)站的站內(nèi)搜索。由于最近也在做數(shù)據(jù)檢索方面的東西,也學(xué)習(xí)了下Lucene.net的使用。2015-06-06
淺談AjaxPro.dll,asp.net 前臺(tái)js調(diào)用后臺(tái)方法
這篇文章主要介紹了淺談AjaxPro.dll,asp.net 前臺(tái)js調(diào)用后臺(tái)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
.Net解決引用程序集沒有強(qiáng)名稱報(bào)錯(cuò)
這篇文章介紹了.Net解決引用程序集沒有強(qiáng)名稱報(bào)錯(cuò)的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
asp.net為網(wǎng)頁(yè)動(dòng)態(tài)添加關(guān)鍵詞的方法
這篇文章主要介紹了asp.net為網(wǎng)頁(yè)動(dòng)態(tài)添加關(guān)鍵詞的方法,可實(shí)現(xiàn)動(dòng)態(tài)添加keyword meta的功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
Asp.net MVC 對(duì)所有用戶輸入的字符串字段做Trim處理的方法
這篇文章主要介紹了Asp.net MVC 如何對(duì)所有用戶輸入的字符串字段做Trim處理,需要的朋友可以參考下2017-06-06
C#頁(yè)碼導(dǎo)航顯示及算法實(shí)現(xiàn)代碼
C#頁(yè)碼導(dǎo)航算法要求:頁(yè)數(shù)小于等于1時(shí)不顯示;頁(yè)數(shù)大于10時(shí),自動(dòng)縮短,需要的朋友可以了解下2012-12-12

