简体   繁体   中英

User control click event

In user control i have a customised button. Iam using this user control on aspx page. when the button in the user control is clicked checkboxes and label in the aspx page should be cleared. Can you please let me know how to accomplish this?

in your usercontrol you need to create a public eventhandler

public event EventHandler UpdateParentPage;

and in your usercontrol's button clicked event, put

protected void btn_Click(object sender, EventArgs e)
        {
                if (this.UpdateParentPage != null)
                  UpdateParentPage(sender, e);
        }

on your parent page code behind, set the event handler for your usercontrol:

userControl.UpdateParentPage+= new EventHandler(userControl_UpdateParentPage);

then, implement the new event handler on your parent page:

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

Some time ago I had to do a similar implementation and came up with creating a Reset-Button-Click event handler.

And ended up with having something really simple like this:

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

 }

There are, of course, other implementations that allow you to scale / enhance your application in a later matter.

Cheers

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

You'll need to create an event in your User Control and have that event raised when the Button in the User Control is clicked. Then, in the ASP.NET page, you'd create an event handler for that User Control event and in this event handler you'd clear out the CheckBox and Label controls as needed.

Check out this article: Passing Information Between Content and Master Pages , focusing on the section titled Passing Information from a Master Page to its Content Page . This part of the article shows how to do something in a content page when the user performs some action in the master page (such as clicking a button). The concept is identical to what you want to do with a User Control.

Also, you might find this tutorial helpful: Interacting with the Content Page from the Master Page . The two articles referenced here have example code in both C# and VB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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