【问题标题】:Usercontrol action event with TextBox带有文本框的用户控件操作事件
【发布时间】:2014-04-09 13:10:52
【问题描述】:

我是 asp.net 的新手, 我的问题是我在 default.aspx 中有一个 TextBox 和用户控件按钮,单击按钮后我需要更改 TextBox 的文本值(来自用户控件的一些默认值)。

这可能吗?如果可以,我需要在哪里编写代码?

Default.aspx

<%@ Register Src="Text.ascx" TagName="Edit" TagPrefix="uc1" %> 
<asp:TextBox ID="TextBox1" runat="server" Width="262px"></asp:TextBox>
<uc1:Edit Id="Edit2" runat="server" /></td>

用户控件 - 按钮

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Text.ascx.cs" Inherits="WebApplication4.WebUserControl1" %>
<asp:Button ID="Button1" runat="server" Text="Edit " OnClientClick="return confirm('Are you certain you want to Navigate?');" Width="341px" onclick="Button1_Click" />

如何从用户控件分组或触发(文本框值更改)?

【问题讨论】:

    标签: c# .net asp.net user-controls


    【解决方案1】:

    从您的用户控件开始:

    <asp:Button ID="Button1" runat="server" Text="Edit " 
        OnClientClick="return confirm('Are you certain you want to Navigate?');" 
        Width="341px" onclick="Button1_Click"/>
    

    在后面的代码中使用它来创建一个自定义事件,该事件会在按钮点击时触发

    using System;
    using System.Web.UI;
    
    namespace TestApplication
    {
        public partial class Edit : UserControl
        {
            public string DefaultValue { get; set; }
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
            private static object EditClickKey = new object();
            public delegate void EditEventHandler(object sender, EditEventArgs e);
            public event EditEventHandler EditClick
            {
                add
                {
                    Events.AddHandler(EditClickKey, value);
                }
                remove
                {
                    Events.RemoveHandler(EditClickKey, value);
                }
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                OnEditClick(new EditEventArgs(DefaultValue));
            }
            protected virtual void OnEditClick(EditEventArgs e)
            {
                var handler = (EditEventHandler)Events[EditClickKey];
                if (handler != null)
                    handler(this, e);
            }
    
            public class EditEventArgs : EventArgs
            {
                private string data;
                private EditEventArgs()
                {
                }
                public EditEventArgs(string data)
                {
                    this.data = data;
                }
                public string Data
                {
                    get 
                    {
                        return data;
                    }
                }
            }
        }
    }
    

    “Default.aspx”页面将包含新自定义事件的事件处理程序。
    标记:

    <asp:TextBox ID="TextBox1" runat="server" Width="262px"></asp:TextBox>
        <uc1:Edit ID="Edit1" runat="server" OnEditClick="EditClick_OnEditClick" DefaultValue="default" />
    


    代码背后:

    protected void EditClick_OnEditClick(object sender, TestApplication.Edit.EditEventArgs e)
            {
                TextBox1.Text = e.Data;
            }
    

    【讨论】:

      【解决方案2】:

      Button1Button1_Click 事件中,您可以使用Page.FindControl() 方法获取对TextBox 的引用,如下所示:

      protected void Button1_Click(...)
      {
           TextBox txtBox = (TextBox)this.Page.FindControl("TextBox1");
           if(txtBox != null)
              txtBox.Text = "Set some text value";
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-02
        • 2014-01-19
        • 1970-01-01
        • 1970-01-01
        • 2010-12-27
        • 1970-01-01
        相关资源
        最近更新 更多