admin管理员组

文章数量:1399737

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 plete all mandatory fields" style="display: none;" >
</asp:Label>

This is the 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; 
    } 

}

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 plete all mandatory fields" style="display: none;" >
</asp:Label>

This is the 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; 
    } 

}

Share Improve this question edited Jun 22, 2011 at 20:21 Bill the Lizard 406k212 gold badges574 silver badges892 bronze badges asked Jan 6, 2010 at 14:07 EtienneEtienne 7,20143 gold badges110 silver badges163 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

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 ment:

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

You shouldn't be using lblError as an ID in JavaScript 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.

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

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)

本文标签: aspnetMaking ASP label visible in JavascriptStack Overflow