ASP.NET动态增加控件 应用篇(二)

来源:未知 责任编辑:智问网络 发表时间:2013-09-02 11:47 点击:

 

此篇,是 前篇 应用篇(一)的延续,也是进阶版。在本篇中使用一个aspx 档案来同时实现,资料清单的显示以及资料编辑同在一起的示范。

当然,资料字段少的时候,可以直接 使用GridView 就直接实现,但在字段多的时候,或是编辑画面要求复杂时,这时在使用GridView 就感觉很麻烦了。

在本篇中 会使用 几个程式相关档案,分别是,

test5.aspx 主页面

MyBase5.aspx.cs 基底程式

Customer 类,Value Object

test5.xml 用来储存示范资料

详细介绍如下:

 

在MyBase5 中有各重点就是,一开始控件,要在Init 时就要全部产生出来,当然这是各偷懒的行为,读者 有兴趣,可以 再去尝试判断页面状态,来决定来些要产生 哪些不产生。

在这个程式中还有各要注意的地方,就是sourceTag(编辑页面) 跟GridView1(主页面) 的显示控制,一开始 需先将sourceTag 隐藏,因为本页面,user 无任何操作一进来要看到的页面是GridView1,sourceTag 是进来页面後 视操作的结果来决定是否显示的。

 

MyBase5.aspx.cs

  1 using System;

  2 using System.Data;

  3 using System.Collections;

  4 using System.Web.UI;

  5 using System.Web.UI.WebControls;

  6 using System.Web.UI.HtmlControls;

  7 using System.Text;

  8 using System.Configuration;

  9

 10 public class MyBase5 : System.Web.UI.Page{

 11

 12     protected GridView GridView1;

 13     protected HtmlGenericControl sourceTag;

 14     protected Button btnSubmit;

 15 www.2cto.com

 16     protected override void OnInit(EventArgs e)

 17     {

 18         base.OnInit(e);

 19         HtmlGenericControl msgTag = new HtmlGenericControl();

 20         msgTag.ID = "divMsg";

 21         sourceTag.Controls.Add(msgTag);

 22

 23         Button btnSave = new Button();

 24         btnSave.Click += new EventHandler(btnSave_Click);

 25         btnSave.Text = "储存";

 26         sourceTag.Controls.Add(btnSave);

 27

 28         Button btnMain = new Button();

 29         btnMain.Click += new EventHandler(btnMain_Click);

 30         btnMain.Text = "回主页面";

 31         sourceTag.Controls.Add(btnMain);

 32

 33         Table myTable = new Table();

 34         sourceTag.Controls.Add(myTable);

 35         dealTableData(myTable);

 36         sourceTag.Visible = false;

 37

 38         GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);

 39     }

 40     protected override void OnLoad(EventArgs e)

 41     {

 42         base.OnLoad(e);

 43

 44         if (!IsPostBack)

 45         {

 46             DataBind();

 47         }

 48     }

 49

 50     public virtual void DataBind()

 51     {

 52

 53     }

 54     protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

 55     {

 56         try

 57         {

 58             Object myObj = GetGridData(e.NewEditIndex);

 59             if (myObj != null)

 60             {

 61                 ShowEditData(myObj);

 62                 Customer myCus = (Customer)myObj;

 63                 sourceTag.Visible = true;

 64                 GridView1.Visible = false;

 65             }

 66         }

 67         catch (Exception ex)

 68         {

 69             ShowMessage(ex.ToString());

 70         }

 71     }

 72     //取得GridView 特定列的资料

 73     public virtual Object GetGridData(int EditIndex)

 74     {

 75         return null;

 76     }

 77     //生成编辑画面的控件

 78     public virtual void dealTableData(Table myTable)

 79     {

 80

 81     }

 82     //设定编辑画面控件的预设值

 83     public virtual void ShowEditData(Object fVObj)

 84     {

 85

 86     }

 87     //储存 按键触发后的处里

 88     public virtual void btnSave_Click(Object sender, EventArgs e)

 89     {

 90     }

 91     //回主页面 按键触发后的处里

 92     public virtual void btnMain_Click(Object sender, EventArgs e)

 93     {

 94         sourceTag.Visible = false;

 95         GridView1.Visible = true;

 96         GridView1.EditIndex = -1;

 97         DataBind();

 98     }

 99     public virtual void ShowMessage(string msg)

100     {

101         HtmlGenericControl msgTag = (HtmlGenericControl)sourceTag.FindControl("divMsg");

102         LiteralControl lc;

103         lc = new LiteralControl(msg + "<br>");

104         msgTag.Controls.Add(lc);

105     }

106 }

 

 

在test5.aspx 跟 前篇用途是一样的,在此篇中主要是多一个GridView 的处理 所以 变复杂一点

