更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢!
一、Asp.net工程
![](https://i-blog.csdnimg.cn/blog_migrate/d699fb0e40e2beb49bca36efe9615591.png)
使用asp.net 数据控件 gridview显示数据
数据源采用 xml(好处是结构规范、便于快速开发,绕开权限控制等)
Button采用imgbutton,没有使用GridView自带的文字linkbutton,美观,易于美化。
用VS2010打开Tool栏编辑器,如下图:
1、添加新网页
2、拖动gridview到页面中
3、前段代码自动生成
打开VS2010,展开Solution查看文件结构:
主要资源:
1、图片
2、数据
3、页面
4、类文件
二、常见代码
GridView 控件的HTML代码:
asp:GridView ID="GridView1" runat="server">
</asp:GridView>
GridView控件事件添加,同时生成后台代码,如下图:
前端代码如下:
<asp:GridView ID="gv_xml" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="gv_xml_RowCancelingEdit" OnRowUpdating="gv_xml_RowUpdating" OnRowEditing="gv_xml_RowEditing" OnRowDeleting="gv_xml_RowDeleting" CellPadding="4" ForeColor="#333333" GridLines="None" > <Columns> <asp:TemplateField HeaderText="域名"> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem,"Domain") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="Txtbox0" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Domain") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="FAT1"> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "FAT1")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="Txtbox1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"FAT1") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="FAT8"> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "FAT8")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="Txtbox2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"FAT8") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="FAT21"> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "FAT21")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="Txtbox3" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"FAT21") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="FAT22"> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "FAT22")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="Txtbox4" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"FAT22") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="FAT23"> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "FAT23")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="Txtbox5" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"FAT23") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:ImageButton ID="Button2" runat="server" Text="Edit" ImageUrl="~/Imgs/btnEdit.png" CommandName="Edit" /> <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Imgs/btnDelete.png" CommandName="Delete" /> </ItemTemplate> <EditItemTemplate> <asp:ImageButton ID="Button1" runat="server" Text="Update" ImageUrl="~/Imgs/btnUpdate.png" CommandName="Update" /> <asp:ImageButton ID="Button3" runat="server" Text="Cancel" ImageUrl="~/Imgs/btnCancel.png" CommandName="Cancel" /> </EditItemTemplate> </asp:TemplateField> </Columns> <AlternatingRowStyle Wrap="False" BackColor="White" /> <EditRowStyle BackColor="#2461BF" /> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#EFF3FB" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <SortedAscendingCellStyle BackColor="#F5F7FB" /> <SortedAscendingHeaderStyle BackColor="#6D95E1" /> <SortedDescendingCellStyle BackColor="#E9EBEF" /> <SortedDescendingHeaderStyle BackColor="#4870BE" /> </asp:GridView>
后台对应的代码如下:
protected void gv_xml_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { gv_xml.EditIndex = -1; Databind(); } protected void gv_xml_RowEditing(object sender, GridViewEditEventArgs e) { gv_xml.EditIndex = e.NewEditIndex; Databind(); } protected void gv_xml_RowUpdating(object sender, GridViewUpdateEventArgs e) { string Xmlpath = String.Format("{0}\\APIData\\FatEnvData.xml", RootPath); GridViewRow row = gv_xml.Rows[e.RowIndex]; //获得当前行 int numCell = row.Cells.Count; //共几列单元格(包含Edit和Delete 2列) int currentRow = row.DataItemIndex; //对应DataSet对应的行索引 DataSet ds = new DataSet(); ds.ReadXml(Xmlpath); DataRow dr; dr = ds.Tables[0].Rows[row.DataItemIndex]; string[] str = null; //此数组定义表的列名 str = new string[]{ "Domain", "FAT1", "FAT8", "FAT21", "FAT22", "FAT23"}; int j = 0; TextBox myTextBox = null; //从第1列开始, 后2列是Edit和Delete for (int i = 0; i < numCell-1; i++) { myTextBox = row.Cells[i].FindControl("Txtbox"+i) as TextBox; //string cText = ((TextBox)row.Cells[i].Controls[0]).Text; dr[str[j]] = myTextBox.Text; j++; } ds.WriteXml(Xmlpath); //将修改写入Table.xml gv_xml.EditIndex = -1; Databind(); } protected void gv_xml_RowDeleting(object sender, GridViewDeleteEventArgs e) { string Xmlpath = String.Format("{0}\\APIData\\FatEnvData.xml", RootPath); GridViewRow row = this.gv_xml.Rows[e.RowIndex]; int curr = row.RowIndex; DataSet ds = new DataSet(); ds.ReadXml(Xmlpath); DataRow dr = ds.Tables[0].Rows[curr]; dr.Delete(); ds.WriteXml(Xmlpath); Databind(); }
页面加载时,数据绑定:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) Databind(); } //xml 数据绑定 public void Databind() { var query = GetXMLData(); gv_xml.DataSource = query; gv_xml.DataBind(); } //查询xml数据 private List<LinqXmlGridViewControl> GetXMLData() { var xDoc = XDocument.Load(Server.MapPath("~/APIData/FatEnvData.xml")); var query = (from LinqXmlGridViewControl in xDoc.Descendants("Server") select new LinqXmlGridViewControl() { Domain = LinqXmlGridViewControl.Element("Domain").Value, FAT1 = LinqXmlGridViewControl.Element("FAT1").Value, FAT8 = LinqXmlGridViewControl.Element("FAT8").Value, FAT21 = LinqXmlGridViewControl.Element("FAT21").Value, FAT22 = LinqXmlGridViewControl.Element("FAT22").Value, FAT23 = LinqXmlGridViewControl.Element("FAT23").Value }).ToList(); return query; }
三、网站部署
代码完成后,需要部署在IIS,打开控制面板-〉控制面板项目-〉管理工具:
在网站上,点击鼠标右键选择添加网站,如下图:
应用程序池默认选择 .Net4.0 ,否则IIS网站无法启动:
物理路径选择,本地网站文件存放地址。Ip选择 本地机器ip,默认端口80
主机名如果有域名可以设置,没有则为空。
部署完成
如下图,此处可以更换pool;路径重新指定;
公司目前用工具自动配置,如果出问题可以登录服务器进行手工修改(网站连接配置,跳转指向,数据库连接串,代码版本查看等等)
作者原创技术文章,转载请注明出处