locked
Can't clear label.text from one button, but can from another? RRS feed

  • Question

  • User1733860820 posted

    If an error occurs, a message is placed in mylabel.text. The next time the same routine is run, I'd like to first clear the label message and then run the process. I added code to the start of the button onclick event like

    mylabel.text = ""

    However, the next is never cleared by the code. Even if the routine runs without error, the same message remains. The routine is calling a sql server sp. I added a test button with the same code to reset the text, and nothing else, and it always clears the label text. I don't get it. There is no javascript involved. Why doesn't this simple code work as expected?

    Monday, August 27, 2018 9:26 PM

Answers

  • User-893317190 posted

    Hi sevi,

    If you want to clear the lblValidate.Text  when the user goes back to your page after she(he) is redirected, you could write in you page's Page_Load event.

    If Not IsPostBack Then
        lblValidate.Text = ""
    End If

    Or if you redirect the user just want to download something, you could let the user download at currentPage  and don't use redirect. 

     Response.Clear()
            Response.AddHeader("content-disposition", "attachment;filename=yourFilename.txt")
    //write your own content
            Response.Write("hellow")
    
            Response.End()

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 3, 2018 2:03 AM

All replies

  • User-893317190 posted

    Hi sevi,

    Normally , you could clear the label's text in a button's  click event. How do you arrange your aspx page? Do you put your button in an updatepanel?

    Please post your aspx pageand code behind so that we could know more about your code.

    Best regards,

    Ackerly Xu

    Tuesday, August 28, 2018 4:34 AM
  • User1733860820 posted

    Yes, the label's text should be cleared after the click event but it's not. The two buttons (the one that I need to work but does not, and the test button I added which only clears the label.text, are inside the same asp:panel; the buttons are right next to each other.

    Test button:

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    lblValidate.Text = ""
    End Sub

    Real button:

    Protected Sub btnExport_Click(sender As Object, e As EventArgs) Handles btnExport.Click

    Dim many variables not shown here

    lblValidate.Text = ""

    Try
    conn.Open()
    With cmd
    .CommandType = CommandType.StoredProcedure
    .Parameters.Clear()
    .Parameters.AddWithValue("@pstrPeriod", Session("Period").ToString)
    End With
    Dim sda As New SqlDataAdapter(cmd)
    sda.Fill(ds)
    dtSourceTable = ds.Tables(0)

    If dtSourceTable.Rows.Count = 0 Then
    lblValidate.Text = "An export was not created!"
    Else
    strExportFilePath = strExportFilePath + "Export.csv"
    Using writer As StreamWriter = New StreamWriter(strExportFilePath)
    WriteDataTable(dtSourceTable, writer, True, "5327")
    End Using
    Session("Path") = strExportFilePath
    Response.Redirect("Doc.aspx")
    End If

    Catch ex As Exception
    lblValidate.Text = ex.Message
    Finally
    If conn.State = ConnectionState.Open Then conn.Close()
    cmd = Nothing
    conn = Nothing
    End Try
    End Sub

    Tuesday, August 28, 2018 7:03 AM
  • User-418973555 posted

    if (IsPostBack)
    {
    txtYourTextBox.Text = "";

    // updatepanel02.Update();  <-- if there is one;
    }

    if you run isPostBack is true like the above example, it will run the page with button click event (eg. button click event is a postback event) and show what you have put inside the code if(IsPostBack) wrap, otherwise it will run normally what you have code outside the if (IsPostBack) , if putting outside 'if (IsPostBack)', it will not clear the textbox since it is not included in the postback event,

    Tuesday, August 28, 2018 7:11 AM
  • User1733860820 posted

    Right, but both buttons are inside an asp:panel, not an updatepanel.

    Tuesday, August 28, 2018 7:13 AM
  • User-893317190 posted

    Hi sevi ,

    Could you post your marks in your aspx page so that we could reproduce your problem?

    Best regards,

    Ackerly Xu

    Tuesday, August 28, 2018 7:59 AM
  • User1733860820 posted

    I can't post the entire page but here is that section

        <asp:Panel ID="pnlNav" runat="server" Width="1600px" Height="40px" >
            <asp:Button ID="Button1" runat="server" Text="Button"  OnClientClick = "return confirm('test')"/>
    
            <asp:Button ID="btnExport" runat="server" Text="Export" Width="50" style="margin-left:10px;" 
                OnClientClick = "return confirm('Pressing OK will export a CSV file. Do you want to continue?')" />
    
        </asp:Panel>

    Tuesday, August 28, 2018 2:07 PM
  • User-893317190 posted

    Hi sevi,

    I'm a little confused by your code you have posted.

    Let's  assume that your code doesn't meet an exception. The is to say  the code will either run the if block or else block.

    Although you write lblValidate.Text = "" at the beginning of the click event ,you write code lblValidate.Text = "An export was not created!" in your if block.

    In you else block , you write Response.Redirect("Doc.aspx") ,that is to say , if the user still stay at the page , your code in if block must have run and the text is set to "An export was not created!". That is why  although you don't meet an exception , the text is still not empty.

    Best regards,

    Ackerly Xu

    Wednesday, August 29, 2018 9:59 AM
  • User1733860820 posted

    Right; this is code I inherited; I am not sure why the redirect is there. If I rem it, nothing seems to change. The page never redirects in any case. Checking that out is on my list of tasks. The code I've posted is a small part of the page and code.

    What could cause the label text to not be cleared?

    Wednesday, August 29, 2018 4:31 PM
  • User-893317190 posted

    Hi sevi,

    If no error occurs, does the label always shows  "An export was not created!"?

    If so, please try to remove the line of code lblValidate.Text = "An export was not created!"  to see whether the code causes the problem.

    Or you could debug to watch the text property of lblValidate  to see when it is not empty.

    Please choose lblValidate.Text ,right click  and click  AddWatch  in debug mode.The text of the label will occur in the lower left corner.

    Best regards,

    Ackerly Xu

    Thursday, August 30, 2018 1:22 AM
  • User1733860820 posted

    The error message "An export was not created!" only appears when it should. I'm testing so I try to invoke errors. Any error message will not be cleared by the next successful code run. To run the export operation, the code must pass by the lblValidate.Text = "" line; but for some reason the text in lblValidate.Text is never cleared. I know it sounds strange; it is strange. I was hoping to find some new-to-me button property that prevented some aspects of postback by asking here, but someone would have mentioned that by now.

    I've already tried setting a breakpoint on the lblValidate.Text = "" line. What it shows is lblValidate.Text = "", even when that label has the error message showing. I know it's the correct label, I changed the color tag and the msg shows in the color.

    This is vs 2012 for various reasons. Someone else on this project had strange issues with labels as well, more serious actually.

    Thank you Ackerly for persisting in your attempts to help -  I really appreciate it.

    Thursday, August 30, 2018 3:45 AM
  • User-1320437544 posted
    Code please? It would be a lot more easier for us to see whats going wrong instead of trying to play on guessing.
    Saturday, September 1, 2018 9:43 PM
  • User1733860820 posted

    The relevant code is above, not going to repost it. I can't post the entire page it's quite large.

    Saturday, September 1, 2018 9:45 PM
  • User-1320437544 posted
    Try turning off the labels viewstate if it is used only for showing statuses. Let us know if it helps.
    Saturday, September 1, 2018 9:49 PM
  • User1733860820 posted

    That didn't make any difference.

    Sunday, September 2, 2018 1:15 AM
  • User1733860820 posted

    Now I have a clue and someone here will be able to explain it.

    In the vb code posted here early on is this line

    Response.Redirect("Doc.aspx")

    If I rem that, all of the labels behave normally, or at least per my expectation. What that redirect does is to present the cvs export as a downloadable file in the browser. Apparently the redirect "eats" the label text changes that were set in the vb for the calling page. Or maybe better said, the redirect seems to "eat" the page's postback event.

    No doubt this is a normal behavior of the asp.net postback system. I will reread the docs that someone here posted about page lifecycle. Is there a way to get the label.text changes to happen, now that it's clear that the redirect is the reason they are not changed?

    Sunday, September 2, 2018 10:28 PM
  • User-893317190 posted

    Hi sevi,

    If you want to clear the lblValidate.Text  when the user goes back to your page after she(he) is redirected, you could write in you page's Page_Load event.

    If Not IsPostBack Then
        lblValidate.Text = ""
    End If

    Or if you redirect the user just want to download something, you could let the user download at currentPage  and don't use redirect. 

     Response.Clear()
            Response.AddHeader("content-disposition", "attachment;filename=yourFilename.txt")
    //write your own content
            Response.Write("hellow")
    
            Response.End()

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 3, 2018 2:03 AM
  • User1733860820 posted

    The second approach will solve this. Thanks Ackerly.

    Monday, September 3, 2018 2:06 AM
  • User1733860820 posted

    No it didn't work.

    If I put a breakpoint up at the start of the function, on the line that has

    lblValidate.text = ""

    when I run the code, the value of lblValidate.text reported to be "", even when on the webpage it has a text message in it. I don't understand that...how can vs report text = "" when it does have text(or at least the page shows the text)?

    If I remove the new code and only rem that redirect, the text is cleared. But the value of lblValidate is still "" at the top of the function, when there is text showing in the browser.

    Monday, September 3, 2018 2:43 AM
  • User-893317190 posted

    Hi sevi,

    What  do you do on page load ? Could you show me how you modify your code  so that I could reproduce your problem?

    Best regards,

    Ackerly Xu

    Monday, September 3, 2018 2:50 AM
  • User1733860820 posted

    In the page onload, if IsPostback = True then lblValidate.Text = ""

    Monday, September 3, 2018 3:17 AM
  • User-893317190 posted

    In the page onload, if IsPostback = True then lblValidate.Text = ""

    Hi sevi ,

    Because you write this code, it will clear the content of your label every time the page posts back ,which is why you see the result.

    I suggest you could remove this line of code or write  if !IsPostback then  lblValidate.Text = "".

    Best regards,

    Ackerly Xu

    Tuesday, September 4, 2018 1:04 AM
  • User1733860820 posted

    I want it to clear that text there. The call to clear the text in the button event is actually redundant. But neither of them actually do clear the text which is why I started this thread.

    The other code in the button click, which calls a db stored proceedure, does load, but for some reason the text is not cleared from the browser; but a breakpoint on lblValidation.text shows it's text value IS "". Contradictory...

    Tuesday, September 4, 2018 1:06 AM
  • User-893317190 posted

    Hi sevi,

    Do you still use redirect ? If you still use redirect, how do you go back to the previous page ? Through a button or through the arrow at upper left corner of the bower?

    If you use the arrow , it will only go back to the history of your page where you don't have time to clear the text.

    This is the reason I could think out for your problem. 

    If so , I suggest you could use a button instead of using the arrow.

    Best regards,

    Ackerly Xu 

    Thursday, September 6, 2018 1:20 AM
  • User1733860820 posted

    No, I removed the redirect code and substituted the alternative, more direct approach mentioned in this thread.

    Thursday, September 6, 2018 1:33 AM
  • User-893317190 posted

    Hi sevi,

    I'm so sorry , the second way I provided will only allow  the download operation. That is to say , all the other operation will not work. Even you could see the label becomes empty , it will not be reflected in the aspx page.

    If you don't mind , I think out a workaround. Please use client side  alert method to show the message instead of the label.

    Below is my code. Here I declare a variable count  a to make a record. At first time , it will throw an exception and alert the error message.

    At the second time , it will download the file.  Please don't forget to use HttpUtility.JavaScriptStringEncode to encode your error message , or it may cause exception.

    The method RegisterStartupScript is used to register  client side js code.

     Public Shared a As Int32 = 1
    
       
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
    
        Protected Sub Button1_Click(sender As Object, e As EventArgs)
    
            Try
                If a = 1 Then
                    a = a + 1
                    Throw New Exception()
    
                End If
    
                Response.Clear()
    
                Response.AddHeader("content-disposition", "attachment;filename=yourfilename.txt")
                'write your own content
                Response.Write("hellow")
    
                Response.End()
    
            Catch ex As Exception
                ScriptManager.RegisterStartupScript(Me, Me.GetType(), "s", "alert('" + HttpUtility.JavaScriptStringEncode(ex.ToString()) + "')", True)
    
    
    
            End Try
        End Sub

    For more information about RegisterStartupScript ,please refer to https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.scriptmanager.registerstartupscript?view=netframework-4.7.2

    Best regards,

    Ackerly Xu

    Friday, September 7, 2018 1:08 AM