Page event is not getting fired at all - c#

I am trying to use the Page_LoadComplete in my user control myusercontrol.ascx.cs but its not getting fired up, I added a break point and nothing, it is possible that the user control does not support this event? and if thats the case what can I use instead?

LoadComplete is not automatically wired up.. You'll have to do that yourself.
protected void Page_Load(object sender, EventArgs e)
{
Page.LoadComplete += new EventHandler(Page_LoadComplete);
}
void Page_LoadComplete(object sender, EventArgs e)
{
//Do your deed
}
Reference: http://connect.microsoft.com/VisualStudio/feedback/details/103322/page-loadcomplete-doesnt-fire-in-custom-controls

The LoadComplete event only happens on the Page. For a control, if you want to do something after the other controls' Load events have fired, about the closest you'll get is PreRender.
Alternatively, you could attach to the Page's LoadComplete event in your control's init stuff. But AFAIK it won't happen automatically.

Related

Add EventHandler to Content Page

I have wired up the Button control on my Master Page in the Content Page as follows:
SiteMaster MasterPage;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
MasterPage = (SiteMaster)Page.Master;
MasterPage.Button1.Click += new EventHandler(Button1_Click);
}
}
void Button1_Click(object sender, EventArgs e)
{
MasterPage.Button1_Click(sender, e);
}
However, whenever I click Button1 on the page (running under localhost), the Page_Load event fires!
What have I done wrong?
If it helps, the MasterPage.Button1_Click event runs a log in script, so there should be no recursive calls.
Is the issue here that the handler Button1_Click is not firing for you?
If so, the reason for this is because you are only assigning the event handler for the Click event on initial page load and not when the page is posting back.
Try assigning the event handler on initial load and subsequent postbacks like so:
protected void Page_Load(object sender, EventArgs e)
{
MasterPage = (SiteMaster)Page.Master;
MasterPage.Button1.Click += new EventHandler(Button1_Click);
}
Hope this helps.
Perhaps you want to wire your event up in the Page_Init as opposed to the Page_Load.
as far as your button click causing Page_Load to fire. Page_Load is always going to get fired because it's part of the page lifecycle.
see: http://support.microsoft.com/kb/305141 for page lifecycle details.
Page_Init: During this event, you can initialize values or connect any event handlers that you may have.
see also question: In asp.net, page_load occuring before radio button OnCheckedChanged event

C# Webbrowser document , invokemember("click"), is it possible to wait until that click action is resolved?

What I have going on is a Invokemember("Click"), the problem is I want to be able to grab the resulting innerhtml. The problem is i'm unsure of how/if it's possible to wait until the resulting action of the invokemember("click") is resolved. Meaning, in a javascript when you perform this click it will take you ot the next 20 items listed. However, i'm unsure of how to tell when that javascript will be fully loaded. Below is what I'm using.
private void button1_Click(object sender, EventArgs e)
{
HtmlElement button = webBrowser1.Document.GetElementById("ctl08_ctl00_InventoryListDisplayFieldRepeater2_ctl00_BlockViewPaging_Next");
button.InvokeMember("click");
HtmlElement document = webBrowser1.Document.GetElementsByTagName("html")[0];
}
One possible solution is to modify your "click" event handler in javascript so it changes the value of some hidden input field right before exiting the method (after all work is done). You can attach to the event of changing field from C# code and act when it's fired.
// Your init method - you can call it after `InitializeComponent`
// in the constructor of your form
Init() {
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
webBrowser1.Document.GetElementsByTagName("statusField")[0].AttachEventHandler("onchange", WorkDone);
}
void WorkDone(object sender, EventArgs e) {
HtmlElement document = webBrowser1.Document.GetElementsByTagName("html")[0];
}
That's the raw solution, I haven't yet checked whether "onchange" is a correct DOM event.
Also, you can't attach to DOM events before the document is completely loaded, that's why I put the attaching logic in the handler of DocumentCompleted event.

Problems with IsPostBack

