Improve company productivity with a Business Account.Sign Up

x
?
Solved

Unable to update an asp.net label text property

Posted on 2011-02-22
3
Medium Priority
?
375 Views
Last Modified: 2012-05-11
I have a function that populates a grid from searching for files in a directory.  I pass the function a search text string to filter by (so the user can enter search text to filter by in textbox called txtSearch).  This works fine on page load.  It populates the grid and updates a record count label called lblCount.  

When I filter by some search text, it updates the grid with the correct records, but it does not update the label control.  It simply stays the same as the full search count.  e.g., these first 2 lines work fine, but the last doesn't do anything, even when I step through:
        gvTracks.DataSource = dtFiles;
        gvTracks.DataBind();
        lblCount.Text = counter.ToString();  //  !!!THIS REFLECTES THE FILTERED REC COUNT WHEN I STEP THROUGH, BUT IT DISPLAYS THE COUNT OF ALL RECS SOMEHOW!!!

// Here's my label control in the aspx page:
Tracks:<asp:Label ID="lblCount" runat="server" Text="Label"></asp:Label>

   
// Here's my function that populates the grid and counts records - again, the variable intCount, calculates correctly when I step through, but doesn't reflect on the label control

 protected void fillGrid(string SearchText)
    {
        DataTable dtFiles = new DataTable();
        DataColumn dc;
        bool isCleanFilename = true;
        int counter = 0;

        // Initialize lblCount
        lblCount.Text = "INIT";

        // File Name
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Filename";
        dtFiles.Columns.Add(dc);

        // Username
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Username";
        dtFiles.Columns.Add(dc);

        // Status
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Status";
        dtFiles.Columns.Add(dc);

        // Notes
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Notes";
        dtFiles.Columns.Add(dc);

        // Directory
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Directory";
        dtFiles.Columns.Add(dc);

        // Add the row - loop through file list
        DataRow row;

        // Get usernames, compare for existence in Composers folders, then loop through them all, and dump files into DataTable
        string[] roles = new string[] { "SH_COMP", "SH_ADMIN", "SH_SUPER" };

        MembershipUserCollection mc = Membership.GetAllUsers(); //FindUsersByRole(roles);

        foreach (MembershipUser u in mc)
        {
            DirectoryInfo di = new DirectoryInfo("C:\\FTP\\Composers" + "\\" + u.UserName.ToString());

            if (di.Exists)
            {
                FileInfo[] rgFiles = di.GetFiles("*.wav");
                foreach (FileInfo fi in rgFiles)
                {
                    isCleanFilename = true;

                    // Check the file that they uploaded for invalid characters
                    if (fi.Name.IndexOf(":") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("*") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("?") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("<") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf(">") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("|") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("#") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("&") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("!") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("+") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("[") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("]") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("{") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("}") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("(") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf(")") > 0) { isCleanFilename = false; }

                    row = dtFiles.NewRow();
                    row["FileName"] = fi.Name;
                    row["Directory"] = fi.Directory.ToString().Replace("\\" + Profile.UserName, "");
                    row["Username"] = u.UserName.ToString();

                    if (isCleanFilename)
                    {
                        row["Status"] = "GOOD";
                        row["Notes"] = "";   // put notes text box value here
                    }
                    else
                    {
                        row["Status"] = "INVALID FILENAME";
                    }

                    // Check for MP3 - give notice to run the converter, if not there
                    if (!File.Exists("c:/Purg MP3s/" + fi.Name.Replace(".wav", ".mp3")))
                        row["Status"] = "MP3 File not created. ";

                    // If SearchText (txtSearch) has a value, filer results
                    if (SearchText != "...Search")
                    {
                        if (u.UserName.ToString().ToLower().IndexOf(SearchText.ToLower()) > -1 || fi.Name.ToLower().IndexOf(SearchText.ToLower()) > -1)
                        {
                            dtFiles.Rows.Add(row);
                            counter = counter + 1;
                        }
                    }
                    else // Add all
                    {
                        dtFiles.Rows.Add(row);
                        counter = counter + 1;
                    }
                }
            }
        }

        gvTracks.DataSource = dtFiles;
        gvTracks.DataBind();
        lblCount.Text = counter.ToString();  //  !!!THIS REFLECTES THE FILTERED REC COUNT WHEN I STEP THROUGH, BUT IT DISPLAYS THE COUNT OF ALL RECS SOMEHOW!!!
    }

Open in new window

0
Comment
Question by:JT_SIRO
  • 2
3 Comments
 
LVL 56

Accepted Solution

by:
Ryan Chong earned 1000 total points
ID: 34957909
Did you check whether somewhere in your scripts you check against the IsPostBack event or somewhere in your scripts are calling function fillGrid if the page is posted back?
0
 

Author Comment

