问题描述
TestUC.ascx设计code
TestUC.ascx Design Code
<asp:TextBox ID="txtbox1" runat="server" ClientIDMode="Static" placeholder="Enter Some Text" ></asp:TextBox><br />
<asp:Button ID="btn1" runat="server" Text="Click" OnClick="btn1_Click" ClientIDMode="Static" />
Test.aspx的页面code
Test.aspx Page Code
<%@ Register Src="~/WebUserControls/TestUC.ascx" TagName="WebUserControlTest"
TagPrefix="uctest" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cphBody" runat="server">
<asp:Label ID="lbl1" runat="server" >Label</asp:Label>
<uctest:WebUserControlTest ID="ucTest" runat="server"></uctest:WebUserControlTest>
</asp:Content>
输出:
我需要..
第一步:输入一些文字在文本框结果
Step2:接着我请点击请点击按钮
[注:这两个控件绑定自UserControl]结果
第三步:在文本框输入内容的文本是显示标签[注标签present在ASPX页面]
Step1: Enter Some text In Text Box
Step2:Then I Click Click Button[Note: This Two Controls Are Bind From UserControl]
Step3:What Text Entered in TextBox Is Show In label [Note Label Present In Aspx Page]
推荐答案
您将需要有一个自定义事件和放大器;你还需要暴露文本框
的文本
属性的用户控件,就像这样。
You will need to have a custom event & you will also need to expose the Text
property of the TextBox
in your UserControl, like this.
public partial class YourUserControl : UserControl
{
public String Text
{
get
{
return this.txtBox1.Text;
}
//write the setter property if you would like to set the text
//of the TextBox from your aspx page
//set
//{
// this.txtBox1.Text = value;
//}
}
public delegate void TextAppliedEventHandler(Object sender, EventArgs e);
public event TextAppliedEventHandler TextApplied;
protected virtual void OnTextApplied(EventArgs e)
{
//Taking a local copy of the event,
//as events can be subscribed/unsubscribed asynchronously.
//If that happens after the below null check then
//NullReferenceException will be thrown
TextAppliedEventHandler handler = TextApplied;
//Checking if the event has been subscribed or not...
if (handler != null)
handler(this, e);
}
protected void yourUserControlButton_Click(Object sender, EventArgs e)
{
OnTextApplied(EventArgs.Empty);
}
}
然后在你的aspx页面,在这里放置 YourUserControl
(或者你被动态地从背后code添加它),你可以订阅这个事件像这一点。
Then in your aspx page, where you have placed YourUserControl
(OR you are dynamically adding it from the code behind), you can subscribe to this event like this.
protected void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
yourUserControl.TextApplied += new YourUserControl.TextAppliedEventHandler(yourUserControl_TextApplied)
}
}
您可以使用用户控件的自定义事件在你的网页是这样的。
You can use the custom event of the user control in your page like this.
protected void yourUserControl_TextApplied(Object sender, EventArgs e)
{
yourLabelInYourPage.Text = yourUserControl.Text;
}
和你做......
修改:您可以重命名控制与放大器;活动,只要你喜欢。我已经使用的名称仅用于举例的目的。
EDIT : You can rename the Controls & Events as you like. I have used the names only for the example purpose.
修改:在网站上的项目,如果要添加用户控件动态然后,
你可能需要在你的页面的命名空间 ASP
,是这样的。
EDIT : In website projects, if you want to add your user control dynamically then,you might need to include the namespace ASP
in your page, like this.
using ASP;
和添加此指令
在ASPX标记你的页面。
And add this Directive
in your page in the aspx markup.
<%@ Reference Control="~/PathToYourUserControl/YourUserControl.ascx" %>
这篇关于如何通过文本框的值present在用户控件要ASPX页面标签通过点击按钮在用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!