简体   繁体   English

如何通过单击 UserControl 中的按钮将 UserControl 中存在的文本框值传递到 Aspx 页面标签

[英]How To Pass Textbox Value Present In UserControl To Aspx Page Label By Clicking Button In UserControl

TestUC.ascx Design Code TestUC.ascx 设计代码

  <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 Page Code Test.aspx 页面代码

 <%@ 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>

OutPut:输出:

在此处输入图片说明

I Need ..我需要..

Step1: Enter Some text In Text Box步骤 1:在文本框中输入一些文本
Step2:Then I Click Click Button [Note: This Two Controls Are Bind From UserControl] Step2:然后我点击点击按钮【注意:这两个控件是从UserControl绑定的】
Step3:What Text Entered in TextBox Is Show In label [Note Label Present In Aspx Page] Step3:在TextBox中输入的文字显示在标签中[注意标签出现在Aspx页面]

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.您将需要一个自定义事件,并且您还需要像这样在 UserControl 中公开TextBoxText属性。

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);
    }
}

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.然后在你的 aspx 页面中,你放置了YourUserControl (或者你从后面的代码动态添加它),你可以像这样订阅这个事件。

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;
}

And you are done...你已经完成了......

EDIT : You can rename the Controls & Events as you like.编辑:您可以根据需要重命名控件和事件。 I have used the names only for the example purpose.我仅将名称用于示例目的。

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.编辑:在网站项目中,如果要动态添加用户控件,则可能需要在页面中包含命名空间ASP ,如下所示。

using ASP;

And add this Directive in your page in the aspx markup.并在您的页面中的 aspx 标记中添加此Directive

<%@ Reference Control="~/PathToYourUserControl/YourUserControl.ascx" %>

other solution : create an event in the usercontrol, which is called in the button click.其他解决方案:在用户控件中创建一个事件,该事件在按钮单击中调用。

Subscribe to this event in the codebehind of the aspx page.在 aspx 页面的代码隐藏中订阅此事件。 that way you can update your interface only if a value is provided.这样,您只能在提供值时更新您的界面。

a little more complicated, but you could re-use this logic to more complex control / parent control feature in the future.稍微复杂一点,但您可以在将来将此逻辑重新用于更复杂的控制/父控制功能。

i can add code snippet if asked如果被问到,我可以添加代码片段

This Answer Is Prepared By Help Of @Devraj Gadhavi i Edited Some Code .这个答案是在@Devraj Gadhavi 的帮助下准备的,我编辑了一些代码。

UserControl Page 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" />

UserControl Page Code用户控制页面代码

 public partial class TestUC : System.Web.UI.UserControl
{

    public String Text
    {
        get
        {
            return this.txtbox1.Text;
        }

    }

    public delegate void TextAppliedEventHandler(Object sender, EventArgs e);
    public event EventHandler TextApplied;

    protected virtual void OnTextApplied(EventArgs e)
    {

        if (TextApplied != null)
            TextApplied(this, e);
    }



    protected void btn1_Click(object sender, EventArgs e)
    {
        OnTextApplied(EventArgs.Empty);
    }
}

Aspx Page Design Code Aspx 页面设计代码

<%@ 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>

Aspx Cs File Code Aspx Cs 文件代码

public partial class Test2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ucTest.TextApplied += new EventHandler(ucTest_TextApplied);


    }

    protected void ucTest_TextApplied(Object sender, EventArgs e)
    {
        lbl1.Text = ucTest.Text;

    }
}

Another method ,If you don't want to expose the Text property of the TextBox in your UserControl Just use method "UserControl.FindControl("Id Of your Textbox which is present in user control")" in your Case WebUserControlTest.FindControl("txtbox1").另一种方法,如果您不想在 UserControl 中公开 TextBox 的 Text 属性,只需在 Case WebUserControlTest.FindControl(" txtbox1”)。

And below is the simpler way to register an event handler on the parent web form's code behind.下面是在父 Web 表单后面的代码上注册事件处理程序的更简单方法。

Code goes as below for parent form asxp.cs父表单 asxp.cs 的代码如下

 protected override void OnInit(EventArgs e)
{
    //find the button control within the user control
    Button button = (Button)WebUserControlTest.FindControl("btn1");
    //wire up event handler
    button.Click += new EventHandler(button_Click);
    base.OnInit(e);
}

void button_Click(object sender, EventArgs e)
{
    TextBox txt = (TextBox) WebUserControlTest.FindControl("txtbox1");
   //id of lable which is present in the parent webform 
    lblParentForm.Text=txt.text; 
}

Also, you can achieve this using JavaScript like below (given your code above):此外,您可以使用如下所示的 JavaScript 来实现这一点(给定上面的代码):

<script type="text/javascript">
  function getUCTextboxValue() {
     let txtName = document.getElementById('<%=ucTest.FindControl("txtbox1").ClientID %>');
     let lbl = document.getElementById('<%=lbl1.ClientID %>');         
     lbl.innerText = txtName.value
  }

Also, from the parent page, you can add a HiddenField and save the textbox value on it (using the JavaScript code above) then catch its value from code behind.此外,从父页面,您可以添加一个 HiddenField 并将文本框值保存在其上(使用上面的 JavaScript 代码),然后从后面的代码中捕获其值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM