I'm using a coda slider like consctuct on one of my pages. Naturally, the anchor ("#currentTab") information is lost after a postback. This is annoying because when you press a button on a certain tab, you always end up on the first tab after the postback.
What is the best way of letting this information survive a postback?
Try this is your page_load event
Me.Form.Attributes("onsubmit") = "this.action+=top.location.hash;"
I'm not doing in ASP, but as a general solution attempt this might work for you too:
create a hidden field within the form that you send
on tab change, fill in the value for this selected tab (if there are no values for identifying the tabs, create them)
when rendering the page after submit, create a piece of javascript on the fly, which switches the tab on DOM:ready.
Either execute your postback as an AJAX request, or add some javascript to the form that will send the anchor value to the server
Rough example
<form onsubmit="this.anchor.value=top.location.hash">
<input name="anchor" type="hidden" value="">
<!-- rest of form -->
</form>
Then you'll need a convention to return it to the client and perform the appropriate action.
Related
I have a form like the following:
<form id="form-12" data-length="120" data-height="240">
<!-- different input fields -->
</form>
I can track the formSubmit event but what i need to include into the event are the values inside the data-attributes. How is this possible with Google Tag Manager? Do i need to to this with Javascript or is there another solution?
Open GTM preview, submit the form, select the form submit event in the preview and inspect the state of your variables on the submit event. You can use any of them.
You won't have the data-attributes among them, however.
You will have to use JS to parse out all the attributes on formSubmit if the attributes are still there on it.
It's worse if they're not. Then you would have to make them available for the future submit on pageload, on button click or on a different suitable trigger.
When binding text/data to a TextBox, it is done like
#Html.TextAreaFor(model=> model.Question)
but, when the PostBack happens, the text shows up in the TextBox. Is there a way to still bind the data to the textbox, but not display the text in the textbox?
I have a search functionality on my website, and when someone searches I want the results page to have the textbox without the text (the searched word).
You've said Postback, so you're using Server side implementation, right?
If that's the case, the simplest way is to add a script that runs every page load.
For example, you have this search textbox:
<input type="textbox" id="search">
In your javascript :
<script type="text/javacript">
$(document).ready(function{
//get the attribute of the search textbox and clear the text
$('#search').val('');
});
</script>
If you're using Ajax implementation then just add a script when your form post succeeds.
You should set the Question property to null or empty on the controller action before displaying the result, or also you make it empty after the page is loaded in the document ready method using jquery.
How can we prevent resubmitting the page on refresh..
For Example:
I have an input field and on submitting the page it adds the value of that field to a list and gets reloaded automatically but if intentionaly refresh the page without writing anything to the field it adds the previously added value to the list. How to prevent this problem in webforms asp.net
Try using <input type="button" /> instead of <input type="submit" /> or their corresponding ASP.NET controls. If you have a submit button on the page, it will post the form when you refresh (regardless of whether is accidental or intentional). But you lose the ability to submit the form using the enter button. The user has to click on the input button.
You might still want to keep the submit button, so in that case after adding to the list, redirect to the URL explicitly using Response.Redirect
Response.Redirect(Request.Url.AbsoluteUri);
In addition, to be totally fail-safe on the server side, you can also check if the item already exists in the list or the database (if that's the backing store for the list) before adding it.
I am using ASP.NET 4.
My UI depends on using an update panel. Updating certain UI elements requires a PostBack which is handled by the ScripManager. Basic stuff. These action occur before the fields are submitted.
The problem comes in with the HTML5 markup that have to deal with.
<input type="text" id="foo" aria-required="true" required>
Using a .NET TextBox control and adding attributes, I get rendered code as such:
<input name="foo" type="text" id="foo" aria-required="true" required="required" />
The HTML5 behavior is that each time a PostBack occurs, the input field thinks it should alert the user, since no value is present in the field, even though the user if off doing other actions within the UI.
Is there a way to bring this HTML5 behavior under control, such that the the INPUT field ignores PostBack?
ASP.NET web forms is a bit strange in that the entire page is a "form" and that postbacks occur in order to fire server side events, not simply to submit form data. As the form "submit" event is being fired, the HTML form validation API will attempt to validate the entire contents of the form. In this case you are firing a submit event that isn't actually a form submission - so the validation behaviour is incorrect.
There are a few ways you could deal with this.
You could add the "formnovalidate" attribute to all the buttons and input with type submit elements that trigger a postback but do not actually submit the form. It is possible to alter this with JavaScript using the inputElement.formNoValidate property as well as using jQuery and JavaScript attribute methods.
You could add the "novalidate" attribute to the form element to avoid client side validation altogether, whilst having the semantic meaning behind the attribute on the input elements involved - perhaps using something like jQuery validation to validate the form yourself on submission, or use the validityState API to perform totally custom validation in JavaScript.
You could conditionally add and remove the "required" attribute, and change the value of "aria-required" using jQuery or native JavaScript so that these are only present during the form submission phase of the process.
You could conditionally add and remove the "novalidate" attribute to the form element, or set the formElement.noValidate property in order to determine if client side validation occurs.
When using a LinkButton the doPostBack() function is rendered in the page and when the button is clicked doPostBack() is invoked at client side submitting the form to the server and also sending information in two hidden fields called EVENTTARGET and EVENTARGUMENT. The EVENTTARGET holds the control name which caused the postback and EVENTARGUMENT holds any info.
With this ASP.NET engine gets the control name from EVENTTARGET argument which caused postback and calls RaisePostBackEvent() event to call right server side event handler. I understand how doPostBack() works and how server side event handler is called.
However for a normal server button how is the correct button click at server side called? How is ASP.NET notified on the control name that was clicked client side?
A normal ASP.NET Button is rendered as a HTML <input type="submit"> element with a unique name attribute. By default this HTML element will trigger an HTTP POST to the HTML form associated with the current ASPX page.
When an HTTP POST happens a sequence of control-name/current-value pairs, known as form data set, is constructed from the successful controls inside the form, for example the textual value inside a text box is transmitted like this. A pair with the name of the button that triggered the submit is also sent to the server and that's how ASP.NET know which button triggered the HTTP POST.
The HTML specification guarantees that only the successful controls inside the form are considered for the form data set that is posted and since the definition of a successful control contains the following rule:
If a form contains more than one submit button, only the activated
submit button is successful.
then ASP.NET can check the posted form data set to check if any normal button was triggered.
Generally speaking you should never have to worry about the internals of a button click. This is the beauty of .Net.
However, to know specifically what is called on the client, you could simply view the source of the page and see what's being called for a button.
Example:
<input name="ctl00$MainContent$btnMyButton" id="ctl00_MainContent_btnMyButton" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$MainContent$btnMyButton", "", true, "", "", false, false))" type="submit" value="My Button"/>
Observe that .Net injects the following javascript routine:
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$MainContent$btnMyButton", "", true, "", "", false, false))