Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ASP.NET clear label after clicking on submit button

Posted on 2012-12-20
13
Medium Priority
?
2,683 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
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!

 
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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

972 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