I have a radio list in my master page and they fire an event when I select one of them.
Now this event isn't not controlled by my master page instead it is controlled by my content page.
My question is, is it possible to pass int/Strings from the master page method to content page method somehow.
P.S i want to pass the int i to content page method in this case
This is how i tide them up.
Master page Code to handle event
public event EventHandler Master_Save;
...
public void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
int i=RadioButtonList1.SelectedIndex;
if(Master_Save!=null)
{ Master_Save(this, EventArgs.Empty); }
}
and my content page code to handle the event
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
(this.Page.Master as Pages_MasterPage).Master_Save += new EventHandler(ContentPage_Save);
}
private void ContentPage_Save(object sender, EventArgs e)
{
//Code that changes a query
}
Sure you can, just define a custom event args class and parametrize your event with it:
public class MasterSaveEventArgs : EventArgs
{
public int Index { get; private set; }
public MasterSaveEventArgs(int index)
{
this.Index = index;
}
}
And then just use it:
public event EventHandler<MasterSaveEventArgs> Master_Save;
...
{ Master_Save(this, new MasterSaveEventArgs(i)); }
...
(this.Page.Master as Pages_MasterPage).Master_Save += ContentPage_Save;
// notice the shortened syntax here
...
private void ContentPage_Save(object sender, MasterSaveEventArgs e)
Related
I am having an issue with my usercontrol being loaded too late in the "post back timeline", because they are being loaded in as a result of a custom event.
As a result the button click events on this usercontrol don't fire on the first click (the entire post-back occurs only the event-handlers for the click don't get raised). On the second click (and hence second post-back), however, the event-handlers work fine.
How can I invoke a second post-back automatically right after one has just finished? So my usercontrol gets loaded correctly.
Default Page
public interface IEventProvider
{
void TriggerEvent(String path);
}
public partial class Default : System.Web.UI.Page, IEventProvider
{
private string LastLoadedControl
{
get
{
return Session[Paths.CURRENTCTRL] as string;
}
set
{
Session[Paths.CURRENTCTRL] = value;
}
}
private void LoadUserControl()
{
string controlPath = LastLoadedControl;
ContentPlaceholder.Controls.Clear();
if (string.IsNullOrEmpty(controlPath))
controlPath = Utils.Paths.USERCTRL_BASE + "Main.ascx";
Control uc = Page.LoadControl(controlPath);
ContentPlaceholder.Controls.Add(uc);
}
protected void Page_Load(object sender, EventArgs e)
{
LoadUserControl();
}
public void TriggerEvent(String path)
{
if (path.Equals("logout"))
{
Session.Clear();
Session.Abandon();
LastLoadedControl = null;
}
else LastLoadedControl = Paths.USERCTRL_BASE + path + ".ascx";
LoadUserControl();
}
}
Usercontrol code
protected void profileBtn_Click(object sender, EventArgs e)
{
Utils.Events.triggerRedirectPage(this.Page, "Login");
}
events code
public static void triggerRedirectPage(Page p, String path)
{
IEventProvider eventProvider = p as IEventProvider;
if (eventProvider != null)
eventProvider.TriggerEvent(path);
}
You can add a button (or another control) with AllowPostBack=true then trigger click event on this button.
I've followed this question and tried to build my solution. The problem is that 'UserControlButtonClicked' appears to be null! So 'UserControlButtonClicked(this, EventArgs.Empty)' inside the if, doesn't run, and the method 'addStepContent' in the parent page is never called.
UserControl 'StepsBar'
public sealed partial class StepsBar : UserControl
{
public event EventHandler UserControlAddStepContent;
[...]
public StepsBar()
{
this.InitializeComponent();
Image step_1 = new Image();
ButtonInfo step_1Info = new ButtonInfo();
step_1Info.Add((int)stepNumber.one, (int)stepStatus.normal);
step_1.Tag = step_1Info;
step_1.Source = setBackground((int)stepStatus.normal);
step_1.Tapped += stepTapped;
[...]
}
public void stepTapped(Object sender, RoutedEventArgs e)
{
[...]
if (step != null)
{
[...]
firePageEvent();
}
}
public void firePageEvent()
{
if (UserControlAddStepContent != null)
{
UserControlAddStepContent(this, EventArgs.Empty);
}
}
Parent Page
public Violation()
{
this.InitializeComponent();
StepsBar stepsBar = new StepsBar();
stepsBar.UserControlAddStepContent += new EventHandler(addStepContent);
}
private void addStepContent(object sender, EventArgs e)
{
CheckBox check_1 = new CheckBox();
check_1.Content = "Check me!";
bodyStackPanel.Children.Add(check_1);
}
This assumes that you want to use an existing delegate rather than make your own and you aren't passing anything specific to the parent page by event args.
In the user control's code-behind (adapt as necessary if not using code-behind or C#):
public partial class MyUserControl : System.Web.UI.UserControl
{
public event EventHandler UserControlButtonClicked;
private void OnUserControlButtonClick()
{
if (UserControlButtonClicked != null)
{
UserControlButtonClicked(this, EventArgs.Empty);
}
}
protected void TheButton_Click(object sender, EventArgs e)
{
// .... do stuff then fire off the event
OnUserControlButtonClick();
}
// .... other code for the user control beyond this point
}
In the page itself you subscribe to the event with something like this:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// hook up event handler for exposed user control event
MyUserControl.UserControlButtonClicked += new
EventHandler(MyUserControl_UserControlButtonClicked);
}
private void MyUserControl_UserControlButtonClicked(object sender, EventArgs e)
{
// ... do something when event is fired
}
}
Solved. The problem was this, on the parent page.
StepsBar stepsBar = new StepsBar();
stepsBar.UserControlAddStepContent += new EventHandler(addStepContent);
The istance of StepsBar was not added to the page. D'OH!
So here's what I've done:
stepsBar.UserControlAddStepContent += new EventHandler(addStepContent);
and on the xaml of the parent page:
<local:StepsBar x:Name="stepsBar"/>
I have a ascx page suppose page1.ascx in that I have a button click event handler
btnSave.Click +=
delegate
{
if (Save != null) Save(this, EventArgs.Empty);
};
and I have another ascx page suppose page2.ascx in that I have a button click
protected void btnEdit_Click(object sender, EventArgs e)
{
// want to call
btnSave.Click +=delegate
{
if (Save != null) Save(this, EventArgs.Empty);
};
}
I want to call btnSave click delegate(page1.ascx) on btnEdit(page2.ascx). Is it possible if yes then how?
If I understood what you mean, one way would be as below:
public class Control1 : UserControl {
public delegate void ButtonClickHandler(object sender, EventArgs e);
public ButtonClickHandler ButtonClickEvent {get;set;}
public void Save(object sender, EventArgs e) {
//do something
if (ButtonClickEvent != null) {ButtonClickEvent(sender, e);}
//do something
}
}
public class Control2 : UserControl {
protected override void OnLoad(EventArgs e) {
control1.ButtonClickEvent += YourMethod;
}
protected void YourMethod(object sender, EventArgs e) { // do something here ... }
}
Another way is to declare your button's event in the first control as property and assign your method in the 2nd control.
I'd recommend moving the code out of the code-behind and into a separate class, so that it is accessible to both .ascx files.
I'm trying to subscribe to the the save button event of a user control that is launched in a separate radwindow from the calling parent. but I am getting object not initialized error, I know why but what am I missing?
Update: I found my error but it appears that if (this.SaveEvent!= null) in the ControlBase is always null
Parent Control Code:
public partial class myControl : ControlBase
{
private myChildControl __myChildControl;
private void myControl_PreRender(object sender, EventArgs e)
{
// error occurs here
//this.__myChildControl.SaveEvent += new myChildControl.SaveEventHandler(__myChildControl_SaveEvent);
// found my error
this.SaveEvent += new myChildControl.SaveEventHandler(__myChildControl_SaveEvent);
}
private void __myChildControl _SaveEvent(object sender, CustomEventArgs e)
{
this.Label1.Text = e.CustomEventArg1.ToString();
this.Label2.Text = e.CustomEventArg2.ToString();
}
}
Child Control Launched in RadWindow:
public partial class myChildControl : ControlBase
{
protected void btnSave_OnClick(object sender, EventArgs e)
{
CustomEventArgs _cea = new CustomEventArgs {CustomEventArg1 = 123, CustomEventArg2 = 12};
callBaseMethod(_cea);
}
}
ControlBase Code:
public class ControlBase : UserControl
{
public event CustomEventHandler SaveEvent;
public delegate void CustomEventHandler(object sender, CustomEventArgs e);
internal void callBaseMethod(CustomEventArgs cea)
{
if (this.SaveEvent!= null)
{
this.SaveEvent(this, cea);
}
}
}
CustomEventArgs class:
public class CustomEventArgs : EventArgs
{
public int CustomEventArgs1 { get; set; }
public int CustomEventArgs2 { get; set; }
}
This isn't possible in codebehind: the RadWindow presents a separate aspx/ascx page altogether that is linked to the main page through javascript alone.
What you need to do is handle the RadWindow OnClientClose event in javascript, then fire something in the parent page that performs the appropriate tasks.
I've created a user control that contains a button and a few other controls.
When declaring my user control in the html markup I'd like to do some sort of :
<asp:CustomControl onclick="CustomControl_Click" ID="cc1" runat="server">
Where CustomControl_Click is obviously the action I want to call when my control's button is clicked.
So far in my control I have:
public event EventHandler Click;
protected void Button1_Click(object sender, EventArgs e)
{
Click.Invoke(sender, e);
}
but how can I forward the Eventhandler of the parent page to assign it to the Click Eventhandler in my control?
Any help is really appreciated!
PS: maybe there's a way of getting the method from the hosting page using reflexion
I'm using a custom button (actually html div with LinkButton embedded in it). Here is code of it:
public delegate void ClickEventHandler(object sender, EventArgs e);
public event ClickEventHandler Click = delegate { };
public string Text
{
get { return cmdLink.Text; }
set { cmdLink.Text = value; }
}
public bool CausesValidation
{
get { return cmdLink.CausesValidation; }
set { cmdLink.CausesValidation = value; }
}
public string OnClientClick
{
get { return cmdLink.OnClientClick; }
set { cmdLink.OnClientClick = value; }
}
public string CssClass
{
get { return cmdLink.CssClass; }
set { cmdLink.CssClass = value; }
}
protected void cmdLink_Click(object sender, EventArgs e)
{
Click(this, e);
}
Here is usage in aspx page:
<Button_Control:ButtonControl ID="btnSave" runat="server" Text="Save"
OnClick="btnSaveClick" />
and this is in code-behind page of aspx page:
protected void btnSaveClick(object sender, EventArgs e)
{
//do stuff here
}
I found it!
Instead of using event bubbling I use reflexion in the click eventhandler of the user control so I have :
public string OnClick;
protected void Button1_Click(object sender, EventArgs e)
{
MethodInfo methodInfo = this.Page.GetType().GetMethod(OnClick);
methodInfo.Invoke(this.Page, new object[]{ sender, e });
}
And it works with declaring the user control as :
<asp:CustomControl OnClick="CustomControl_Click" ID="cc1" runat="server">