關(guān)于兩個自定義控件的取值問題及接口的應(yīng)用
更新時間:2013年01月27日 12:34:31 作者:
一個.aspx的頁面中,用到了兩個用戶控件,其中想做的到A控件有一個按鈕,點擊的時候獲取到B控件中的一個textbox的值想必大家會使用findcontrol獲取控件吧,而在生成的時候名字是不確定的,那么如何書寫呢?接下來為您提供詳細(xì)的解決方法,感興趣的朋友可以了解下啊
“一個.aspx的頁面中,用到了兩個用戶控件,其中想做的到A控件有一個按鈕,點擊的時候獲取到B控件中的一個textbox的值。
因為在生成的時候名字會改變,用findcontrol的時候名字該如何寫呢?
另外像這種問題有幾種解決的辦法呢?”
論壇上看到這個問題,Insus.NET提供自己的解決方法,先看看解決運(yùn)行的效果:
首先創(chuàng)建一個站點,然后創(chuàng)建兩個用戶控件,一個是UcA,一個是UcB。 在UcB的控件上拉一個TextBox。
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UcB.ascx.cs" Inherits="UcB" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
創(chuàng)建一個接口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實現(xiàn)這個接口,接口返回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)建一個aspx頁面,如Default.aspx,切換至設(shè)計模式,把兩個用戶控件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)建一個接口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();
}
}
接口創(chuàng)建好之后,在Default.aspx.cs實現(xiàn)這個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>");
}
}
因為在生成的時候名字會改變,用findcontrol的時候名字該如何寫呢?
另外像這種問題有幾種解決的辦法呢?”
論壇上看到這個問題,Insus.NET提供自己的解決方法,先看看解決運(yùn)行的效果:
首先創(chuàng)建一個站點,然后創(chuàng)建兩個用戶控件,一個是UcA,一個是UcB。 在UcB的控件上拉一個TextBox。
復(fù)制代碼 代碼如下:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UcB.ascx.cs" Inherits="UcB" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
創(chuàng)建一個接口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實現(xiàn)這個接口,接口返回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)建一個aspx頁面,如Default.aspx,切換至設(shè)計模式,把兩個用戶控件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)建一個接口Interface,目的是為了獲取UcB這個用戶控件。
復(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實現(xiàn)這個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;
}
}
到最后,我們在UcA這個用戶控件的按鈕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>");
}
}
相關(guān)文章
asp.net下 jquery jason 高效傳輸數(shù)據(jù)
jquery jason 高效傳輸數(shù)據(jù)轉(zhuǎn)自網(wǎng)上稍有修改2009-03-03
ASP.NET中控件的EnableViewState屬性及徹底禁用
如果我們在開發(fā)Web應(yīng)用程序時,某些控件是不需要接受用戶的操作或只需要接受一次操作的時候,我們可以將這些控件的EnableViewState屬性改為false,這樣可以優(yōu)化我們的程序,提高網(wǎng)絡(luò)訪問的速度。2016-06-06
淺談AjaxPro.dll,asp.net 前臺js調(diào)用后臺方法
這篇文章主要介紹了淺談AjaxPro.dll,asp.net 前臺js調(diào)用后臺方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
asp.net為網(wǎng)頁動態(tài)添加關(guān)鍵詞的方法
這篇文章主要介紹了asp.net為網(wǎng)頁動態(tài)添加關(guān)鍵詞的方法,可實現(xiàn)動態(tài)添加keyword meta的功能,非常具有實用價值,需要的朋友可以參考下2015-04-04
Asp.net MVC 對所有用戶輸入的字符串字段做Trim處理的方法
這篇文章主要介紹了Asp.net MVC 如何對所有用戶輸入的字符串字段做Trim處理,需要的朋友可以參考下2017-06-06

