用户控制单击事件

时间:2010-03-04 18:30:26

标签: c# asp.net

在用户控制中,我有一个自定义按钮。我在aspx页面上使用这个用户控件。当单击用户控件中的按钮时,应清除aspx页面中的标签和标签。能告诉我如何实现这个目标吗?

4 个答案:

答案 0 :(得分:4)

在您的usercontrol中,您需要创建一个公共事件处理程序

public event EventHandler UpdateParentPage;

并在你的usercontrol的按钮点击事件中,输入

protected void btn_Click(object sender, EventArgs e)
        {
                if (this.UpdateParentPage != null)
                  UpdateParentPage(sender, e);
        }
在您的父页面代码上

,为您的usercontrol设置事件处理程序:

userControl.UpdateParentPage+= new EventHandler(userControl_UpdateParentPage);

然后,在您的父页面上实现新的事件处理程序:

protected void userControl_UpdateViewState(object sender, EventArgs e)
{
        //clear your checkboxes and label here
}

答案 1 :(得分:2)

前段时间我不得不做一个类似的实现,然后想出了一个 Reset-Button-Click 事件处理程序。

最后得到了一些非常简单的东西:

protected void ButtonReset_Click(object sender, EventArgs e) {
    if (!TextBox1.Enabled || !ButtonSubmit.Enabled) {
        TextBox1.Enabled = true;
        ButtonSubmit.Enabled = true;
    }
    VieStateData.ResetSession(); // Created a dedicated class to handle the data and session state
    TextBox1.Text = string.Empty;
    TextBox2.Text = string.Empty;

    // More controls to modify

 }

当然,还有其他实现可以让您在以后的事情中扩展/增强您的应用程序。

干杯

答案 2 :(得分:1)

如果您不介意进行回发,最简单的方法是将事件处理程序添加到按钮的OnClick事件,然后手动将CheckBoxes的IsChecked属性设置为false,并将标签的Text属性设置为事件处理程序中的空白字符串。

答案 3 :(得分:1)

您需要在用户控件中创建一个事件,并在单击用户控件中的按钮时引发该事件。然后,在ASP.NET页面中,您将为该User Control事件创建一个事件处理程序,并在此事件处理程序中根据需要清除CheckBox和Label控件。

查看此文章:Passing Information Between Content and Master Pages ,重点关注标题为将信息从母版页传递到其内容页的部分。本文的这一部分展示了当用户在母版页中执行某些操作(例如单击按钮)时如何在内容页面中执行某些操作。该概念与您要对用户控件执行的操作相同。

此外,您可能会发现本教程很有用:Interacting with the Content Page from the Master Page。这里引用的两篇文章在C#和VB中都有示例代码。