Every time I test the IsPostBack in PageLoad() false is returned whether or not post data is present. My first reaction was to check to see if the runat="server" tag was missing from the form or submit button. However, they were all added and the WriteEmail.aspx page still always returns false for IsPostBack. I have also tried using IsCrossPagePostBack in place of IsPostBack.
ListInstructors.aspx:
<form runat="server" method="post" action="WriteEmail.aspx">
...
<input type="submit" id="writeEmail" value="Write Email" runat="server" />
</form>
WriteEmail.aspx:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Response.Redirect("ListInstructors.aspx");
}
}
Post != Postback. A postback is when you post back to the same page. The action on your form is posting to a new page.
It looks like all you're doing is using the WriteEmail.aspx page to send a message and then going back to where you just were. You're not even displaying a form to collect the text there. It's a very... Classic ASP-ish... way to handle things.
Instead, put the code you use to send a message in class separate class and if needed put the class in the App_Code folder. Also change the submit button to an <asp:button ... /> Then you can just call it the code from the server's Click event for your button and never leave your ListInstructors.aspx page.
In response to your comment: No. From MSDN:
... make a cross-page request by assigning a page URL to the PostBackUrl property of a button control that implements the IButtonControl interface.
The IsPostBack is not true because the form is not being submitted from the WriteEmail.aspx page; submitting a form from the same page is what causes a PostBack. If you submitted the form from the WriteEmail.aspx page, it would be a PostBack; as it is, it's just a Post.
You might find this MSDN reference to be useful:
http://msdn.microsoft.com/en-us/library/ms178141.aspx
Related
(Using ASP.NET Web Forms. Microsoft .NET Framework Version:4.0.30319, ASP.NET Version:4.0.30319.34274)
I have an asp:TextBox field with a RequiredFieldValidator for that text field:
<asp:TextBox
runat="server"
ID="MyTextBox" />
<asp:RequiredFieldValidator
runat="server"
ID="RequiredUserName"
ControlToValidate="MyTextBox"
ErrorMessage="MyTextBox is required"
Display="None" />
When the user submits the form on the page, I validate it and then clear the form so that they user can use the form again. The code behind in Page_Load looks like:
if (Page.IsPostBack)
{
Page.Validate();
if (Page.IsValid)
{
SaveFormSubmissionToDatabase();
MyTextBox.Text = "";
}
}
So, I validate everything, it Page.IsValid is true, I save the users form data and then try to clear the form for the user to fill in again. But since the form is cleared, the RequiredFieldValid shows its error message, even though I have already run Page.Validate() and have not run it again.
Why is this and what can I do to avoid this error message from being displayed after I clear my form? More generally, why is this RequiredFieldValidator being run again?
I should note that the error message is actually being displayed in an asp:ValidationSummary control.
(I thought that maybe client side validation was triggering the error messages being displayed, so I set EnableClientScript="false" in the RequiredFieldValidator, but that didn't change anything)
I have researched this issue for a bit and have found other people with the same issue, but no solution. This question, for example. The proposed solution there is to redirect to the same page, which doesn't work form my use case because I need to display some information about the form that was just submitted, including a success message, and that information would be lost if I redirected to the same page.
the reason you get the message is because of the call to Page.Validate() which is exactly what is for as the name indicates. And because with every postback the validate is called.
Typically you would not put that code in the Page_Load, but in the submit button's onClick.
i.e.:
protected void SubmitButton_OnClick(object sender, EventArgs e)
{
Page.Validate();
if (Page.IsValid)
{
SaveFormSubmissionToDatabase();
MyTextBox.Text = "";
}
}
Update: Let me refine the reason. In the original post the MyTextBox.Text = "" is not the last thing that happens. The controls on the page will also fire their events and validations, and this happens after the Page_Load event.
For full life-cycle reference: https://msdn.microsoft.com/en-us/library/ms178472.aspx#general_page_lifecycle_stages
I have an Advanced Find Form that contains a few asp:textbox controls as well as asp:dropdownlist controls. When I hit my reset button, it works well in clearing or resetting my textboxes and dropdownlists.
However, when the user clicks "Go" to submit the search query and a grid is shown with the results, the reset button no longer works.
Here's my input button:
<input type="reset" value="Clear All" />
EDIT:
Please note that I need to reset the fields to "Default Values" and also I need to be doing without a postback, to be doing it on client side
Have you try with normal button :
<asp:Button ID="txtResetbtn" runat="server" OnClick="txtResetbtn_Click" />
protected void txtResetbtn_Click(object sender, EventArgs e)
{
TextBox1.Text = string.Empty;
}
I think the problem is that HTML button don't reset after a postback
Or you can try to do this in the :
<asp:Button runat="server" ID="reBt" Text="Reset"
OnClientClick="this.form.reset();return false;"
CausesValidation="false" />
From : Reset a page in ASP.NET without postback
That link can help you too : Is there an easy way to clear an ASP.NET form?
The input type "reset" does not clear the form, it resets the form to its default values. To clear the form after submitting, you'll have to use javascript to set all values to empty.
Edit:
Since you're using asp.net, you could also use an <asp:button /> to call a method that clears the values of each control in the form.
Edit 2:
If you need to keep this function client-side, you'll have to use Javascript. Also, I think it's important to make a distiction between resetting to a default value (what the "reset" input type does) and clearing the values in a form, even after a submit. To do the later on the client side, you'll have to write a little javascript.
I managed to workaround the problem. As TenneC and Nikolay have mentioned in previous answers, I used an ASP.Net button but I've performed the reset function using jquery as follows:
$(document).ready(function(){
$("#BtnClear").click(function () {
$('#FillForm input').val(function () {
return this.defaultValue;
});
$('#DDLId1').val(function () {
return this.defaultValue;
});
$('#DDLId2').val(function () {
return this.defaultValue;
});
});
});
I have Page1.aspx containing
Name: <asp:TextBox ID="txt1" runat="server" />
Page2.aspx tries to access its contents by
TextBox txt2 = (TextBox)PreviousPage.FindControl("txt1");
However I end up getting an Object reference not set to instance of an object exception
I've used PreviousPage before and have had success with this snippet of code I found elsewhere online (Can't remember where I found it!)
So..
Option 1:
On your first page you have your button that takes you to the second page, you need to set the PostBackUrl property to the new page url:
<asp:Button ID="button1" Runat="server" Text="submit" PostBackUrl="~/Page2.aspx" />
(This is presuming that your 1st page is a form that redirects to your Page2.aspx)
Then in the new page's code behind you need to write something along the lines of this:
public void page_load()
{
if(!IsPostBack)
{
TextBox tb = (TextBox)PreviousPage.FindControl("txt2");
Response.Write(tb.Text);}
}
You will need to transfer the value of the previous page's txt2.Text to a textbox or label on the new page if you are wanting to do any more postbacks on the second page, otherwise you will lose that value.
Option 2:
You could also use a Session variable surely to store your data?!
Session["text"] = txt2.Text;
Once your'e in the new page, the last page is probably gone, I'd suggest transferring your data over the session.
I have a form action in an ASCX page set to an external URL
<form id="fLoginForm" runat="server" action="http://external.url" method="post" defaultbutton="bSignIn">
Inside there is a standard ASP linkbutton
<asp:LinkButton CssClass="btn" ID="bSignIn" runat="server" Text="Sign In" OnClick="bSignIn_Click" />
The event "bSignin_Click" never gets fired when I have action="http://external.url" set on the form tag. However when I remove the action, it works as expected. I thought for runat='server' forms, the form would always post back? I need to read the URL from the action attrib and then redirect to it with some hidden input values also in the page.
Thanks.
Actually when you click on the link button, your form will postback to handle the click event. On form action you have given action url and it will be redirected to your action URL.
Logically you are doing wrong, you have to remove action URL
Why not handle the sign-in functionality within the handler bSignIn_Click()?
And then redirect the user to the desired URL using Response.Redirect("http://external.url");
Well the behaviour was actually very weird, as it behaved completely differently on a colleagues machine - rechecking in the solution from SVN, and it worked. Strange.
With the form you have, when the Linkbutton is clicked, all the data in your form is posted to the URL, and not posted back to the same form (hence the term "postback") where you can handle all those events.
You can try writing that URL in a hidden field (instead of form action attribute) and read that hidden field value in codebehind.
This seems to be a common problem but I cannot find a solution.
I type in my username and password which are in a login control I have created.
I then press enter once I've typed in my password and the page just refreshes. It triggers the page load event but not the button on click event.
If I press the submit button then everything works fine.
using your forms default button is correct, but you need to supply it the correct id as it will be rendered to HTML.
so you do as Jon said above:
<form runat="server" DefaultButton="SubmitButton">
But ensure you use the Button name that will be rendered.
You can achieve this my making the Button public in your control, or a method that will return it's ClientId.
Let's say your button is called btnSubmit, and your implementation of your control ucLogin.
Give your form an id
<form runat="server" id="form1">
Then in your page load in your code behind of your page, set the DefaultButton by handing it your button client id.
protected void Page_Load(object sender, EventArgs e)
{
form1.DefaultButton = ucLogin.btnSubmit.ClientID;
}
If you're using ASP.NET 2.0 or higher, you can set a default button attribute for your page's Form:
<form runat="server" DefaultButton="SubmitButton">
Pressing ENTER on a text input executes Form.onsubmit, not Button.onclick.
I suppose this was inspired by the fact that you can have a form without an actual submit button (depending solely on the use of ENTER).