How to change imagebutton click event method - c#

I have an imagebutton which i set up at design time via the designer and assigned a method to its Click event. I need to now change that buttons target event method dynamically.
I have tried this by setting the following code but it doesn't seem to alter the target event for the imagebutton to my desired method 'imgBtnFw_Click_Details'
imgBtn.Click +=new ImageClickEventHandler(imgBtnFw_Click_Details);
Im thinking maybe i need to detach the currently assigned click event but not sure.
Does anybody have a correct set of steps for switching the target firing event method?

Its working...
Event Binding..
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
imgBtn.Click -= ImageButton1_Click; // remove previous handler
imgBtn.Click +=imgBtnFw_Click_Details; // add new handler
}
}
Event handler ...
protected void imgBtnFw_Click_Details(object sender, ImageClickEventArgs e)
{
//your implementation
}

Related

When there is no PerformClick() in asp.net webform

I want to use PerformClick() in asp.net webforma but found that can't be used. Any replacement for this PerformClick. I also have write the code as below but still have no idea. Thank you.
protected void Page_Load(object sender, EventArgs e)
{
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.ButtonEnter.Click += new System.EventHandler(this.ButtonEnter_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
Button.PerformClick() has namespace System.Windows.Forms, therefore you can't use it in ASPX page's code behind.
Assumed ButtonEnter is a button server control, you can simulate button click programmatically in 2 ways:
1) Direct calling server-side Click event handler method
ButtonEnter.Click(sender, eventArgs);
Note: Adjust both sender & eventArgs arguments depending on your requirements.
2) Using client-side event handler & RegisterStartupScript
JS
function click() {
document.getElementById("<%= ButtonEnter.ClientID %>").click();
}
Code behind
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "Click", "click()", true);
Related issues:
Programmatically fire a button's click event
How to programmatically fire the input(button) onclick event

Why does my dynamically created user control doesn't fire button click event

i have a problem with user control.
i create it dynamically on my aspx page after clicking on a button:
protected void btnAddRules_Click(object sender, EventArgs e)
{
RuleProperty Control = (RuleProperty)LoadControl("RuleProperty.ascx");
MyPanel.Controls.Add(Control);
}
when i click on a button of my user control, the button event wont fire and the user control will disappear. here is the button event:
protected void btnAdd_Click1(object sender, EventArgs e)
{
WowzaRule rule = GetRuleFromGUI();
RuleList.Add(rule);
//Session["RuleList"] = RuleList;
//List<WowzaRule> test = new List<WowzaRule>();
SaveToXMLFiles(txtdbnum.Text, RuleList);
}
i understand that after pressing the button on mypage the usercontrol is released and if its not created on pag_init or page Load it wont stay, but i need to create it on my button click event and find a way for it not to disapper.
thanks in advance, Daniel
You might have to add an event handler that it can fire the click event and call your delegate
Control.Click += btnAdd_Click1;
Dynamically created controls, once added, have to be on a page on every page load in order to work correctly. What happens in your case:
RuleProperty is added after the button click
Page loads with this control
User clicks on the button within RuleProperty
Control is not added to the control tree during the page load (corresponding code is only in the button click handler, and that button was not clicked)
ASP.NET does not know which control triggered the event, so the event is not processed
To go around this issue you need to add you control on every page loading, for example using some flag stored in ViewState:
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["AddRuleProperty"] != null && (bool)ViewState["AddRuleProperty"])
{
AddRulePropertyControl();
}
}
protected void btnAddRules_Click(object sender, EventArgs e)
{
AddRulePropertyControl();
ViewState["AddRuleProperty"] = true;
}
private void AddRulePropertyControl()
{
RuleProperty Control = (RuleProperty)LoadControl("RuleProperty.ascx");
MyPanel.Controls.Add(Control);
}
Update.
If you want to remove the control from the page later on in the control's click handler, you need to remove corresponding ViewState key. This is not possible from the control directly, since property Page.ViewState is protected, and also this would have created an unwanted dependency.
What seems as the right way to do this is to subscribe to the very same event from the Page (you might need to make this event visible from the controller) and reset the key in there. Like this:
private void AddRulePropertyControl()
{
RuleProperty Control = (RuleProperty)LoadControl("RuleProperty.ascx");
Control.ButtonClick += RuleProperty_ButtonClick;
MyPanel.Controls.Add(Control);
}
private void RuleProperty_ButtonClick()
{
ViewState["AddRuleProperty"] = false;
}
Please note that event name here is not real, this is just a sketch of what can be done.

How can i catch onclick event of Dynamically loaded control?

