需要帮助的按钮控件 [英] Need Help with a button control

查看:66
本文介绍了需要帮助的按钮控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这就是我想要做的.我创建了一个aspx页面,该页面从XML文件读取并显示数据.在页面上有4个按钮.第一个联系人,上一个,下一个和最后一个联系人.我的前三个按钮正常工作,但是我无法使最后一个联系人"按钮正常工作.它应该转到XML文件中的最后一条记录,并仅显示该数据.并且由于某种原因,如果单击第一联系人"按钮并转到第一条记录,则该按钮不会变灰.请帮助我,这开始伤害我的大脑.

ok here is what i am trying to do. I have created an aspx page that reads from an XML file and displays the data. On the page there are 4 buttons. First Contact, Previous, Next and Last Contact. I have the first three buttons working right, however, I can not get the Last Contact button to work at all. It should go to the last records in the XML file and display that data only. And for some reason, the First Contact buttons doesnt grey out if you click on it and you go to the first record. Please help me this is starting to hurt my brain.

        protected void btnNext_Click(object sender, EventArgs e)
        {
            if (navigator.MoveToNext())     //moves to the next record.
            {
                DisplayData();      //displays the data when the next button is pushed.
                btnPrevious.Enabled = true;     //makes sure that the previous button is enabled at end of record.
            }
            else
            {
                btnNext.Enabled = false;        //disables the next button at end of contacts.
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            if (navigator.MoveToPrevious())                //move to the previous record.
            {
                DisplayData();      //displays the data when the previous button is pushed.
                btnNext.Enabled = true;     //makes sure that next is able to be pressed if previous is disabled.
            }
            else
            {
                btnPrevious.Enabled = false;        //this will disable the previous button.
            }
        }

        protected void btnFirstContact_Click(object sender, EventArgs e)
        {

        }
                
    }
}

推荐答案



为什么不使用LINQ读取XML文件?
我认为您应该节省一些时间阅读LINQ文章;).

也许这对您有用:

Hi,

why don''t using LINQ for reading XML-File?
I think you should save some time for reading a LINQ Article ;).

Maybe this works for you:

var xml_path = @"C:\contacts.xml";
          var doc = XDocument.Load(xml_path);
          var last_entry = doc.Descendants("Contact").LastOrDefault();
          MessageBox.Show(last_entry.ToString());




干杯.




Cheers.


这是到目前为止的全部代码,

here is the whole code so far,

         XPathDocument document = new XPathDocument(path);       //made XPathDocument in order to navigate to each record.
                navigator = document.CreateNavigator();         //this creates the Navigator.
                navigator.MoveToRoot();     //moves to the Root of the XML file.
                navigator.MoveToChild("Contacts", String.Empty);        //moves to the Child of the Contacts Attribute.
                navigator.MoveToFirstChild();       //moves to the First Child of Contacts.
                
                DisplayData();      //displays the data that we need to show.

                Session["navigator"] = navigator;       //stores the nevigator to be reused in current location.
            }
            else
            {
                navigator = (XPathNavigator)Session["navigator"];       //retrieves the navigator.
            }
        }

        private void DisplayData()      //creates a method so we do not have to retype the same code repeatedly.
        {
            lableID.Text = navigator.GetAttribute("ID", String.Empty);      //displays the ID of the contact.
            navigator.MoveToFirstChild();       //we tell it to move to the Category Attribute.
            lableCategory.Text = navigator.Value;       //displays the category field.
            navigator.MoveToNext();     //tells it to move to the Name Attribute.
            navigator.MoveToFirstChild();       //tells it to move to the First Name.
            lableFirstName.Text = navigator.Value;      //displays the first name field.
            navigator.MoveToNext();     //tells it to move to the Last Name.
            lableLastName.Text = navigator.Value;       //displays the last name field.
            navigator.MoveToParent();       //tells it to go back to Contact.
            navigator.MoveToNext();     //tells it to go to Address.
            navigator.MoveToFirstChild();       //tells it to move to the Street, which is a child of Address.
            lableStreet.Text = navigator.Value;     //displays the street field.
            navigator.MoveToNext();     //moves to the next sibling.
            lableCity.Text = navigator.Value;       //displays the city field.
            navigator.MoveToNext();     //moves tot he next sibling.
            lableState.Text = navigator.Value;      //displays the state field.
            navigator.MoveToNext();     //moves tot he next sibling.
            lableZip.Text = navigator.Value;        //dispalys the zip code.
            navigator.MoveToParent();       //moves back to the parent.
            navigator.MoveToNext();     //moves to the next child.
            lablePhone.Text = navigator.Value;      //displays the phone number field.
            navigator.MoveToParent();       //enables to move to the next Contact node.
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            if (navigator.MoveToNext())     //moves to the next record.
            {
                DisplayData();      //displays the data when the next button is pushed.
                btnPrevious.Enabled = true;     //makes sure that the previous button is enabled at end of record.
            }
            else
            {
                btnNext.Enabled = false;        //disables the next button at end of contacts.
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            if (navigator.MoveToPrevious())                //move to the previous record.
            {
                DisplayData();      //displays the data when the previous button is pushed.
                btnNext.Enabled = true;     //makes sure that next is able to be pressed if previous is disabled.
            }
            else
            {
                btnPrevious.Enabled = false;        //this will disable the previous button.
            }
        }

        protected void btnFirstContact_Click(object sender, EventArgs e)
        {
            
        }
                
    }
}


好,我明白了,这是我想出的解决方案
ok I got it figured out, this is the solution that I came up with
protected void btnMoveToLast_Click(object sender, EventArgs e)
        {
           do
           {
               if (navigator.NodeType == XPathNodeType.Element)
               {
                   if (navigator.HasChildren == true) 
                   {
                       navigator.MoveToFirstChild();

                       do 
                       {
                           DisplayData();
                       }
                       while (navigator.MoveToNext());
                   }
               }
           }
           while (navigator.MoveToNext());
           btnMoveToLast.Enabled = false;
           btnNext.Enabled = false;
           btnPrevious.Enabled = true;
           }


这篇关于需要帮助的按钮控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