开发者

how to clear an label value in javascript

开发者 https://www.devze.com 2022-12-10 14:59 出处:网络
I haveanlabel \"test\"comimgfrom.cs[c#code] text=\"data savedsuccessfully\" .but onceIclick thesavebuttonineedtoclear itstext

I have an label "test" comimg from .cs [c# code] text="data saved successfully" . but once I click the save button i need to clear its text right now I have 3 开发者_如何学C required field validators. with message [cannot be blank, cannot be blank,cannot be blank,] as user as clicked the save button I need to clear the text of the label. But need to show the required fields validator message

any idea how to solve it

thank you


make a javascript function like:

<Script type="text/javascript">
function clearText(cntId) {
  var cnt = document.getElementById(cntId);
  cnt.value ="";
  return false;
}
</script>

then on your submit button attach a client side event

<asp:Button id='btnSubmit' Text='Submit' onClientClick='clearText("<%this.lblLable.ClientId%>");' .... />


On the client-side use a script like this

<script type="text/javascript">
  function clearLabelValue(){
     var labelObj = document.getElementById("<%= myLabel.ClientID %>");
     labelObj.value = "";
  }
</script>

<asp:Label id="myLabel" runat="server" Text="Some text"/>
<asp:Button id="myButton" runat="server" Text="Submit" OnClientClick="clearLabelValue();return false;"/>

Didn't test it in detail, but should work.

It is not really clear what you want to achieve, although I have the feeling there may be a "better" (more standard compliant) way of achieving what you want. Maybe you could describe more clearly what you want, so we may be able to help you.


In these situations when a particular button has validation attached to it and also we need to fire some javascript what is done is to define a javascript function which is called on click of save button.

What this javascript function does:

This function will take your label and will set its value as blank so that the text is cleared.

Now in order to validate the page which happens internally (in case the javascript function is not written on the save button click) we need to explicitly call what asp.net call for client side validation.

There is a function page_ClientValidate which needs to be called from this javascript function so that validation is still done and we also do some other processing like clearing the label in this case.


<!--for cleaning to label ; -->

document.getElementById("MyLabel").innerHTML = "";


<!--and label is like;-->

<asp:Label ID="MyLabel" runat="server" ></asp:Label>


You can simply achieve this using below script:

<script type="text/javascript">
  function clearLabelValue(){
      document.getElementById("<%= myLabel.ClientID %>").innerText=""

  }
</script>
<asp:Label ID="myLabel" runat="server" ></asp:Label>
<asp:Button id="myButton" runat="server" Text="Submit" OnClientClick="clearLabelValue();"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消