how to clear label text when required field fired

Last post 03-12-2009 3:10 AM by sunny74. 11 replies.

Sort Posts:

  • how to clear label text when required field fired

    03-10-2009, 8:57 AM
    • Member
      4 point Member
    • ramesh.sri
    • Member since 04-09-2008, 2:08 PM
    • Posts 33

    Hi ,

    I have one textbox with required field validation and submit button, when I enter value in texbox and click on submit button I am saving the record in database and  I displaying label message “ record is saved”,

     

    Once again if he click on submit button, I am displaying “please enter values” message by using required field, and label message “record is saved “ is displaying and I am clearing the label message in page load, but page load event is not also not get fired . because of required field .

    at same time I want clear the text in the label which was  “record is saved”

    Thanks

    ramesh 

     

  • Re: how to clear label text when required field fired

    03-10-2009, 9:08 AM
    • Star
      10,561 point Star
    • getchinna_sv
    • Member since 09-10-2008, 8:29 PM
    • Hyderabad
    • Posts 1,802
    Where you are assigning the text to the label...
    Chinna_sv...
  • Re: how to clear label text when required field fired

    03-10-2009, 9:08 AM
    • All-Star
      36,754 point All-Star
    • rtpHarry
    • Member since 10-01-2006, 8:51 AM
    • Lincoln, England
    • Posts 5,926

     Hey,

     You can set EnableViewstate="false" in the <asp:Label> markup. If you also set the label to visible="False" then it will not appear. Then when you set the label message you can also set it to visible = true. Both the visible setting and the text you assigned will be forgotten next time the page refreshes.

  • Re: how to clear label text when required field fired

    03-10-2009, 9:11 AM
    • Star
      8,527 point Star
    • Steelymar
    • Member since 01-28-2009, 9:39 AM
    • Bulgaria
    • Posts 1,258

    Add event on your textBox (onfocus="Text1_focus()") 

    <asp:TextBox ID="TextBox1" runat="server" onfocus="Text1_focus()"></asp:TextBox> 

    and in the head tag add:

    <head runat="server">

    <script language="javascript" type="text/javascript">
    <!--

    function Text1_focus() {
    document.getElementById ("MyMessage").style .visibility="hidden"
    }

    // -->
    </script>

    </head>

    Regards,
    Stefan Uzunov
  • Re: how to clear label text when required field fired

    03-10-2009, 9:29 AM
    • Participant
      1,382 point Participant
    • sunny74
    • Member since 10-20-2007, 3:47 PM
    • Posts 547

    Hi, 

    Do the following:

    In the Page_Load Event write:

    If(!IsPostback)

    Label1.Text = "";

    Then in the Submit Button Click event write the following:

    If(TextBox1.Text.ToString() != String.Empty)

    {

     // write code for insertion to database

    //check if DB insert success

     // If(DB Insert is success)

       Label1.Text = "Record is saved"

       Label1.Visible = true;

    }

    else

    {

      Label1.Text = ""

       Label1.Visible = false;


    }

    Toggling Visibility is optional.

    Hope this solves your problem.

    Inform me if this does not.

    SUBHRANIL BASU RAY
    Mumbai

    TODAY'S TECHNIQUES ARE OBSOLETE FOR TOMORROW'S JOB.
    PLS MARK MY REPLY "AS ANSWER" IF IT HELPED YOU.
  • Re: how to clear label text when required field fired

    03-10-2009, 12:03 PM

    For required field validator set

    SetFocusOnError="true".

    Regards,
    Ram Reddy Mekha, +91-994-840-4315
    http://abhiramreddymekha.blogspot.com

    Please Mark as Answer if the post helps you.
  • Re: how to clear label text when required field fired

    03-10-2009, 11:56 PM
    • Member
      4 point Member
    • ramesh.sri
    • Member since 04-09-2008, 2:08 PM
    • Posts 33

    Hi ,

    Thanks for your reply,

    Answer was not much help full for me, My actualy problem was when i click on submit button first required fields was fired and asking message "Please enter value", before requried field was fired i want to clear the label message.

    because first time when i fill the values and click on submit button i am displaying message "record was saved" in label , once again with out filling any values when i click on submit button requried fields are display "please enter values" and also label message is displaying "Record is saved" that why i want to clear the label message when requried fields are fired.

     

    Thanks

    Ramesh

     

     

     

  • Re: how to clear label text when required field fired

    03-11-2009, 12:22 AM

     

    Hey what does this enable view state do?....Smile
  • Re: how to clear label text when required field fired

    03-11-2009, 12:25 AM

    I got ur point, but as per the case u can not any modification to the controls in the page if there is any validation control.

    when you click the button first the validation controls will fire in the browser , hence u can not poceed.

    even if you try to clear the label text in the button click also will not work.

    I suggest you one think if it is ok for you do this

    Insterd of displaying the message "record inserted successfully" in a Lable, Display it in a messagebox, or keep an alert after the record is inserted. after the insert code  call an alert function from Java script;

    Response.Write("<script language='javascript'>alert('Record Inseted Successfully');</script>");//wrte this at the end of the insert code,

    PLEASE MARK AS ANSWER IF MY POST SOLVED YOUR PROBLEM
  • Re: how to clear label text when required field fired

    03-11-2009, 12:32 AM

     

    Hey....I got your problem...You can do one thing..In the OnSubmit_btnclick event of your submit button you can check whether the field that is compulsary(for which required field validator is kept) is " "(null) and if it is then make the label showing "data is saved" as invisible,i.e. make its visible property false. I hope this post has helped you....
  • Re: how to clear label text when required field fired

    03-11-2009, 12:58 AM
    • Star
      8,527 point Star
    • Steelymar
    • Member since 01-28-2009, 9:39 AM
    • Bulgaria
    • Posts 1,258

    the easiest way to do this is whit JavaScript...

    have you try the script that I suggest in my privios post?

    Regards,
    Stefan Uzunov
  • Re: how to clear label text when required field fired

    03-12-2009, 3:10 AM
    Answer
    • Participant
      1,382 point Participant
    • sunny74
    • Member since 10-20-2007, 3:47 PM
    • Posts 547

    Hi,

    Since you r saying that previous solution didn't work I giving a fresh solution now.

    Do the following:

    1) Delete the required field navigator.

    2) Make changes to the previous code given by me so that it is as given below:

     protected void Page_Load(object sender, EventArgs e)
        {

          If(!IsPostback)

           Label1.Text = "";// Label1 is the label where you display message like" record is saved"

     

        }

     protected void btn_Submit_Click(object sender, EventArgs e)
        {

           If(TextBox1.Text.ToString() != String.Empty) // where TextBox1 is where you r entering values

           {

             // write code to validate the string if required

            // write code for insertion to database

           //check if DB insert success

            // If(DB Insert is success)

               Label1.Text = "Record is saved";

             

            }

        else

         {

            Label1.Text = "Please enter values";

           


           }


        }

    3) Save the changes and run the program.

    Let me know if problem is solved.

     

    SUBHRANIL BASU RAY
    Mumbai

    TODAY'S TECHNIQUES ARE OBSOLETE FOR TOMORROW'S JOB.
    PLS MARK MY REPLY "AS ANSWER" IF IT HELPED YOU.
Page 1 of 1 (12 items)