ASP.NET jquery function call - asp.net

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();

Related

Trying to remove a property from an input (textbox)

I add this in my Render method (custom webcontrol):
Me.Attributes.Add("onkeypress", "chang(event,this);")
If affects some textboxes if they have some properties. But there are times that i don't want this property to be set, so no javascript will be executed. I've tried to remove it in code-behind on page_load and i was going to try to remove it on prerender method but it happens before my controls Render method.
How can i remove this property?
Take a look at the ASP Events Lifecycle. As you can see, the render event is at the bottom of the execution list. Since no events are fired after render, and render is where you are adding this functionality, then render is also where you must remove this functionality.
You could try to move the function that adds it into a higher event (load for example) and then remove it on render. Either that, or when you are applying it, perform any checks to see if the objects requires it or not.

.net - identify whether a button has a class when clicked

I have an asp button which I am using in 2 different places (appending to a new place and adding a class using jquery under certain conditions).
I need to slightly alter the function that runs when this button is clicked depending on whether or not this button has a given class. Is this possible??
So something like this...
if myASPButton hasclass("xyz") then
...
end if
i'm using vb.net for what it's worth.
It's not possible, clicking a button causes a postback, the only information posted back is form data. Modifications to the HTML client-side wont be reflected severside.
You will need to make a hidden field and populate the value of the hidden field and use that for checking severside.

How to hide / show CustomValidation validation messages without postback

UPDATE : Figured it out..
The objects I was passing to the ValidatorHookupControl were'nt being set properly (were null). Now that they are, the messages are currectly dissapearing when the hooked up control looses it's focus.
ORIGINAL POST ..
Hi,
I have some ClientValidation controls that have ClientSideValidation methods which work fine when validating the page..
However, how can I make it so that when a certain control that a CustomValidators clientside method kicks in and udates the validation message depending on whether the validation has passed or not. (Like the RequiredFieldValidator or RegExValidator).
My Customvalidators do not have their ControltoValidate properties set as some of them depend on multiple controls.
I don't want any postbacks (full or partial).
I have tried..
Adding an onchange attribute on dropdowns, radioboxes and checkboxes that call a helper clientside method which calls Page_ClientValidate('GroupName'), then setting window.location back to the control in question (as it went back to top of screen).
Using this method the args.IsValid is still being set by the ClientSideValidation method.
And I have tried ValidatorHookupControl (control, validator) but that doesn't seem to work either.
Any thoughts..?
My bad, was passing null objects into the ValidatorHookupControl method.
Works now. Doh!

How do I trigger a PostBack with arguments from Javascript?

This is what I'm doing: Using jquery, I'm popping up a "form" over the page that lets the user search for branches of the company and select the one they want. When the form pops up, they can type in a textbox, and it will do AJAX requests back to the server to return the top n results for what they've entered, and those results will be put into a list for them. I want the user to be able to select one by clicking a link that says "select" or something, and at that point I want it to do a PostBack have the Branch Selector control that this is in change it's SelectedBranch property to the newly selected branch. I've got this all working right now with a hard coded list of LinkButtons, but how do I do the same thing with a dynamic list of links inserted with jquery?
Look at the HTML that gets emitted for your hard coded LinkButtons. You'll see that each one calls the JavaScript __doPostBack function when clicked. I believe this function takes two arguments: a control ID and an extra command argument you can use for your own purposes.
I would suggest adding a single control to the page whose only job is handling events for the dynamic links. Then, when you are creating the links with jquery, make each one call __doPostBack, passing the event handling control's ID for the first argument and some other string for the second argument that identifies which link was clicked. In the Click event for the handling control, look at the second argument value and do what you need to do.
The short answer is... you don't.
ASP.NET relies on the Viewstate for the current state of the controls, including items in a DropDownList or similar control. Dynamically updating a list on the client will not modify the viewstate, so will not be available on the back end.
The general workaround for this is to just add a hidden field which updates/stores the current selection via js on the client side. Then read it from this field on the backend rather than List.SelectedValue.

Fail to load values in textboxes

I have textboxes which is placed inside accordian (ajax control).In pageload event i have written the code to load values in textboxes. But onload event one error is coming as object not set to instance of a object. Values are not coming in textboxes.
Then i tried to initialize controls in textboxes .Then the error cleared. But
Values are not coming in textboxes. What may be the reason for this?
Have you set the properties of the Accordian properly?
If yes, check the values which you are assigning to the textbox. If they are blank, the values won't be visible.
Try giving a hardcoded value to your textbox and check if it appears or not -
textboxObject.Text = "Hello World!";
At a guess, as the Textbox is a child control of the accordian you possible have to use the AccordianObject.FindControl() method to find your textbox.
More information or posting some code would help, otherwise we are just going to be taking stabs in the dark.

Resources