I have a dropdownlist (on Page) which has OnSelectedIndexChange event thats Loads different Control (ascx) dynamically each time ( with LoadControl Command) - into the page.
Each Control Has a Button(runat=server) and TextBox(runat=server).
When i click on the button - i cant get into the Onclick function .
How can i get into the OnClick Function of the Ascx ?
I know that each SelectedIndexChange its makes postback - so i know i have to save something in the viewstate. but i dont know how to save it and later get the values eneterd on the TexstBox. ( of Each ascx)
You need to add an event handler to the user control, like this:
public event EventHandler ButtonClick;
And in the click event of the button:
protected void Button1_Click(object sender, EventArgs e)
{
if (this.ButtonClick != null)
this.ButtonClick(this, e);
}
Then, from the page, you can get the click event like this:
<UC:MyUserControl ID="UserControl1" runat="server" OnButtonClick="UserControl1_ButtonClick" ... />
protected void UserControl1_ButtonClick(object sender, EventArgs e)
{
//Handle the click event here
}
If you're loading the controls dynamically, then you'll need to make sure the controls are rehydrated after postback, and emulate the code above by assinging the event handler through code:
MyUserControl ctrl = (MyUserControl)this.LoadControl("...");
ctrl.ButtonClick += new EventHandler(UserControl1_ButtonClick);

Custom Component On Click Event

I've built a custom component that basically has a picture box and label in it. In the parent form, I want to be able to detect when its been clicked on. The standard .click event doesn't seem to be working, but I've never used events before so am unsure if I'm using them correctly. Heres the code I'm using (in the parent) to try and make it recognise the click:
Item aItem = new Item();
aItem.Icon = ItemImage;
aItem.Title = Title;
aItem.Click += new EventHandler(ItemClicked);
aItem.Filename = File;
and heres the method its calling:
public void ItemClicked(Object sender, EventArgs e)
{
MessageBox.Show("Item Clicked!");
}
This code never fires. Do I need to put anything into the component or am I just doing this wrong?
Cheers
Right I finally worked it out. Tejs response just confused me more so here's what I did.
In my UserControl I had the following event:
public event EventHandler Clicked;
Then I had an event for when the image was clicked (still in the UserControl) and I just called the Clicked event:
private void imgItem_Click(object sender, EventArgs e)
{
Clicked(this, e);
}
Then in my parent form, when I created the object, I had the following:
Item aItem = new Item();
aItem.Clicked += new EventHandler(ItemClicked);
void ItemClicked(object sender, EventArgs e)
{
MessageBox.Show("Clicked!");
}
You would do this by exposing an event':
Your custom component:
// A custom delegate like MyItemClickedHandler, or you could make a Func<> or Action<>
public event MyItemClickedHandler ItemClickedEvent;
public void ItemClicked(object sender, EventArgs e)
{
if(ItemClickedEvent != null)
ItemClickedEvent(); // Your delegate could pass parameters if needed
}
Then your parent form simply observes the event:
myCustomControl.ItemClickedEvent += new MyItemClickedHandler(SomeMethod);
Then, whenever the event is raised on your custom control, the parent is notified because it subscribed the event.

Raising the Load event within a dynamic loaded web usercontrol

I need to load a web user control dynamically.
Looking at http://weblogs.asp.net/srkirkland/archive/2007/11/05/dynamically-render-a-web-user-control.aspx, it states that the page lifecycle events are not fired.
I thought I might be able to raise the events through reflection. I cannot figure how to fire the events, am I missing something?
Thanks
Podge
You can do something like this before calling RenderControl:
Page page = new Page();
page.Controls.Add(report);
In this case Init method will be called.
an answer given on that link of yours
The standard Load event should fire just fine. The standard ASP.Net control events are raised for usercontrols. If you are wanting to fire events inside your usercontrol from the parent page then you'll want to do something like this:
Inside your usercontrol create an event and wire it up. In this example I'll call it from Page_Load:
public event EventHandler TestEvent;
protected void Page_Load(object sender, EventArgs e)
{
if (this.TestEvent != null)
{
this.TestEvent(this, e);
}
}
Inside your parent page wire up the user controls TestEvent:
protected override void OnInit(EventArgs e)
{
MyUserControl uc = LoadControl("~/PathToUserControl.ascx");
uc.TestEvent += new EventHandler(MyUserControl_TestEvent);
}
protected void MyUserControl_TestEvent(object sender, EventArgs e)
{
//this code will execute when the usercontrol's Page_Load event is fired.
}
Hope that helps!!

Categories

Resources