test5.aspx

 1 <%@ Page aspcompat="true" language="C#" EnableEventValidation="false" explicit="true" src="./myBase5.aspx.cs" Inherits="MyBase5"%>

 2 <%@ import namespace="System.Data" %>

 3 <%@ import namespace="System.Data.SqlClient" %>

 4

 5    <script language="C#" runat=server>

 6

 7     public override void DataBind()

 8     {

 9         DataSet d = new DataSet();

10         d.ReadXml(Server.MapPath("test5.xml"));

11         GridView1.DataSource = d.Tables[0];

12         GridView1.DataBind();

13     }

14

15     public override Object GetGridData(int EditIndex)

16     {

17         int i = 1;

18         Customer myCus = new Customer();

19         GridViewRow row = GridView1.Rows[EditIndex];

20         myCus.客户编号= row.Cells[i++].Text;

21         myCus.联络人= row.Cells[i++].Text;

22         myCus.电话= row.Cells[i++].Text;

23         myCus.传真= row.Cells[i++].Text;

24         return myCus;

25     }      

26     public override void dealTableData(Table myTable)

27     {

28         TxtItem ti;

29

30         cusTable myCT = new cusTable(myTable);

31

32         myCT.AddRow();

33         ti = new TxtItem("txCardCode", "客户编号:");

34         myCT.AddEditControl(ti);

35         ti = new TxtItem("txContact", "联络人:");

36         myCT.AddEditControl(ti);

37

38         myCT.AddRow();

39         ti = new TxtItem("txPhone", "电话:");

40         myCT.AddEditControl(ti);

41         ti = new TxtItem("txFax", "传真:");

42         myCT.AddEditControl(ti);

43     }

44     public override void ShowEditData(Object fVObj)

45     {

46         Customer myCus = (Customer)fVObj;

47         TextBox tb;

48         string s = "";   

49         tb = (TextBox)sourceTag.FindControl("txCardCode");

50         tb.Text = myCus.客户编号;

51         tb = (TextBox)sourceTag.FindControl("txContact");

52         tb.Text = myCus.联络人;

53         tb = (TextBox)sourceTag.FindControl("txPhone");

54         tb.Text = myCus.电话;

55         tb = (TextBox)sourceTag.FindControl("txFax");

56         tb.Text = myCus.传真;

57

58     }

59     public override void btnSave_Click(Object sender, EventArgs e)

60     {

61         TextBox tb;

62         string s = "";

63         tb = (TextBox)sourceTag.FindControl("txCardCode");

64         s += tb.Text;

65         tb = (TextBox)sourceTag.FindControl("txContact");

66         s += "," + tb.Text;

67         tb = (TextBox)sourceTag.FindControl("txPhone");

68         s += "," + tb.Text;

69         tb = (TextBox)sourceTag.FindControl("txFax");

70         s += "," + tb.Text;

71

72         ShowMessage(s);

73     }

74    </script>

75 <html>

76 <head>

77     <meta http-equiv="content-type" content="text/html; charset=utf-8" />

78 </head>

79 <body>

80    <form runat="server">

81

82       <h3>动态增加控件 应用篇(二)</h3>

83       

84       <p>

85         <asp:GridView ID="GridView1" runat="server">       

86         <Columns>

87             <asp:CommandField HeaderText="编辑" ShowEditButton="True" />

88         </Columns>

89         </asp:gridview>

90       <div id="sourceTag"  runat="server">

91       </div>

92       <p>

93       一条小龙

94    </form>

95 </body>

96 </html>

 

 

Customer

 1 public class Customer

 2 {

 3     public string 客户编号;

 4     public string 联络人;

 5     public string 电话;

 6     public string 传真;

 7

 8     public Customer()

 9     {

10     }

11 }

 

 

test5.xml

 1 <?xml version="1.0" encoding="utf-8"?>

 2 <customers>

 3   <customer>

 4     <客户编号>一條小龍</客户编号>

 5     <联络人>大龍</联络人>

 6     <电话>123456780</电话>

 7     <传真>123456710</传真>

 8   </customer>

 9   <customer>

10     <客户编号>一條小龍分公司</客户编号>

11     <联络人>小龍</联络人>

12     <电话>123456789</电话>

13     <传真>123456711</传真>

14   </customer>

15 </customers>

 

\

 

\

 

 

 

 

 

 

~~~ 一条小龙(babydragoner) ~~~

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • 浅析.NET下XML数据访问新机制
    • asp.net 面试+笔试题目第1/2页
    • C# 邮件地址是否合法的验证
    • asp.net 设置GridView的选中行的实现代码
    • C#高级编程:数据库连接[1]
    • 经典C++程序1
    • IIS 自动回收导致后台定时器失效的问题解决
    • ASP.NET&#160;GridView列表代码示例
    • Asp.net MVC源码分析--Action Filter的链式调用
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1