If a user has disabled Javascript in their client browser and they try to use a Form on the Web Page, this causes a Postback. My understanding is that the Form would create a Javascript function __doPostBack to handle the Form submission, but with Javascript disabled a normal Postback occurs. When checking what as been posted back to the server, the Form is empty. Can a Form work with Javascript disabled?
A form ABSOLUTELY works without Javascript.
Just put your form consumption code in the button click event.
something like this perhaps
void btnSubmit_Click(Object sender,
EventArgs e)
{
// When the button is clicked,
// send the values of the input fields to the database.
}
The form will be submitted without javascript.
Any client-side validation will not run, so you may have invalid data. This is why you always, always perform server-side validation of your forms.
Now, you state that your form is empty. If you depend on javascript, jquery, or some other client-side programming to set values in your fields, then you may see empty values. I'm not sure where you're checking, but if you take a look at the request object in your page_load, you should see your data there.
Related
I am working on web application. I have a grid that shows data to the user. When user click on the any row of the grid, it redirects user to another page, as we have a asp link control on the a column.
Issues
My code is like
if (!Page.IsPostBack)
{
//CODE
}
When user click on the BROWSER BACK button, it does not execute the CODE. Simply show the data from CACHE.
How can I execute the CODE , on browser back button click ?
It's a global problem with ASP.Net. Most of developers thinks like Windows developer. It ends with a postback for mostly any action, including navigation actions.
To solve such problems:
Avoid using button to change page. Generate an hyperlink with the target url.
Wrap parts of the page within an update panel. The benefits is that the browser won't see a page change after each postback. Then you will be able to "refresh" the page without warning dialog
When populating a grid, instead of "selecting" a row from codebehin, generate a link to the same page with "?ID=42" in the url, and bind this value to the grid as the selectedvalue
The root cause of this "evil" behavior, is that postback are issued using HTTP Post request. Then, the browser requires to repost the page to rebuild it.
Using the technics mentionned above, you are issuing GET request. This kind of request doesn't require to resubmit anything.
Try Disabling browser cache for the page that you don't want to cache.
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
Response.Cache.SetValidUntilExpires(false);
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
if (!Page.IsPostBack)
{
//CODE
}
}
I have seen this question in What Great .NET Developers Ought To Know -By Scott Hanselman
I am not very sure, but as per my knowledge a button control gets rendered as HTML with input tag as submit(with a dynamic Id). When a POST event is fired ASP.Net uses dynamically generated button ID and matches respective button click event at C# side....
Please let me know, if my understanding is correct. Thought it can be completely wrong or out of context.
UPDATE:-
I did little google and came across this link
Which states as
An ASP.NET Button control in HTML is an HTML input element with a type
attribute set to "submit". A submit button in HTML invokes a POST,
using the action attribute from the form element, which, for ASP.NET,
is the current page. Since this is a POST, the form variables are
populated with all of the values from the input elements in the form
element, including the value (name) of the submit button. When the
POST arrives at the server and is parsed by ASP.NET, it will
acknowledge that the POST was initiated by the submit button, which is
turned into a Click event for the original ASP.NET Button control.
Did you try creating simple ASP.NET single page app with multiple buttons?
You can do that and then inspect the output page in browser.
You shall see hidden field __EVENTTARGET with value of button id that makes submit, on every postback this hidden field travels to server as well as __VIEWSTATE.
Maybe this is the 'answer'?
An ASP.NET Button control in HTML is an HTML input element with a type attribute set to "submit". A submit button in HTML invokes a POST, using the action attribute from the form element, which, for ASP.NET, is the current page. Since this is a POST, the form variables are populated with all of the values from the input elements in the form element, including the value (name) of the submit button. When the POST arrives at the server and is parsed by ASP.NET, it will acknowledge that the POST was initiated by the submit button, which is turned into a Click event for the original ASP.NET Button control.
In .NET Web Forms applications, a variety of events, often click events, post a form back to a server. The event is a message such as "Button1_OnClick", which is bound to a method (i.e. event handler) by
an event delegate.
The following code sample has a System.EventHandler object that binds the Button1_OnClick method to the Button1.Click event.
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.Button1.Click += new System.EventHandler(this.Button1_OnClick);
}
private void Button1_OnClick(object sender, System.EventArgs e)
{
}
Okay, so I have my asp.net page with all of my comparison and requiredfield validators. This leaves me with two concerns.
What additional validation do I need? Do I need anything in the code behind? I want them to be unable to hit the 'save' button until their textbox information is complete, and it seems to be doing this with just the validator controls, but I'm unsure if there are other steps I need to take.
If I have a requiredfield validator and I want to turn it off under special circumstances, where in the codebehind would I set it to true? Can I do it on the 'save' button click, before it prevents the button from functioning?
1.
You need as many validations as necessary, you can create many different validators.
You need server-side validation in the code behind. If a postback occurred then the form passed the validation, but there are some validation features which are unusable at client-side. For instance you register to a homepage and you have a form where the username is required and there is a regular expression validator too. These validators will run at client-side. But if the username has to be unique and you can only check that using a database then obviously this can't be checked at the client-side, therefore, the client-side will evaluate the page to be valid, a postback will occur and it's the job of the server-side to check whether the username is unique.
Note that you can create custom validators if you need to do anything exotic.
2.
Depending on your needs you can set the Enabled property of your validators whenever you need to do that. Read more about that here.
As per my knowledge,
We should include a check for "Page.IsValid" on the server side (code behind) whenever we are using the ASP.NET Validators. This would ensure a check at the server side even if the javascript is being disabled on the browser.
No, you can't do that on the save button click as the button click would not be hit until the validation passes.
Hope this Helps!!
1 if you want add validation server
protected void Button1_Click(object sender, EventArgs e) {
//Proceed only if the validation is successfull
if (!Page.IsValid) {
return;}
}
2 You can set CausesValidation="false" to button
The #1 question with Page.IsValid is ok.
The requiredfieldvalidator is a javascript validator, so you can't disable it from codebehind once it was enabled (unless it satisfies the condition and you can go through). But it's possible to disable it via javascript, check this code:
ValidatorEnable(workPhoneValidator, false);
Link: Dynamically enable or disable RequiredFieldValidator based on value of DropDownList
I have a form where I dynamically populate a DropDownList using Jquery's ajax function to retrieve a list of values from a web service. I originally had a Button control which submitted the form. This caused the "exception:Invalid postback or callback argument. Event validation is enable...." error.
After researching options, such as disabling event validation (bad) and registering for event validation (which would not work in this case) the best option seemed to be to swap the Button control for a LinkButton control. I did this and, sure enough, it works fine now.
My question is...why?
What is different about the LinkButton that means that it does not cause the event validation error and have I, by changing to a LinkButton, introduced a new security risk because event validation isn't happening?
The postback validation error is happening because the data you send back at the postback is no the same than when it was sent by the server.
You should take a look at this blog post by Scott K. Allen. He suggests to add all the possible values for your dropdown in the Render event for your web page.
You could also create your own version of the DropDownList since it won't require event validation as this guy suggests.
My personnal take is that you might have to rethink how you interact with your data. If you need to feed dynamically your DropDownList and you use ASP.NET WebForms then you are required to have a PostBack for that. You could use a UpdatePanel to make it feel "Ajax" if you want.
What's a simple good way to find out if a html button (the newer not input) caused the postback?
If the button is a HtmlButton control then you can just handle its ServerClick event:
<button id="MyButton" runat="server" onserverclick="MyButton_Click">Foo</button>
And then in your code-behind:
protected void MyButton_Click(object sender, EventArgs e)
{
// if you reach here it means that MyButton was clicked
}
Easiest way to do it is to add an OnClick event and just add sample code in the code behind. Then I usually add a breakpoint in the code.
I'm assuming you want to know how server-side ASP.NET code can determine if a given HTML button was pressed to cause the post back?
I can't think of any excellent solutions. Two sub-par solutions below:
1.) Add a client-side onclick to the button and have it modify a form element that the server has access to (i.e. hdnButtonClicked). The page_load will check that element and the value will allow it to determine if the HTML button was clicked.
2.) Have the button trigger a server-side method. Easiest way is to convert the button to an ASP.NET control and wire it up with an event handler but I'm sure you could work up some JavaScript to trigger a server-side method.
Unfortunately, I don't think information pertaining to how an HTML form was submitted is available on post back without some form of client side preparation. ASP.NET does this behind the scenes.