TextBox的Usercontrol动作事件

时间:2011-09-26 06:58:53

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

我是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" />

如何从usercontrol中分组或触发(文本框值更改)?

2 个答案:

答案 0 :(得分:2)

从您的用户控件开始:

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

答案 1 :(得分:1)

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

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