I have an .aspx page which sends a letter to a customer if a button on that page is clicked. Onclick the page calls itself, so the mail send class is in the same file. However I do not want the mail sent when the page is simply loaded. I want it send the letter when the button is clicked, so, I'm trying with the following code:
void page_Load(Object sender, System.EventArgs e)
{
if (IsPostBack)
{
SendMail();
}
}
But it doesn't work. What am I doing wrong?
Why not use the Button's Click Event then?
What you are talking about is you want to send an email only when a specific button is clicked. Then why not register to it's click event instead of bloating your page_load with extra code?
Button's click event is raised only when that button's click causes a postback. So, that's your best option.
Make an event handler for the button's click event (Just double click the button in Visual Studio's Designer).
Using Page_Load will result in emails being sent out when the user posts back in any circumstance, not just your button click.
Looks like the page does not find the correct event handler for the Page_Load, check the case and correct it to Page_Load
See if this works (replace your page_Load method with the following code):
protected override void OnLoad(System.EventArgs e)
{
base.OnLoad(e);
if (IsPostBack)
{
SendMail();
}
}
You are currently relying on AutoEventWireup functionality to hook up your page events. This is slow and problematic and may be the cause of your issue. The method I gave you overrides Page.OnLoad and should correct the problem as well.
The Page_Load event is raised every time the page is posted, well by means of postback or callback, if you want to use server side events you should call you SendMail method in the button's click event:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && !IsCallback)
{
/*occurs the first time the page is loaded*/
}
if (IsPostBack)
{
/*occurs every time a postback is raised (e.g. by form submission) */
}
if (IsCallback)
{
/*occurs every time a callback is raised, e.g. by generating callbacks by means of AJAX/
}
}
protected void SendMail_Click(object sender, EventArgs e) { SendMail(); }
Read more at MSDN: ASP.NET Page Life Cycle.

How to call an event handler from one control to the another control?

In Visual C# Form Application, When I Click on the button I want to add to the other controls(like listboxes,labels,textboxes) in same form.
How do I do this?
I have no idea what "to come to the other controls" might mean. But the event handlers in your Form derived class is the switchboard. Implement the button's Click event and have it do whatever you want done with any other controls. A trivial example:
private void button1_Click(object sender, EventArgs e) {
label1.Text = "You clicked the button!";
}
In the form designer, add an event handler to the button's Click event.
The form designer will give you a new method like this; add your code into this method:
private void button_Click(object sender, EventArgs e)
{
// Write some code that uses list boxes, labels, text boxes etc.
}
You question is somewhat unclear, but if you simply want to access other controls on the form, just go ahead and do so:
private void YourButton_Click(object sender, EventArgs e)
{
string someValue = yourTextBox.Text;
// do something with the value
}
If you want to add one event handler to many controls, you can do it.
Just go to properties of control you wish to subscribe, find appropriate event from list (ex: onClick) and choise your existed handler.
But this method will be sutable if events compotable.
Describe your task more detail.

Windows Forms: detect the change of the focused control

I'm implementing copy-paste in a Windows Forms application.
I need to enable/disable the bar-buttons for this two operations when the user changes the focused element in the application.
I can find the current focused control using something like this: http://www.syncfusion.com/FAQ/windowsforms/faq_c41c.aspx#q1021q, but how can I detect that the focused control has changed?
In your form load event handler you could also loop through all of the controls contained in the form and for each focusable control add an event handler for the Enter event:
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control control in Controls)
{
control.Enter += ControlReceivedFocus;
}
}
void ControlReceivedFocus(object sender, EventArgs e)
{
Debug.WriteLine(sender + " received focus.");
}
My proposal is to use Application.Idle event.
Write logic that enables/disables your buttons in Application.Idle event.
Subscribe to Application.Idle event on form shown event
Check button availability on button click (so you never pass accidental click under heavy load)
Do not forget to remove Idle handler on form disposing (or closing), because this is static event
Using this technique you will always have correct buttons state, and you not need to worry about subscribing to many controls events to detect focus change. This is also light-weight approach, because Idle event is raised only when application is not busy.
I think you should add an event handler to the control (or if you have many of the same type, subclass it, and override the appropriate OnChange handler). This way you won't have to 'find' the focused control (it will be given as the sender parameter), and the event will only arise when the change actually happened.
To detect the focus on a control you can create this event:
void MyGotFocus(object sender, EventArgs e)
{
if (sender is TextBox)
{
//TODO YOUR OPERATION
//FOR EXAMPLE
(sender as TextBox).SelectAll();
}
}
and the next step is to associate the control and event by code:
myText1.GotFocus += MyGotFocus;
myText2.GotFocus += MyGotFocus;

Categories