views:

44

answers:

4

I need help with the javascript below please. I have 2 text boxes and 1 button on my form. What I would like to happen is if User enters a value in textbox one and not textbox two, then event need not be fired. However, if there is a value in textbox one and then textbox 2 then I want a post back to fire. My code below does not seem to be doing this at all and I would appreciate it if someone could help me fix this or better still figure it out. Thanks in advance

function Changed(textControl) {
    //  alert(textControl.value);
    var conlength = document.getElementById('<%=txtLength.ClientID %>');
    var conwidth = document.getElementById('<%=txtwidth.ClientID %>');
    if (conlength != null && conwidth != null) {
        if (conlength.value != null && conlength.value != ' ' && conwidth.value != null && conwidth.value != ' ') {
            // ' ' corresponds to c#'s String.Empty                
            __doPostBack(document.getElementById('<%=btncalcboardfeet.ClientID %>'), 'Calculate Board Feet Button event has been fired');
        }
    }
}
A: 

Why not take a simpler approach ... if your condition is met, programmatically click your submit button ;-) it's super simple using something like jquery even if you're into that sort of thing:

$("#<%=btncalcboardfeet.ClientID %>").click();

That way you don't have to try to figure out what internal javascript function asp.net is trying to call (as that may change in the future (however unlikely)).

edit: non jQuery version:

document.getElementById("<%=btncalcboardfeet.ClientID %>").click();
Joel Martinez
THat would work IF I could use jquery in my code but I am not to do that .
Kobojunkie
see edit, non jquery version is just as easy (albeit a few characters longer) ;-)
Joel Martinez
A: 

instead of that last if statement just try the following:

if (conlength.value && conwidth.value) {
            // ' ' corresponds to c#'s String.Empty                
            __doPostBack(document.getElementById('<%=btncalcboardfeet.ClientID %>'), 'Calculate Board Feet Button event has been fired');
}
Alex Sexton
that did it! Thanks
Kobojunkie
Wait, just another thing! When I initially launch my page and enter values in textbox 1 and then textbox 2, the click event is fired but the textboxes are instead cleared of their values and so nothing else happens as a result. Is there something I need to do in addition to get this working normal?
Kobojunkie
That doesn't seem related to this code. Nothing here modifies any values. Is there something else running on the page?
Alex Sexton
Well.... for the one the controls are contained in a Usercontrol which is dynamically loaded on the page and reloaded on postbacks. Could that be an issue and if yes, is there a way I can take care of that? I mean when I had an actual button on the page that the user would have to click to initiate postback, I did not have this problem. Now I have Javascript doing the button clicking for me and this problem shows up. Is there supposed to be some difference?
Kobojunkie
If it worked with a normal form submit before, you could just fire the `submit()` function on the parent form, rather than call the postback yourself. I'm not incredibly familiar with .NET so I can't help in that regard. Sounds like a different issue entirely.
Alex Sexton
A: 

If you are using ASP.NET WebForms and are using the asp:TextBox to collect the information, you can use the RequiredFieldValidator to ensure that both fields have a value before the page will postback.

LBushkin
+1  A: 

You need to look at the length of the VALUE, not the element itself:

var conlength = document.getElementById('<%=txtLength.ClientID %>').value;
Diodeus