欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于兩個(gè)自定義控件的取值問(wèn)題及接口的應(yīng)用

 更新時(shí)間:2013年01月27日 12:34:31   作者:  
一個(gè).aspx的頁(yè)面中,用到了兩個(gè)用戶控件,其中想做的到A控件有一個(gè)按鈕,點(diǎn)擊的時(shí)候獲取到B控件中的一個(gè)textbox的值想必大家會(huì)使用findcontrol獲取控件吧,而在生成的時(shí)候名字是不確定的,那么如何書寫呢?接下來(lái)為您提供詳細(xì)的解決方法,感興趣的朋友可以了解下啊
“一個(gè).aspx的頁(yè)面中,用到了兩個(gè)用戶控件,其中想做的到A控件有一個(gè)按鈕,點(diǎn)擊的時(shí)候獲取到B控件中的一個(gè)textbox的值。

因?yàn)樵谏傻臅r(shí)候名字會(huì)改變,用findcontrol的時(shí)候名字該如何寫呢?
另外像這種問(wèn)題有幾種解決的辦法呢?”

論壇上看到這個(gè)問(wèn)題,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();
}
}

接下來(lái),用戶控件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>");
}
}

相關(guān)文章

最新評(píng)論