开发者

User control click event

开发者 https://www.devze.com 2022-12-22 10:09 出处:网络
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 pl

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 a开发者_开发问答ccomplish 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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号