asp.net - how to clear an label value in javascript - Stack Overflow

I haveanlabel "test"imgfrom.cs[c#code] text="data savedsuccessfully"

I have an label "test" img 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 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

I have an label "test" img 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 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

Share Improve this question edited Nov 7, 2009 at 12:26 Juri 33k20 gold badges108 silver badges137 bronze badges asked Nov 7, 2009 at 12:20 happysmilehappysmile 7,78738 gold badges106 silver badges182 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 1

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 pliant) 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 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();"/>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744222349a4563844.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信