by:JT_SIRO
ID: 34958097
Well, yes and no.  I only call fillGrid in page load if !Postback, but I do call fillGrid from my btnSearch event, passing it the search text that fillGrid filters by.  I think I've got it right, because the grid renders exactly how I intend, but it seems to ignore whatever I populate lblCount.Text with on postbacks.  I only set that in one place and have put a break point on it and it shows the correct value all the time, yet doesn't update the UI for lblCount, yet it does for gvTracks.DataSource.  So strange...  Here's the code for PageLoad, fillGrid and btnSearch_Click.  Look at the two lines where I wrote "NOTE:::", to know where the problem lies.
protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            fillGrid("...Search");
        }
   }

    protected void fillGrid(string SearchText)
    {
        DataTable dtFiles = new DataTable();
        DataColumn dc;
        bool isCleanFilename = true;
        int counter = 0;

        // Initialize lblCount
        lblCount.Text = "INIT";

        // File Name
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Filename";
        dtFiles.Columns.Add(dc);

        // Username
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Username";
        dtFiles.Columns.Add(dc);

        // Status
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Status";
        dtFiles.Columns.Add(dc);

        // Notes
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Notes";
        dtFiles.Columns.Add(dc);

        // Directory
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Directory";
        dtFiles.Columns.Add(dc);

        // Add the row - loop through file list
        DataRow row;

        // Get usernames, compare for existence in Composers folders, then loop through them all, and dump files into DataTable
        string[] roles = new string[] { "SH_COMP", "SH_ADMIN", "SH_SUPER" };

        MembershipUserCollection mc = Membership.GetAllUsers(); //FindUsersByRole(roles);

        foreach (MembershipUser u in mc)
        {
            DirectoryInfo di = new DirectoryInfo("C:\\FTP\\Composers" + "\\" + u.UserName.ToString());

            if (di.Exists)
            {
                FileInfo[] rgFiles = di.GetFiles("*.wav");
                foreach (FileInfo fi in rgFiles)
                {
                    isCleanFilename = true;

                    // Check the file that they uploaded for invalid characters
                    if (fi.Name.IndexOf(":") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("*") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("?") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("<") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf(">") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("|") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("#") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("&") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("!") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("+") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("[") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("]") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("{") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("}") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf("(") > 0) { isCleanFilename = false; }
                    if (fi.Name.IndexOf(")") > 0) { isCleanFilename = false; }

                    row = dtFiles.NewRow();
                    row["FileName"] = fi.Name;
                    row["Directory"] = fi.Directory.ToString().Replace("\\" + Profile.UserName, "");
                    row["Username"] = u.UserName.ToString();

                    if (isCleanFilename)
                    {
                        row["Status"] = "GOOD";
                        row["Notes"] = "";   // put notes text box value here
                    }
                    else
                    {
                        row["Status"] = "INVALID FILENAME";
                    }

                    // Check for MP3 - give notice to run the converter, if not there
                    if (!File.Exists("c:/Purg MP3s/" + fi.Name.Replace(".wav", ".mp3")))
                        row["Status"] = "MP3 File not created. ";

                    // If SearchText (txtSearch) has a value, filer results
                    if (SearchText != "...Search")
                    {
                        if (u.UserName.ToString().ToLower().IndexOf(SearchText.ToLower()) > -1 || fi.Name.ToLower().IndexOf(SearchText.ToLower()) > -1)
                        {
                            dtFiles.Rows.Add(row);
                            counter = counter + 1;
                        }
                    }
                    else // Add all
                    {
                        dtFiles.Rows.Add(row);
                        counter = counter + 1;
                    }
                }
            }
        }

// NOTE:::THESE LINES WORK WITH THE GRID PERFECTLY AND SHOW THE FILTERED SUBSET
        gvTracks.DataSource = dtFiles;
        gvTracks.DataBind();

// NOTE:::THIS LINE IS IGNORED - I'VE HARD-CODED VALUES THAT ARE NOT REFLECTED ON THE UI
        lblCount.Text = counter.ToString();  //  !!!THIS REFLECTES THE FILTERED REC COUNT WHEN I STEP THROUGH, BUT IT DISPLAYS THE COUNT OF ALL RECS SOMEHOW!!!
    }


// THIS IS THE BUTTON THAT PASSES THE FILTER TEXT TO fillGrid()
    protected void  btnSearch_Click(object sender, EventArgs e)
    {
        fillGrid(txtSearch.Text);
    }

Open in new window

0
 

Author Comment

by:JT_SIRO
ID: 34958121
HEY!  I just decided to wrap my label control in the same update panel that my grid is in and it worked!  Can you offer any insight into why?  I suspect it's because I had triggers in the UpdatePanel that surrounded my grid, that didn't communicate with controls outside of it.  Is that correct?  Even thought my codebehind appeared to set the label values correctly...  Must be.

I didn't really want to include my label or the buttons in the UpdatePanel because they are up near the header of my page, so I could disable the grid on postback.  Now I guess I'll disable the whole panel on postback, so the user can't click anything....  I think.....  uggg, programming is hard..
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
SingleRun is a tool that ensures that only one instance of an application is started, running it again brings the application to focus.
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…

608 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