国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

關于兩個自定義控件的取值問題及接口的應用

2024-07-10 13:17:59
字體:
來源:轉載
供稿:網友
“一個.aspx的頁面中,用到了兩個用戶控件,其中想做的到A控件有一個按鈕,點擊的時候獲取到B控件中的一個textbox的值。

因為在生成的時候名字會改變,用findcontrol的時候名字該如何寫呢?
另外像這種問題有幾種解決的辦法呢?”

論壇上看到這個問題,Insus.NET提供自己的解決方法,先看看解決運行的效果:

關于兩個自定義控件的取值問題及接口的應用

 
首先創建一個站點,然后創建兩個用戶控件,一個是UcA,一個是UcB。 在UcB的控件上拉一個TextBox。

復制代碼 代碼如下:


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UcB.ascx.cs" Inherits="UcB" %>
<asp:TextBox 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 runat="server">
<fieldset>
<legend>UcA
</legend>
<uc1:UcA runat="server" />
</fieldset>
<fieldset>
<legend>UcB
</legend>
<uc2:UcB 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>");
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长白| 禹州市| 定兴县| 祁连县| 额敏县| 滨州市| 南投县| 邓州市| 丹凤县| 昌宁县| 洛宁县| 雷山县| 习水县| 巴南区| 东光县| 焉耆| 高碑店市| 长垣县| 柳江县| 紫阳县| 文登市| 武邑县| 古丈县| 务川| 太仓市| 佳木斯市| 独山县| 自贡市| 沙田区| 延寿县| 蛟河市| 上高县| 山西省| 都匀市| 阳原县| 青海省| 板桥市| 宁德市| 十堰市| 岗巴县| 陈巴尔虎旗|