views:

301

answers:

4

This is my label i want to display if the user have left out field before clicking the button. What am i doing wrong because nothing is happening when I click the button...

<asp:Label ID="lblError" runat="server" 
Text="* Please complete all mandatory fields" style="display: none;" >
</asp:Label>

This is my function i call when i click on the button

function valSubmit(){
    varName = document.form1.txtName.value;
    varSurname = document.form1.txtSurname.value;

    if (varName == "" || varSurname == "") 
    {
     document.getElementById('lblError').style.display = 'inherit'; 

    }
    else
    { 

     .................other code go here...........................
    return true; 
    } 

}

+4  A: 

Why not use the Validation controls? These will give you client and server side validation out of the box - not that I'm lazy or anything... ;-)

Edit for comment:

The RequiredFieldValidator can be set to display a single red asterisk by the side of each control, and a validation summary control could be used BUT that would take up space.

So, it's possible that ASP.Net is renaming your control, so your JS should read:

document.getElementById('<%= lblError.ClientID %>').style.display = 'inherit';

Give that a go...

Personally, I'd still use the Validator controls ;-)

Mike Kingscott
Space is a problem on my form, it is a very small pop up form.
Etienne
Incidentally, if space is a premium, you could use a Custom Validator to check the state of each control and the display of the error message would be taken care of by the Custom Validator as well. You would have to write your JS as previously mentioned (the ClientID) and you would also have to write some corresponding server-side validation in case your visitor has turned off JavaScrript - something your OQ will fall down on.
Mike Kingscott
Meant to mention this as well: if your are using the Validation controls, make sure you check Page.IsValid in your code before performing any operations that rely on that valid data, mmm'kay?
Mike Kingscott
Nope did not work...........thinking about just giving the user a Alert
Etienne
Hi Etienne, could you please post the source of the html page that is generated by your ASPX page? I would like to see what is being emitted by the controls and what's being put in your JS.
Mike Kingscott
+1  A: 

You shouldn't be using lblError as an ID in javacript code. Instead you should use:

'<%= lblError.ClientID %>'

Of course this is only possible if you are generating the javascript code in the asp.net file.

kgiannakakis
Nope did not work.
Etienne
A: 

Take a look at jquery, you can select by classes instead of id's which will never be altered when rendered onto the page (unlike id's)

maxp
A: 

on your desired event use this

document.getElementById('<%= lblError.ClientID %>').style.display = ""; or
document.getElementById('<%= lblError.ClientID %>').style.display = "block"

ok then try this, instead of client side, make it serverside. First set it invisible like , on formload event set invisible using lblEror.visible = false and remove style ="display:none" from html. Then on the desired event/s make it visible and after processing again invisible.

If you want it strictly thorugh js.try this workaround. remove style from asp label. on body onload make it disable from some js function. now on the btn click event make it visible using the method something like this

    function Validate()
    {
         var objLbl = $get('<%=lblError.ClientID%>');
         if (validations fails)
         {
             objLbl.style.display = ""; //displays label
             return false;
         }
         else
         {
             objLbl.style.display="none" //hides label
             return true;
         }
    }
<asp:button id="btnValidate" runat="server" onclientclick="return validate();"/>

Hope this will work

Amit Ranjan
Vote if its your solutions
Amit Ranjan
Note does not work.........
Etienne