List保存也XML文件。两个dropDownList和一个GridView的挑选和展示。

今天咱们上学如何将List<T>写成一个XML文件保存起来。因为我们于召开动态网站开时,需要针对有无太经常变化之数码来也XML文件,让程序直接去读取,而非是历次是SQL数据库取。

大遥远无写ASP.NET了,今天时有发生探望论坛及一个题目:
“两独dropDownList和一个GridView,已经展开了数绑定,现在纪念让第一单下拉菜单的多少变动时,第二只下拉菜单自动转换至相应的多寡,同时入选gridview中互对应之尽,不知道怎样落实,很焦急,求大神相助”
图片 1

为了解决一个题材,我们得用先创造一个自定义的Result()在ASP.NET
MVC中。这个Result操作可以继承ActionResult或者ContentResult。

事实上,实现起来竟得及简单,下面先从准备数据开始,创建一个目标Customer:
图片 2

图片 3

source code:

 

图片 4图片 5

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Customer
/// </summary>
namespace Insus.NET
{
    public class Customer
    {
        private int _CustomerID;
        private string _CustomerName;
        private string _PID;
        private bool _IsActived;

        public int CustomerID
        {
            get { return _CustomerID; }
            set { _CustomerID = value; }
        }
        public string CustomerName
        {
            get { return _CustomerName; }
            set { _CustomerName = value; }
        }
        public string PID
        {
            get { return _PID; }
            set { _PID = value; }
        }
        public bool IsActived
        {
            get { return _IsActived; }
            set { _IsActived = value; }
        }
    }
}

图片 6图片 7

View Code

 class XmlResult<T> : ActionResult
    {
        public T Data { private get; set; }
        public string FilePhysicalPath { private get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            XmlSerializer xml = new XmlSerializer(typeof(T));

            using (StreamWriter writer = new StreamWriter(FilePhysicalPath))
            {
                xml.Serialize(writer, Data);
            }
        }
    }

接通下,我们创建一个实具,数据来源都在此类实现 CustomerEntity:
图片 8

View Code

 

创建Model:
图片 9

source code:

 

图片 10图片 11

缔造一个Entity,是数码的聚众:
图片 12

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for CustomerEntity
/// </summary>
namespace Insus.NET
{
    public class CustomerEntity
    {
        public CustomerEntity()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public IEnumerable<Customer> Customers = new List<Customer> {
            new Customer {CustomerID = 9,CustomerName = "张三",PID = "123456789012"  },
            new Customer {CustomerID = 10,CustomerName = "李四",PID = "321245677812"  },
            new Customer {CustomerID = 30,CustomerName = "吴广",PID = "213445678912"  },
            new Customer {CustomerID = 35,CustomerName = "王维",PID = "334456789012"  },
            new Customer {CustomerID = 36,CustomerName = "赵勇",PID = "213445678912"  }
        };


        public IEnumerable<Customer> GetForFirstDropDownData()
        {
            var oo = Customers.Select(o => new Customer { CustomerID = o.CustomerID, CustomerName = o.CustomerName });
            return oo;
        }

        public IEnumerable<Customer> GetForSecondDropDownData(int customerId)
        {
            var oo = Customers.Select(o => new Customer { CustomerID = o.CustomerID, PID = o.PID })
                                .Where(w => w.CustomerID == customerId);
            return oo;
        }


        public IEnumerable<Customer> GetForGridview(int customerId)
        {
            List<Customer> _cust = new List<Customer>();
            foreach (Customer c in Customers)
            {
                if (c.CustomerID == customerId)
                    _cust.Add(new Customer { CustomerID = c.CustomerID, CustomerName = c.CustomerName, PID = c.PID, IsActived = true });
                else
                    _cust.Add(c);
            }
            return _cust;
        }

    }
}

 

View Code

code source:

Ok,数据准备好了,下面就是可创建网页实现相关的功力xxx.aspx:
图片 13

图片 14图片 15

source code:

 public class NumberEntity
    {
        public List<Number> Numbers()
        {
            return new List<Number>(){
                new Number() { Num = 20 },
                new Number() { Num = 40 },
                new Number() { Num = 60 },
                new Number() { Num = 80 }
            };
        }
    }

图片 16图片 17

View Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
                AutoPostBack="true">
            </asp:DropDownList>&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>

            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:TemplateField HeaderText="CustomerID">
                        <ItemTemplate>
                            <%# Eval("CustomerID") %>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="CustomerName">
                        <ItemTemplate>
                            <%# Eval("CustomerName") %>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="PID">
                        <ItemTemplate>
                            <%# Eval("PID") %>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>

连下去,创建控制器

View Code

图片 18

xxx.aspx.cs:
图片 19

code source:

 

图片 20图片 21

source code:

 public ActionResult XmlDemo()
        {
            return View();
        }

        public ActionResult ConvertListSaveFile()
        {
            NumberEntity ne = new NumberEntity();

            string fileName = Guid.NewGuid().ToString() + ".xml";
            string filephysicalPath = Server.MapPath("~/Temp/") + fileName;

            return new XmlResult<List<Number>>()
            {
                Data = ne.Numbers(),
                FilePhysicalPath = filephysicalPath
            };
        }

图片 22图片 23

View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    CustomerEntity ce = new CustomerEntity();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Data_Binding();
        }
    }

    private void Data_Binding()
    {
        this.DropDownList1.DataSource = ce.GetForFirstDropDownData();
        this.DropDownList1.DataValueField = "CustomerID";
        this.DropDownList1.DataTextField = "CustomerName";
        this.DropDownList1.DataBind();

        this.DropDownList2.DataSource = ce.GetForSecondDropDownData(Convert.ToInt32(this.DropDownList1.SelectedItem.Value));
        this.DropDownList2.DataTextField = "PID";
        this.DropDownList2.DataBind();

        this.GridView1.DataSource = ce.GetForGridview(Convert.ToInt32(this.DropDownList1.SelectedItem.Value));
        this.GridView1.DataBind();
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var ddl = (DropDownList)sender;
        this.DropDownList2.DataSource = ce.GetForSecondDropDownData(Convert.ToInt32(ddl.SelectedItem.Value));
        this.DropDownList2.DataTextField = "PID";
        this.DropDownList2.DataBind();

        this.GridView1.DataSource = ce.GetForGridview(Convert.ToInt32(ddl.SelectedItem.Value));
        this.GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.DataRow) return;

        Customer drv = (Customer)e.Row.DataItem;

        bool isActived = (bool)drv.IsActived;

        if (isActived)
            e.Row.BackColor = System.Drawing.Color.Red;
    }
}

好视图,简单:
图片 24

View Code

 效果演示:
图片 25

动画实时演示:
图片 26

 

 

相关文章