ASP.NET clear label after clicking on submit button

CipherIS
CipherIS used Ask the Experts™
on
I have a form that exports data to excel when clicking on the submit button.  I obtain data from a SPROC in a database and dump the data to a DataTable.  If the dt.rows.count > 0 then I continue to export otherwise I set a label on the page ("lblMsg") to indicate no records were found.  

I intentionally make it fail by looking for data I know does not exist.  The message displays correctly.  However, when I make it look for data that does not exist lblMsg does not remove the Message even though I have stuck lblMsg.Text = "" all over the place.

Any ideas how to resolve?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
Can you show the code?

Author

Commented:
This Method is called after clicking on the Submit Button

    protected void ExportToExcel(DataTable dt)
    {
        this.lblMsg.Text = "";   NOT WORKING

        string sFileName = "MyFile_" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + ".xls";

        GridView gv = new GridView();
        gv.AllowPaging = false;
        gv.DataSource = dt;
        gv.DataBind();

        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", sFileName));
        Response.ContentType = "application/vnd.ms-excel";
        //Response.ContentType = "application/vnd.openxmlformats";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        //Change the Header Row Foreground to white color
        gv.HeaderRow.Style.Add("color", "#FFFFFF");
        //Applying stlye to gridview header cells
        for (int i = 0; i < gv.HeaderRow.Cells.Count; i++)
        {
            //Change to Blue
            gv.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
        }
        int j = 1;
        //This loop is used to apply stlye to cells based on particular row
        foreach (GridViewRow gvrow in gv.Rows)
        {
            gvrow.BackColor = Color.White;
            if (j <= gv.Rows.Count)
            {
                if (j % 2 != 0)
                {
                    for (int k = 0; k < gvrow.Cells.Count; k++)
                    {
                        gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                    }
                }
            }
            j++;
        }
        gv.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
        Response.Flush();
    }

Open in new window

Most Valuable Expert 2012
Top Expert 2014

Commented:
Are you expecting the label to clear before the export? Is the Rendering of grid not being done?

Label would only clear once the processing has been completed on the server and response returns to client.
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Author

Commented:
I'm not trying to render the grid.  I am using the grid to export the data in the datatable.  Excel is opening and my data is properly formatted in Excel.  The Label is NOT clearing at all - no matter where I put the code to clear.

I moved this.lblMsg.text = "" to after Response.Flush();  It is not working.
Most Valuable Expert 2012
Top Expert 2014

Commented:
It would because after the following

        Response.Write(sw.ToString());
        Response.End();
        Response.Flush();

nothing else goes to the client.

Author

Commented:
What is the solution then?
Most Valuable Expert 2012
Top Expert 2014

Commented:
Clear the label on client hand side before submission to the server. Use OnClientClient to execute the javascript.

Author

Commented:
javascript solution seems not to be working
Most Valuable Expert 2012
Top Expert 2014

Commented:
Show the code you have tried.

Author

Commented:
<asp:Content ID="Content2" ContentPlaceHolderID="cphBody" Runat="Server">

    <script type="text/javascript">
        function ClearTextbox() {
            document.getElementById('<%=lblMsg.ClientID%>').value = '';
            return false;
        }
    </script>

<asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>

<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"
                         OnClientClick="return ClearTextbox();" />   

</asp:Content>

Open in new window

Most Valuable Expert 2012
Top Expert 2014
Commented:
> return false;

That should be

 return true;



Is it a label? Try

document.getElementById('<%=lblMsg.ClientID%>').innerHTML = '';

Author

Commented:
Thank You!

Commented:
this work!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial