xrx215
xrx215

Reputation: 759

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?

Upvotes: 4

Views: 3842

Answers (4)

Faizan S.
Faizan S.

Reputation: 8644

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

Upvotes: 2

Scott Mitchell
Scott Mitchell

Reputation: 8759

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.

Upvotes: 1

madatanic
madatanic

Reputation: 1790

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
}

Upvotes: 4

Jeffrey Lott
Jeffrey Lott

Reputation: 7439

If you don't mind doing a postback, the easiest way would be to add an eventhandler to the OnClick event of the button, and then manually set the IsChecked property of the CheckBoxes to false and the Text property of the label to a blank string in the eventhandler.

Upvotes: 1

Related Questions