Asp.net Cancel Button - asp.net

I am trying this in my Form Load Event
cmdCancel.Attributes.Add("onClick", "document.forms[0].reset();return false;")
but it doesn't clear my form. My form is a "ContentPage", part of a masterpage.
Am I missing something?

Try this:
cmdCancel.Attributes.Add("onClick","document.getElementById('" + this.Page.ClientId + "').reset(); return false;");

A call to "Reset" on a form resets the values to what was supplied in the markup for the page. So if there are any values supplied (via ViewState or any other mechanism) that is what the fields will be reset to, not blank.

Shouldn't cancel take you away from the form entry page?
It sounds like you are trying to code "reset", but you are functionally attempting "cancel".
Personally, I always have an event that fires on "cleanup" and does all form cleanup that I need to do (not just for reset form fields).

Related

Activate-Deactivate submit button in a form

I made a registration form using backbone.js, CoffeeScript and jquery.
I am trying to disable the submit button after 1 click (so that it doesn't fire events again and again) , i also want that button get active again when i fill fields of my form.. Thanks for your time.
In your code that runs the event (hopefully in the events delegate routine of your view) all you have to do is tell JQuery to disable the button
$("#btnSubmit").attr("disabled", true);
Then when you need to re-enable it
$("#btnSubmit").removeAttr("disabled");
Just call the appropriate calls when required.
seems there is no enable/disable property. maybe you could use the “silent” option to keep the “change” event from triggering?
or just create a validation routine and call it from the event i.e.
(pseudo code)
BUTTON EVENT FIRED
IF IsValid() then
'do something
else
'do something else
end if
func IsValid() as boolean
' check to see if the form is ready to submit i.e. required fields are valid
Thanks alot #Bryan and #max ..
I have done it as :
enable_button: ->
$("#my_button").attr("disabled", false).removeClass('disabled')
I am also changing the color of button on enable/disable.

ASP.NET jquery function call

Suppose an ASP.NET .NET 3.5 Site.
There is an asp:TextBox defined with this jquery javascript attached to it:
$("#myTextBox").change(function() {});
which means that if the user changes the text value, this function will be called.
And of course it works.
The problem is that I've made a button in which's EventHandler it automatically fills this TextBox with the default value. A round trip / page reload occurs and a new value is correctly set. With the difference, that the jquery function change() is not called;
And i need to call that function because it sets a variable to decide whether the text has been changed.
EDITED:
The variable is set to check if the user provided some input. If that's the case, it offers him the possibility to save it.
The problem isn't that the variable is not set, when someone clicks on the Button (which makes a value auto-fill), the problem is that the Button causes the page to be reloaded and the var value is lost. And i need to keep the value until the user tries to leave the page.
REFLECTION
Perhaps I could set cookies across the page reloads ...
You probably need to adjust the ID if it's inside anything:
$("#<%=myTextBox.ClientID %>").change(function() {});
If I understand your situation correctly, you know server-side if the value has changed. Then just make sure that you output
$('#myTextBox').change();
when this is the case. You could put this code inside a placeholder and in the button event handler toggle a bool determining whether the placeholder is rendered or not.
Seems like you should be setting this other variable, the one that indicates whether the value has changed or not, from the code-behind when the value changes. In other words, it looks like you need to do this in two places.
Alternatively, you could change the button to set the value via javascript, and set it's OnClientClick handler instead of it's OnClick handler. Make sure the end of the OnClientClick returns false, to avoid the postback. In that case, you'd be able to avoid the postback altogether, and your problem would be solved.
<asp:Button runat="server" ID="someId" OnClientClick="$('#myTextBox').val('someValue');return false;"/>
Try:
$('[id$=myTextBox]').change();

Controls are empty after submit

I have a checkboxlist and textbox controls on my asp.net page and the are dynamically created and added to the page. When I populate the values and submit the form, the values are empty by the time it hits the server. Any help?
They are empty because they are being re-created too late in the page lifecycle.
Without knowing the precise point in the ASP.NET Page Lifecycle you're adding your controls, (though I'd guess it's either Page_Load or an event handler), it goes something like this:
Build control tree
Add dynamic controls
Render
(Postback)
Build control tree
Reconstitute viewstate & bind to Post values
Add dynamic controls
Render
To solve this, you need to make sure your controls are created early enough in the lifecycle. Standard practice is to break the "control creation" into a separate method, and during CreateChildControls, check to see if they need to be created:
override CreateChildControls()
{
if(IsPostBack)
{
EnsureDynamicControlsAreAdded();
}
}
This way, if they do need to be initially added by something as far late in the lifecycle as an event handler (Button_Click, for example), you can call the same EnsureDynamicControlsAreAdded method from there as well, and on the next round-trip they'll be created much earlier.
Further to Rex M's answer, you could try creating the controls in the "Page_Init" event - this is one of the first events in the page lifecycle, and is where I would normally create controls in a viewstateless page (NB: If you do this, do not surround the content of the Page_Init handler with an "if (!IsPostback)" - this will prevent it from working as intended).

How to reset ASP.NET Form? aspnetForm.reset() causes Event Validation invalid postback error

I need to reset an ASP.NET form. I am trying to use document.aspnetForm.reset() but then whenever I try to do anything after that I get an event validation error. How can I reset my form without upsetting ASP.NET's Event Validation? I CANNOT turn off event validation. Thanks!
You will need to clear each form element manually. If you have jQuery you could retrieve all input[#type="text"] and set their value to "" and so on for other form elements...
Add return false; to you javascript code. It will prevent postback.

Which event to do I programatically add validation to a control?

I need to add requiredvalidator on a textbox programatically on a page, do I do that in page_load or some event before that?
That depends on why you need to add it. If it is always going to be there, then OnInit is a good place.
If you need to add it only after an action has occured then you want to do it after LoadViewState has been called so you can continue to add it once you add it the first time. For the first time add, most likely you will want to do it because of some post back event, so you could add it in your event handler.
I would suggest adding a placeholder to the control at the location of where you will want this control. Then you add the control when it is required. You should then store some information in ViewState to know that you added the control. You can then override LoadViewState, and add the control there if it is needed.
If you need the Validator only if the control is loaded with some data, then you add it right after the data has been loaded, be that OnLoad or some property accessor.
Page_Load is good for changing controls' settings.
EDIT :
This code tested and works :
// in page_load event :
validator.ControlToValidate = textboxToValidate.ID;
But if you're generating your validators after an event dynamically, problem might be different.

Resources