Problems with IsPostBack - c#

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.

Related

Telerik combobox - viewstate without autopostback

I turned off AutoPostback on my control because I need to validate something with javascript. And if everything is ok I'm doing postback clikcing on hidden button. The problem is that combobox looses selected value on page reloading.ViewStateMode set to Enabled. I'm populating combobox in page_load event:
protected void Page_Load(object sender, EventArgs e)
{
(!IsPostback)
{
InitializeItems(); // Helper method that binds data
}
}
Before going into the internals of Telerik, you can try to resolve the original problem. You said you want to perform validation before the postback takes place.
All you have to do is register the script you want to be ran on form submit:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
this.Page.ClientScript.RegisterOnSubmitStatement(typeof(YourClass), this.UniqueID, "your validation script here.");
}
Then in order to cancel the postback, your validation script needs to return false.

Executing last action when back button pressed ASP.Net

I have a ListBox with a "list of servers" that has AutoPostBack enabled and an SelectedIndexChanged event attached to it:
protected void lbServerList_SelectedIndexChanged(object sender, EventArgs e)
{
if ( lbServerList.SelectedValue.ToString() != "")
{
Response.Redirect("detail.aspx?Server=" + lbServerList.SelectedValue.ToString());
}
}
Then I have a textbox to add a "server" with a button "btnServertoAdd" (to execute the addition)
protected void btnServertoAdd_Click(object sender, EventArgs e)
{
Response.Redirect("add.aspx?Server=" + tbServertoAdd.Text);
}
Scenario: If I select an item from the ListBox it will go to detail.aspx showing the server specs: Awesome.
Now, If I click back (browser button) and then type something in the TextBox and click btnServerToAdd it will still go to detail.aspx and not to add.aspx as it should....
How can I fix this?
Let me know if more code is needed.
This is occurring because when you click the button, the selected server is also different from the original value (as stored in the view state). Both events are fired, but evidently the SelectedIndexChanged event is fired first and the Redirect skips the rest of the processing.
I can't think of how to not make the SelectedIndexChanged event fire the second time around, so instead what you could do is, instead of Redirecting in the events themselves:
Have a pair of bool member variables in your page class.
Set one to true in each event handler.
In the page OnLoadComplete event, check each and redirect as necessary:
If both are true, redirect to add.aspx.
If one is true, redirect to the corresponding page.
Otherwise don't redirect at all.

Page event is not getting fired at all

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.

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.

Categories