Solved

ASP.NET clear label after clicking on submit button

Posted on 2012-12-20
13
2,560 Views
Last Modified: 2016-10-17
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?
0
Comment
Question by:CipherIS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
13 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38710862
Can you show the code?
0
 
LVL 1

Author Comment

by:CipherIS
ID: 38710869
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

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38710971
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.
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 1

Author Comment

by:CipherIS
ID: 38710990
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.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38711001
It would because after the following

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

nothing else goes to the client.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 38711018
What is the solution then?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38711024
Clear the label on client hand side before submission to the server. Use OnClientClient to execute the javascript.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 38711054
javascript solution seems not to be working
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38711062
Show the code you have tried.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 38711071
<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

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 38711105
> return false;

That should be

 return true;



Is it a label? Try

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

Author Closing Comment

by:CipherIS
ID: 38711122
Thank You!
0
 

Expert Comment

by:chiu ft
ID: 41847615
this work!!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question