Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi Everyone,

Currently my webpage displays list of emails in gridview and emails folders from treeview. However, if I change page index on Initial Page Load/First Time Load it gives me error.

But If i click on any of the TreeView and then click on Gridview pagination. It does not return any error.

C#
<pre>protected void Page_Load(object sender, EventArgs e)
        {
            B_EmailMsgDelete.Attributes.Add("onclick", 
        "javascript:if(!doDeleteEmailMsg()) return false;");
            IB_EmailMover.Attributes.Add("onclick", "javascript:if(!doMoveEmailMsg()) return false;");
            
            if (!IsPostBack)
            {
                TreeNode rootTreeNode = new TreeNode();
                rootTreeNode.Text = string.Format(CultureInfo.CurrentCulture, " {0}", this.curEmailBox);
                rootTreeNode.Value = this.EmailFolderList[0];
                rootTreeNode.ImageUrl = "../../Images/emailbox.gif";

                for (int i = 0; i < this.EmailFolderList.Length; i++)
                {
                    if (i > 0)
                    {
                        TreeNode treeNode = new TreeNode();
                        treeNode.Text = string.Format(CultureInfo.CurrentCulture, " {0}", this.EmailFolderList[i]);
                        treeNode.Value = this.EmailFolderList[i];
                        treeNode.ImageUrl = "../../Images/folder.gif";
                        rootTreeNode.ChildNodes.Add(treeNode);
                    }

                    DL_MoveTo.Items.Add(this.EmailFolderList[i]);
                }

                TV_EmaiBox.Nodes.Add(rootTreeNode);
                this.GetListEmailList(rootTreeNode.Value);
            }
        }


TreeView On Page Change Index
C#
protected void TV_EmaiBox_SelectedNodeChanged(object sender, EventArgs e)
        {
            string checkFolder = TV_EmaiBox.SelectedNode.Value.ToString();
            this.GetListEmailList(checkFolder);
        }


GridList On Page Index
C#
protected void GV_EmailList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GV_EmailList.PageIndex = e.NewPageIndex;
    string checkFolder = TV_EmaiBox.SelectedNode.Value.ToString();
    this.GetListEmailList(checkFolder);
}


GetEmailList Method
C#
private void GetListEmailList(string folderName)
       {
           try
           {
               LB_ActionError.Text = string.Empty;
               PH_MsgDetail.Visible = true;

               var emails = EmailMsg.GetEmailListInFolder(folderName);
               LB_CurBox.Text = string.Format(CultureInfo.CurrentCulture, "{0} ({1})", folderName, emails.Count);

               int intTotal = emails.Count;
               if (intTotal == 0)
               {
                   LB_ActionStatus.Text = Utility.SetActStatus(true, "There is no email message found in this folder.");
                   GV_EmailList.DataSource = new ArrayList();
                   GV_EmailList.DataBind();
                   GV_EmailList.Visible = false;
               }
               else
               {
                   LB_ActionStatus.Text = Utility.SetActStatus(true, string.Format(CultureInfo.CurrentCulture, "There are {0} email message(s) found in this folder.", intTotal));
                   GV_EmailList.DataSource = emails;
                   GV_EmailList.DataBind();
                   GV_EmailList.Visible = true;

                   GV_EmailList.SelectedIndex = -1;
               }
           }
           catch (Exception ex)
           {
               LB_ActionStatus.Text = Utility.SetActStatus(false, ex.Message.ToString());
           }

           LB_AdminEmailBox.Text = string.Format(CultureInfo.CurrentCulture, Entity.GetSetting("MailBoxAddress"), folderName);
       }
   }


What I have tried:

I have tried many approaches but no luck so far. What I want is when this page is loaded for the first time. GridView page index should not return any when I change page index. 
Posted
Updated 29-May-19 4:20am
Comments
Richard Deeming 29-May-19 9:39am    
"it gives me error"

Click "Improve question" and update your question with the full details of the error. Remember to indicate which line of code it's thrown from.

1 solution

Events do not always fire when you "think" they will. You cannot assume things are in a valid state when they do fire. (particularly during "start up")

You cannot assume a "selected item" (node) is valid unless you check for null first, for example.

string checkFolder = TV_EmaiBox.SelectedNode.Value.ToString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900