Solved

ASP.NET clear label after clicking on submit button

Posted on 2012-12-20
13
2,458 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
  • 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 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