重复表操作

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.XPath;

namespace DeleteRepeatTableMsg
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DeleteRow.Attributes.Add("OnClick", "if(confirm('您确认删除吗?')){return true;}else{return false;}");
            DelselectedRow.Attributes.Add("OnClick", "if(confirm('您确认删除吗?')){return true;}else{return false;}");
            try
            {
                XmlFormView.XmlLocation = "http://cx-files/DGTAX/2009.xml";
                XmlFormView.SaveLocation = "http://cx-files/DGTAX";
                XmlFormView.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }


        }


        protected void LoadForm(string formId, string instanceId)
        {
            //Form_Template ft = new Form_Template(formId);
            ////Response.Write(ft.templateLocation + "---" + ft.saveLocation);
            //XmlFormView.XmlLocation = ft.doclibLocation + "/" + instanceId + ".xml";
            //XmlFormView.SaveLocation = ft.saveLocation;
            ////XmlFormView.ShowFooter = true;
            ////XmlFormView.ShowHeader = true;
            //XmlFormView.DataBind();
            //Response.Write("XmlLocation:<br>" + ft.doclibLocation + "/" + instanceId + ".xml");
            //Response.Write("saveLocation:<br>" + ft.saveLocation);
        }

       

        protected void AddRow_Click(object sender, EventArgs e)
        {
            try
            {
                XPathNavigator group = XmlFormView.XmlForm.MainDataSource.CreateNavigator().SelectSingleNode("/my:root/my:body/my:RepeatTable[last()]", XmlFormView.XmlForm.NamespaceManager);
                XPathNavigator NewRow = group.Clone();
                NewRow.InsertBefore(group);

                //设置默认值

                NewRow.SelectSingleNode("my:A", XmlFormView.XmlForm.NamespaceManager).SetValue(GetRowCount().ToString());
                NewRow.SelectSingleNode("my:B", XmlFormView.XmlForm.NamespaceManager).SetValue(Convert.ToInt32(99 + GetRowCount()).ToString());
                NewRow.SelectSingleNode("my:C", XmlFormView.XmlForm.NamespaceManager).SetValue("2009-01-20");
            }
            catch (Exception ex)
            {
                throw ex;
            }

 

        }

        protected void DeleteRow_Click(object sender, EventArgs e)
        {
            try
            {
                //这样只能删除最后一行
                // XPathNavigator parent = XmlFormView.XmlForm.MainDataSource.CreateNavigator().SelectSingleNode("/my:root/my:body/my:RepeatTable[last()]", XmlFormView.XmlForm.NamespaceManager);
                // group.DeleteSelf();

                //删除指定行
                XPathNavigator parent = XmlFormView.XmlForm.MainDataSource.CreateNavigator().SelectSingleNode("/my:root/my:body", XmlFormView.XmlForm.NamespaceManager);
                XPathNodeIterator rowitor = parent.Select("my:RepeatTable", XmlFormView.XmlForm.NamespaceManager);
                for (int i = 0; i < Convert.ToInt32(RowNO.Text); i++)
                {
                    rowitor.MoveNext();
                }
                rowitor.Current.DeleteSelf();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        protected void SwichView_Click(object sender, EventArgs e)
        {
            try
            {
                XPathNavigator viewID = XmlFormView.XmlForm.MainDataSource.CreateNavigator().SelectSingleNode("/my:root/my:body/my:viewID", XmlFormView.XmlForm.NamespaceManager);

                viewID.SetValue(ViewID.SelectedValue);

                XmlFormView.XmlForm.Submit();
                //XmlFormView.XmlForm.SetSaveAsDialogFilename("2009-02-04.xml");

                Response.Redirect("redi.aspx");

            }
            catch (Exception ex)
            {
                throw ex;
            }

            //只能在VSTA托管代码中使用,服务器端不支持
            //XmlFormView.XmlForm.ViewInfos.SwitchView(DropDownList1.SelectedValue);

            //XmlFormView.XmlForm.ViewInfos.Initial = XmlFormView.XmlForm.ViewInfos["FirstView"];

        }

        protected void CalA_Click(object sender, EventArgs e)
        {
            try
            {
                XPathNavigator xn = XmlFormView.XmlForm.MainDataSource.CreateNavigator();
                string strExpression = "/my:root/my:body/my:RepeatTable/my:A[../my:A>0]";
                XPathExpression xe = XPathExpression.Compile(strExpression, XmlFormView.XmlForm.NamespaceManager);

                //XPathNodeIterator rowitor = xn.Select(xe);
                XPathNodeIterator rowitor = xn.Select(strExpression, XmlFormView.XmlForm.NamespaceManager);

                decimal tempnum = 0;

                while (rowitor.MoveNext())
                {
                    tempnum += Convert.ToDecimal(rowitor.Current.Value);
                }

                //下面的写法是错误的!
                //XPathNodeIterator rowitor = xn.Select("my:RepeatTable", XmlFormView.XmlForm.NamespaceManager);

                //for (int i = 0; i < rowitor.Count;i++ )
                //{

                //    tempnum += Convert.ToInt32(rowitor.Current.SelectSingleNode("//my:A", XmlFormView.XmlForm.NamespaceManager).Value);
                //    rowitor.MoveNext();

                //}

                CalResult.Text = tempnum.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }


        }

        protected void DelselectedRow_Click(object sender, EventArgs e)
        {
            try
            {
                XPathNavigator xn = XmlFormView.XmlForm.MainDataSource.CreateNavigator();
                string strExpression = @"/my:root/my:body/my:RepeatTable[my:delCheck='true']";

                XPathNodeIterator rowitor = xn.Select(strExpression, XmlFormView.XmlForm.NamespaceManager);

                int i = 0;

                while (rowitor.Count > 0)
                {
                    i++;
                    rowitor.MoveNext();
                    rowitor.Current.DeleteSelf();
                    rowitor = xn.Select(strExpression, XmlFormView.XmlForm.NamespaceManager);
                }
                XmlFormView.XmlForm.Submit();
                Response.Redirect("redi.aspx");
            }
            catch (Exception ex)
            {
                throw ex;
            }


            //失败的方法一
            //while (rowitor.MoveNext())
            //{

            //    i++;
            //    rowitor.Current.DeleteSelf();
            //    if (rowitor.Current.HasChildren)
            //        rowitor.Current.MoveToFirstChild();  

            //}

            //失败的方法二
            //for (int j = 0; j < rowitor.Count;j++ )
            //{
            //    i++;
            //    rowitor.MoveNext();
            //    rowitor.Current.DeleteSelf();
            //}

            //只能在VSTA托管代码中使用,服务器端不支持
            //XPathNodeIterator selRow = XmlFormView.XmlForm.CurrentView.GetSelectedNodes();
            //selRow.Current.DeleteSelf();
         
        }

        protected void DelRange_Click(object sender, EventArgs e)
        {
            try
            {
                XPathNavigator xn = XmlFormView.XmlForm.MainDataSource.CreateNavigator();

                XPathNavigator n1 = xn.SelectSingleNode("/my:root/my:body/my:RepeatTable[" + firstRow.Text + "]", XmlFormView.XmlForm.NamespaceManager);

                XPathNavigator n2 = n1.SelectSingleNode("/my:root/my:body/my:RepeatTable[" + lastRow.Text + "]", XmlFormView.XmlForm.NamespaceManager);

                xn.MoveTo(n1);

                xn.DeleteRange(n2);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        protected int GetRowCount()
        {
            XPathNavigator xn = XmlFormView.XmlForm.MainDataSource.CreateNavigator();
            string strExpression = @"/my:root/my:body/my:RepeatTable";

            XPathNodeIterator rowitor = xn.Select(strExpression, XmlFormView.XmlForm.NamespaceManager);

            return rowitor.Count;
        }
    }
}

你可能感兴趣的:(操作)