Solved

ASP.NET clear label after clicking on submit button

Posted on 2012-12-20
13
2,388 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now